Batch file rename multiple files only echos the result - windows

I was having an issue with creating a batch file (.bat) to rename multiple files in the same directory based on specific parameters. In order to make it happen for multiple files I had to add an incremental value to it.
From research(mostly here) I pieced together the below command, but whatever I do I cannot actually get to do it. It just echos what's supposed to do and when I remove the echo from before 'ren' it says bad syntax.
#echo off
CD C:\FolderPath\
setlocal enabledelayedexpansion
set /a count=0
for /f "tokens=*" %%a in ('dir /b /od *.txt') do (
echo ren CDFF_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%hr%%time:~3,2%%time:~6,2%%time:~9,2%_!count!.txt
set /a count+=1
)

Add the first parameter to ren command (the file to rename):
ren "%%a" "CDFF_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%hr%%time:~3,2%%time:~6,2%%time:~9,2%_!count!.txt"
So the whole code is:
#echo off
setlocal enabledelayedexpansion
CD C:\FolderPath\
set /a count=0
for /f "tokens=*" %%a in ('dir /b /od *.txt') do (
echo ren "%%a" "CDFF_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%hr%%time:~3,2%%time:~6,2%%time:~9,2%_!count!.txt"
set /a count+=1
)
Please note it will not work for files with weird names. Eg. "File( name.txt"

ren requires the input filname as its first argument. Hence your syntax error.
Here is an alternative implementation:
#echo off
setlocal enabledelayedexpansion
set FOLDER=%1
set DATE=%date:~-4,4%%date:~-10,2%%date:~-7,2%_%hr%%time:~3,2%%time:~6,2%%time:~9,2%
set /a count=0
for /r %FOLDER% %%a in (*.txt) do (
ren "%%a" CDFF_%DATE%_!count!.txt
set /a count += 1
)
call as
rename.bat C:\FolderPath

Related

two condtions in one loop (Batch file)

I have built 2 batch files, which each have his own function first one:
Changes the file name to today's date
SET src_folder="C:\DIR_A\"
SET tar_folder="C:\DIR_A\DIR_B"
for /f %%a IN ('dir "%src_folder%" /b') do REN *.xml %time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml
pause
Second one:
Move files from DIR_A to DIR_B
SET src_folder="C:\DIR_A\"
SET tar_folder="C:\DIR_A\DIR_B"
for /f %%a IN ('dir "%src_folder%*.xml" /b') do move %src_folder%\%%a %tar_folder%
pause
My question is: how can I combine them both in one loop?
Second question is with the naming loop. The loop only names the first XML file, and says that the name already exist. That's true therefor is there anyway to make it pause before it name again, so the files have different names?
Simple: to use several commands, use a code block:
for %%a IN ("%src_folder%") do (
echo RENAME %%a
echo COPY %%a
)
Note: keep in mind, you may need to use delayed expansion (not in this example though)
Applied to your code:
#echo off
setlocal enabledelayedexpansion
SET src_folder="C:\DIR_A\"
SET tar_folder="C:\DIR_A\DIR_B"
for %%a IN ("%src_folder%*.xml") do (
REN "%%a" !time:~0,2!!time:~3,2!!time:~6,2!_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml
move "%src_folder%\%%a" "%tar_folder%"
timeout 1 >nul
)
Note: here you need delayed expansion (at least) with the time variable. Consider to delay date too (would be "best practice")
Alternatively: first rename all files, then copy them in one go:
#echo off
setlocal enabledelayedexpansion
SET src_folder="C:\DIR_A\"
SET tar_folder="C:\DIR_A\DIR_B"
for %%a IN ("%src_folder%*.xml") do (
REN "%%a" !time:~0,2!!time:~3,2!!time:~6,2!_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml
timeout 1 >nul
)
move "%src_folder%\*.xml" "%tar_folder%\"
Simplest is to do it in one go, without rename, just move them with a new name:
#echo off
for %%a IN (*.xml) do (
move "%source_folder%\%%a" "%tar_folder%\%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml"
)
pause
Other methods:
#echo off
setlocal enabledelayedexpansion
for /f %%a IN ('dir /b /a-d "%src_folder%\*.xml"') do (
set "myren=%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml"
ren "%%a" "!myren!"
move "%src_folder%\!myren!" "%tar_folder%"
)
pause
Or without delayedexpansion:
#echo off
for /f %%a IN ('dir /b /a-d "%src_folder%\*.xml"') do (
move "%%a" "%tar_folder%"
ren "%tar_folder%\%%a" "%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml"
)
pause
or even:
#echo off
for /f %%a IN ('dir /b /a-d "%src_folder%\*.xml"') do (
ren "%source_folder%\%%a" "%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml"
)
move /Y "%src_folder%\*.xml" "%tar_folder%"
pause
Simplest, do it in one go:

