Rename A Group Of Files - batch-file

I need to rename a group of files in the same folder.
When I try to run the batch file, it doesn't work correctly:
`ren *.txt Updated_*.txt`
The file names contain date_names_location.txt, examples are below
08232013_name1_nyc.txt
08212013_name1_nyc.txt
08232013_name1_la.txt
08212013_name1_la.txt
When I run the batch file I get back:
Updated_1_name1_nyc.txt
instead of
'Updated_08232013_name1_nyc.txt'
Any ideas on how to fix? Thanks

This is one way:
#echo off
for /f "delims=" %%a in ('dir /b /a-d *.txt') do ren "%%a" "Updated_%%a"

REN has no insert mode, so it just replaces the beginning of your file names. Try the solution provided here
Batch renaming files using Windows 7 REN (adding prefix)?

Related

Windows delete many files

i'm trying to delete many files in many sub-folders with random extensions, how can i do that?
I have many encrypted files (ransomware), now i decrypted all files and i have a duplicate each file (30.000 files).
All encrypted files have a random name extensions with 6 characters
like "namefile.pdf.123456" / "namefile.docx.ujyrtf"
Can you help me to write e script to delete all these files?
Are stored in folder and sub-folders
Maybe i can try to use a Multiple File Renamer to rename .jpg. to *.jpg.del and then delete all *.del by cmd, i don't know if i can do this.
I try to use CMD's like this and i type:
del *.jpg.*
but this command deleted all .jpg and all .jpg.*
Thank you
ATTENTION: there aren't any legit files in these folders. It is a folder data with mkv, jpg, doc, xls, etc. Documents, Video, Audio. Not application or Windows Folder or something like that. Don't use this SCRIPT on system partition, program folders because there may be legit files that would be deleted. Thanks Gerard
If all of the files have a 6 digit extension then we can use a findstr regex to delete them.
from cmd
#for /f "delims=" %i in ('dir /b /a-d *.????.* ^| findstr /r "\.[^\.][^\.][^\.][^\.][^\.][^\.]$"') do #echo del "%i"
or in a batch file:
#echo off
cd /d "C:\Path to files\to delete\"
for /f %%i "delims=" in ('dir /b /a-d *.????.* ^| findstr /r "\.[^\.][^\.][^\.][^\.][^\.][^\.]$"') do echo del "%%i"
This will just echo the result, you need to remove echo only once you can confirm that it does not delete un-intended files.

Batch - Rename Filetype and overwrite file

I have a batch file which is using the following code:
cd ..\changed
for /f "delims=" %%i in ('dir /b *.txt') do (
ren "%%i" "*.xxx"
)
The change of filetype works great, but there is a problem when in folder changed are two files with the same name. Is there any posibility how to overwrite file with the same name using similar code?
Thank You

Copy only htm not html files batch windows

I created a windows batch file to copy only files with specific extensions into a different folder. Here is the line of code I used:
for /R "%cd%" %%f in (*.htm) do copy "%%f" "%cd%\myfolder"
The issue is that this will copy any extension that starts with .htm, i.e. .html, which I do not want; only .htm. How is this copy prevented?
I've tried
"(*.htm)"
("*.htm")
(*".htm")
(*."htm")
(*.htm*)
Thanks
Solution:
for /R %%f in (*.htm) do if /I "%%~xf" == ".htm" copy "%%f" "myfolder"
Thanks #Aacini and #Monacraft
This should work:
for /R %%f in (*.htm) do if /I "%%~xf" == ".htm" copy "%%f" "myfolder"
A couple comments unrelated to your problem:
%cd% is a variable that is always replaced by the current folder. If you give any name without a previous path, the name is assumed to be in the current folder. This way name and %cd%\name is exactly the same and the second one is customarily never used.
In for /R [path] %%f ... command, if the path is not given, the current folder is assumed.
You could check using an if statement:
for /R "%cd%" %%f in (*.*) do if /i "%%~xf"==".htm" copy "%%f" "%cd%\myfolder"
And that is the logical way to do this in batch.
Mona
The reason *.htm matches .html files is because of short 8.3 file names. A file with .html extension will have a short name with .htm extension.
Monacraft and Aacini have provided working solutions using IF statemnts within the body of the loop.
Here is a solution that uses DIR /B piped to FINDSTR within a FOR /F IN() clause.
for /f "eol=: delims=" %%F in ('dir /b /s /a-d-h-s *.html ^| findstr /lie .htm') do copy "%%F" "myfolder"
There's probably a better way, but (*.ht?) should do it.
If you are working with a windows vista or later OS, you can use robocopy and exclude not needed files
robocopy "%cd%" "%cd%\myFolder" *.htm /xf *.html

Change extension of specific filetype on current folder and subdirectories with .bat

I would like to rename all the .log as .ok from a particular folder and subdirectories
The following will usually work just fine:
#echo off
for /r "PathToYourFolderHere" %%F in (.) do ren "%%F\*.log" *.ok
But the above can have problems if short file names are enabled on your drive and you have extensions longer than 3 characters. It will also rename files like name.log2 because the short name will have an extension of .log.
The following will only rename true .log files:
#echo off
for /f "eol=: delims=" %%F in (
'"dir /b /s /a-d PathToYourFolder\*.log|findstr /lie .log"'
) do ren "%%F" *.ok
Note: The rules for how RENAME treats wildcards can be found at How does the Windows RENAME command interpret wildcards?
run a .bat file from the folder containing:
for /R %%x in (*.log) do rename "%%x" "%%~nx.ok"
/R for recursive
%%~nx for the filename without extension

Batch Script - Empty Multiple Directories In Paths With A Pattern

I'm looking to write a short batch script that will delete all files within a set of directories. More specifically, suppose I have the top directory "workspace" and it contains several directories beginning with the sting "project" (e.g. project-something, project-another). Then each of these "project" directories contain a "model" directory. I want to have the script empty each of these model directories.
I know this is doesn't work, but I looking for something along the lines of
del project*\model\*
But I know that the * after project will not select all directories starting with project then proceed into the model directories to clear them. What would be a correct way to go about doing this?
Thank you for your time!
Put this into a .bat file and run.
#echo off
for /F "usebackq delims=" %%F in (`dir /ad /s /b model`) do (
del /s /q "%%F"
echo Removed "%%F"
)
pause

Resources