rename files in batch script

I have folder with files named:
-backup- powerpoint1.ppt
-backup- powerpoint2.ppt
-backup- powerpoint3.ppt
I need to rename those files in the same folder to:
powerpoint1.ppt
powerpoint2.ppt
powerpoint3.ppt
I need a batch script to accomplish that.
I have searched online and came up with this so far but the values are resolving properly:
#echo off
setlocal EnableDelayedExpansion
set a=1
for /f "delims=" %%i in ('dir /b *.pptx') do (
set "dt=%%i"
echo.!dt!
set sh=!dt!
echo.!sh!
set "str=%sh:~12,4%"
echo "%%i" "%str%"
set /a a+=1
)
You code does not work as expected, because you missed applying delayed expansion for the interim variables sh and str. Also the extracted string portion !sh:~12,4! is not correct, it should read !sh:~9! in order to remove the -backup- prefix. This is the corrected code:
#echo off
setlocal EnableDelayedExpansion
set a=1
for /f "delims=" %%i in ('dir /b *.pptx') do (
set "dt=%%i"
echo/!dt!
set sh=!dt!
echo/!sh!
set "str=!sh:~9!"
ECHO ren "%%i" "!str!"
set /a a+=1
)
Actually I would not extract certain character positions, but I would remove the constant prefix -backup- by sub-string replacement. And you do not need the interim variables sh and str, neither do you need the counter a. So I would do it like this:
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('dir /b *.pptx') do (
set "dt=%%i"
set "dt=!dt:*-backup- =!"
ECHO ren "%%i" "!dt!"
)
endlocal
After having verified the correct output, remove the upper-case ECHO command from the code to actually rename files.
If the unwanted parts in your file names are space separated a simple:
#Echo off
For /F "delims=" %%A in ('Dir /B/A-D "-backup- powerpoint*"'
) Do For /f "tokens=2" %%B in ("%%A") Do Echo Ren "%%A" "%%B"
Should do (independent of the extension).
Ren "-backup- powerpoint1.ppt" "powerpoint1.ppt"
Ren "-backup- powerpoint2.ppt" "powerpoint2.ppt"
Ren "-backup- powerpoint3.ppt" "powerpoint3.ppt"

How to generate automatic numbering of output files in a batch file?

I have a bunch of files say,
xxx111.txt
xxx112.txt
xxx113.txt
I want to remove the last 3 characters of all the file names and I'm using this script
#echo off
setlocal enabledelayedexpansion
set X=3
for %%f in (*) do if %%f neq %~nx0 (
set "filename=%%~nf"
set "filename=!filename:~,-%X%!"
ren "%%f" "!filename!%%~xf"
)
popd
pause
This runs perfectly when the output filenames are different. However, in the above case all file will be output as xxx.txt and the script throws me the error
"A duplicate file name exists, or the file cannot be found".
Is there any way to tweak this so that duplicate files will be renamed and maybe numbered 1,2,3...?
Unfortunately I cannot install any other software.
#echo off
setlocal EnableDelayedExpansion
set X=3
for /F "delims=" %%f in ('dir /A:-D /B') do if "%%f" neq "%~NX0" (
set "filename=%%~Nf"
set "filename=!filename:~,-%X%!"
if exist "!filename!%%~Xf" call :getNewName "%%~Xf"
ren "%%f" "!filename!%%~Xf"
)
popd
pause
goto :EOF
:getNewName ext
set i=0
:nextNum
set /A i+=1
if exist "%filename%%i%%~1" goto nextNum
set "filename=%filename%%i%"
exit /B
You should not use plain for %%f command when renaming files. Depending on where the new names are placed in the list of original names, they may be processed a second time by the for %%f. Always use for /F for renaming.

Not looping through all the files

Windows batch script:
I have three files in a drectory.I`m trying to loop through the multiple files located in the directory and rename the files,but some how its not looping through.
i can see that the var1 is getting the right file name but not for sub1.This in turn renames the other two files in the directory with wrong output. Could some one please help me on this.
#echo on & setlocal EnableDelayedExpansion
set a=9
for /f "tokens=*" %%i in ('dir /b "C:\XX\YY\ZZ*"') do (
set var1=%%i
SET sub1=%var1:~7,22%
ECHO %sub1%
ren "%%i" "ABC!sub1!_!a!.dat"
set /a a+=1
)
#echo on
setlocal EnableDelayedExpansion
set a=10001
for /f "tokens=*" %%i in ('dir /b "C:\TEST\PBM\PAR*"') do (
set var1=%%i
SET sub1=!var1:~7,22!
ECHO !sub1!
ECHO ren "%%i" "ABC!sub1!_!a!.dat"
set /a a+=1
)
With delayedexpansion any reference to the value of a variable that is changed within the loop must use the !var! syntax. %var% will access the parse-time value.
Note: REN is now echoed for verification.

How to rename multiple files using a windows batch file script

I have and will have files which are named "x_1.txt x_2.txt x_3.txt, ..." my other program where I input these files cannot recognize the order so it sorts like this "x_1.txt , x_101.txt , x_2.txt"). a solution is to rename the files to x00001.txt , x00002.txt , ....
I have so far wrote the .bat file below, but two problems I have which , I'd be very glad if you help me solve them :
1- how can I remove the 'number'.txt from string x_'number'.txt
2- (solved) how can I use the variable of this string to rename the file name ( the rename part of this file is not working!)
cls
setlocal enabledelayedexpansion
set /A count=100000
for %%f in (*.txt) do (
set /a count+=1
set str=!count:~1!
echo !str!
echo %%f
set filename=%%f
set filename=!filename:~0,5! /Comment: here I want to just keep the x_ part which I don't know how"
echo !filename!
set str3=!filname!!str!
echo !str3!
/// ren %%f !str3!.txt /Comment: Here I cannot use the variable str3,
call:renamer %%f !str3!
)
:renamer
ren %1 %2.txt
Thanks in advance
If the following conditions are true:
You want to rename all of your .txt files in the current folder
All of the .txt files have exactly one _ in the name, immediately before the number
None of your file names contain !
Then the following will work
#echo off
setlocal enableDelayedExpansion
for %%F in (*.txt) do for /f "tokens=1,2 delims=_." %%A in ("%%F") do (
set num=0000%%B
ren "%%F" "%%A!num:~-5!.txt"
)
But to eliminate the conditions requires much more complicated code.
Here is one robust solution that should properly rename all files that meet the template.
It allows for multiple _ in the name.
It only renames files with a name that ends with _NNN.txt where NNN is a number
It properly handles ! in the file name.
Note that it will not properly handle numbers that exceeds 99999. It is simple to expand the degree of 0 padding.
#echo off
setlocal disableDelayedExpansion
pushd .
subst #: .
#:
for /f "eol=: delims=" %%F in ('dir /b /a-d *.txt^|findstr /er "_[0-9]*.txt"') do (
set "name=%%~nF"
setlocal enableDelayedExpansion
for /f "eol=: delims=" %%A in ("!name:_=\x!") do (
endlocal
set "file=%%F"
set "name=%%~pA"
set "num=%%~nA"
setlocal enableDelayedExpansion
set "num=0000!num:~1!"
set "name=!name:~1,-1!"
ren "!file!" "!name:\x=_!!num:~-5!.txt"
endlocal
)
)
popd
subst /d #:

Resources