DOS Batch Rename Files - batch-file

I have a large set files with names structured string_int_int_int_string.extension, and would like to batch rename them with left zero padding to 7 digits on the second int.
Example: rename stringA_1_2_3_stringB.jpg to stringA_1_0000002_3_stringB.jpg.
I've seen some helpful posts here, here, and here but haven't quite done it.
Here is what I have so far (not working, of course):
dir /b *.* >temp.txt
for /f "tokens=%%1,%%2,%%3,%%4,%%5 delims=_" %x in (temp.txt) do (
setlocal enabledelayedexpansion
set PAD=000000%%k
set PAD=!PAD:~7!
ren "%%i_%%j_%%k_%%l_%%m" %%i_%%j_%PAD%_%%l_%%m
)
I specifically want to do this with a batch file, not some other language or tool. (I'm aware of the various renaming tools out there.)
Any help is most welcome!

setlocal EnableDelayedExpansion
dir /b *.* >temp.txt
for /F "tokens=1-5 delims=_" %%a in (temp.txt) do (
set PAD=000000%%c
set PAD=!PAD:~-7!
ren "%%a_%%b_%%c_%%d_%%e" "%%a_%%b_!PAD!_%%d_%%e"
)

I use FINDSTR to filter out file names that don't match the specified pattern. A total of 4 tokens are needed - the first 3, followed by the rest of the file name.
#echo off
setlocal disableDelayedExpansion
for /f "tokens=1,2,3* delims=_" %%A in (
'dir /b /a-d * ^|findstr /r "^[^_]*_[0-9]*_[0-9]*_[0-9]*_[^_]"'
) do (
set "mid=%%C"
set "pad=0000000%%C"
set "start=%%A_%%B"
set "end=%%D"
setlocal enableDelayedExpansion
echo ren "!start!_!mid!_!end!" "!start!_!pad:~-7!_!end!"
endlocal
)

Related

Rename file in batch with index retain in Windows

I have a batch of file with the following format
YYYYYYYYYY(X).txt
YYYYYYYYYY(XX).txt
YYYYYYYYYY(XXX).txt
YYYYYYYYYY(XXXX).txt
Y are simplified Chinese character/punctuation with no fixed length, i.e. the no. of Y ranged from a few to more than 10
And X are number (0-9)
I wanted to rename all the file and delete all the Y in front. What command can I use with cmd?
[untested]
for /f "delims=" %%b in ('dir /b /a-d "d:\the path\*(*).txt" ') do (
for /f "tokens=2delims=()" %%c in ("%%b") do ren "d:\the path\%%b" "%%c%%~xb")
Here's some quick untested example code, which should rename, YYYYYYYYYY(X).txt to (X).txt, YYYYYYYYYY(XX).txt to (XX).txt, YYYYYYYYYY(XXX).txt to (XXX).txt, and YYYYYYYYYY(XXXX).txt to (XXXX).txt, subject to matching names not already existing, and which is exactly what your question asks.
#For %%G In ("C:\Users\Gary\Documents\?*(*).txt") Do #(
Set "}=%%~nG" & SetLocal EnableDelayedExpansion
Ren "%%G" "(!}:*(=!%%~xG" & EndLocal)
Here's some additional untested example code, which should rename, YYYYYYYYYY(X).txt to X.txt, YYYYYYYYYY(XX).txt to XX.txt, YYYYYYYYYY(XXX).txt to XXX.txt, and YYYYYYYYYY(XXXX).txt to XXXX.txt, subject to matching names not already existing.
For %%G In ("C:\Users\Gary\Documents\*(*).txt") Do (
Set "f=%%~nG"
SetLocal EnableDelayedExpansion
Set "n=!f:*(=!"
Ren "%%G" "!n:~,-1!%%~xG"
EndLocal
)

Batch file to rename file and add sequence number to the file name

I picked up the code below from another post. I believe it should pick up the current directory folder and include it in the renaming part of the process, however that doesn't seem to work for me.
#ECHO OFF
setlocal enabledelayedexpansion
PUSHD "%~1"
set inc=0
FOR /f "delims=" %%a in ('dir /b /a-d') DO (
set /a inc+=1
Echo Ren: "%%a" "%~n1!inc!%%~xa"
Ren "%%a" "%~n1!inc!%%~xa"
)
POPD
I have a .txt file that will be received into a folder each day named and time stamped. Example as below:
FileNameA_20170418153000.txt
Essentially I'd like to amend the code above to rename the file: filenam0001.txt and continue to update the sequence number (which works perfectly well).
i.e.
filenam0001.txt
filenam0002.txt
filenam0003.txt
Any help would be greatly appreciated.
you need to add the leading zeros manually (add some zeros, then cut the last x characters):
#echo off
setlocal EnableDelayedExpansion
set inc=0
for /l %%a in (1,1,50) do (
set /a inc+=1
set num=00000000!inc!
set num=!num:~-5!
echo !num!
)
I modified your code in order to insert the leading zeros in a simple way...
#ECHO OFF
setlocal enabledelayedexpansion
PUSHD "%~1"
set inc=10000
FOR /f "delims=" %%a in ('dir /b /a-d') DO (
set /a inc+=1
Echo Ren: "%%a" "%~n1_%%~na!inc:~1!%%~xa"
Ren "%%a" "%~n1_%%~na!inc:~1!%%~xa"
)
POPD

Batch file to rename text files within directory with text from within text file at specific line except if line blank

#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
cd "C:\Documents and Settings\John\Desktop\New\Interest\f2"
Pushd "C:\Documents and Settings\John\Desktop\New\Interest\f2"
Set Line#=26
Set /A LOfs=24 -1, Len=34 - LOfs
For %%A in (*.txt) do For /F "tokens=1* delims=:" %%B in (
'Findstr /N ".*" "%%A" ^|Findstr "^%Line#%:"'
) do if %errorlevel% == 0 Set "Line=%%C"&Ren "%%~fA" "!Line:~%LOfs%,%Len%! - %%A!""
Popd
In the above I am trying to change the filename of files in a directory with text in it at a certain position.
If line 26 is blank do nothing and do not change filename.
I have gone wrong somewhere and am going round in circles.
Can anyone help?
Thank you.
You don't state how your script fails, but I can see some potential problems. I also see possible simplifications.
You certainly don't need both CD and PUSHD
I got rid of the numeric variables and included the number literals in the actual code. You can revert back to variables if you want.
You don't need the outer FOR loop. FINDSTR can search multiple files when using wildcards in the file name, and then it will include the filename, followed by : in the output. So if you add the /N option, output will have the form filename:line#:text. You can then adjust the 2nd FINDSTR to return only the correct line numbers.
It is not enough to ignore blank lines. Your rename only works if there is at least one valid file name character after the 23rd character. Filenames cannot include :, *, ?, /, \, <, >, or |. (I may have missed some). I adjusted the FOR /F delims and the FINDSTR search to compensate.
FOR variable expansion like %%A will corrupt values if they contain ! and delayed expansion is enabled. ! is a valid character in file names. So the delayed expansion must be toggled on and off within the loop.
I believe the following will do what you want. The code below will simply echo the rename commands. Remove the ECHO before the ren once it gives the correct results.
#echo off
setlocal disableDelayedExpansion
pushd "C:\Documents and Settings\John\Desktop\New\Interest\f2"
for /f "tokens=1,3 delims=:*?\/<>|" %%A in (
'findstr /n "^" "*.txt" ^| findstr "^[^:]*:26:.......................[^:*?\\/<>|]"'
) do (
set "old=%%A"
set "line=%%B"
setlocal enableDelayedExpansion
ECHO ren "!old!" "!line:~23,11! - !old!"
endlocal
)
popd
An slightly different method to Daves:
#Echo Off
Set "SrcDir=%UserProfile%\Desktop\New\Interest\f2"
Set "Mask=*.txt"
Set "Line#=26"
Set "LOfs=23"
Set "Len=11"
If /I Not "%CD%"=="%SrcDir%" Pushd "%SrcDir%"2>Nul&&(Set _=T)||Exit/B
For /F "Tokens=1-2* Delims=:" %%A In ('FindStr/N "^" "%Mask%" 2^>Nul'
) Do If "%%B"=="%Line#%" If Not "%%~C"=="" (Set "Line=%%C"
SetLocal EnableDelayedExpansion
If Not "!Line:~%LOfs%,%Len%!"=="" (
If Not Exist "!Line:~%LOfs%,%Len%! - %%A" (
Ren "%%A" "!Line:~%LOfs%,%Len%! - %%A"))
EndLocal)
If "_"=="T" PopD
This method don't require findstr.exe nor toggle setlocal/endlocal, so it should run faster. Also, it avoids to re-process any already renamed file changing the plain for %%A by a for /F combined with dir command.
#Echo off
SetLocal EnableDelayedExpansion
cd "C:\Documents and Settings\John\Desktop\New\Interest\f2"
Set /A Line#=26, LOfs=24 -1, Len=34 - LOfs
For /F "delims=" %%A in ('dir /A-D /B *.txt') do (
rem Read the desired line from this file
(for /L %%i in (1,1,%Line#%) do set "Line=" & set /P "Line=") < "%%A"
if defined Line ECHO Ren "%%~fA" "!Line:~%LOfs%,%Len%! - %%A"
)
Note also that when this Batch file ends the current directory is automatically recovered to the current one when setlocal command was executed, so pushd/popd commands are not needed either.

batch script how to create a string out of different variable parts

in my company we create software for different customers to handle our machines. As each product is unique, so is the control software, but not completely new. So for a start we copy an old project, rename it and change it until it fits.
Usually the directory name is the name for the new program (our ide uses the directory name, but also relies on some other files following the same name scheme).
For the renaming I've wrote a short batch script which finds the old name scheme and retrieves from the directory name the new one.
But the only solution I've found for this uses a new batchfile for each file to be renamed.
Is there a better way to get the content of !progNeu! ?
#echo off
SETLOCAL enabledelayedexpansion
set pfad=%CD%
for /d %%A in (%pfad%) do (set progNeu=%%~nxA)
for /f "tokens=1,2 delims=|" %%B in ('dir /b *.s19 ^| findstr /v "appl"') > do (
set progAlt=%%B
set rumpfAlt=!progAlt:.s19=!
>x ECHO !rumpfAlt!&FOR %%C IN (x) DO SET /A strlength=%%~zC - 2&del x
for %%D in (!rumpfAlt!*.*) do (
set progAlt=%%D
>x.bat echo #echo off
>>x.bat echo set ausg=!progAlt!
>>x.bat echo echo %%ausg:~!strlength!%%
for /f "" %%E in ('x.bat') do (
set "dateiNeu=!progNeu!%%E"
if exist !dateiNeu! del !dateiNeu!
rename %%D !dateiNeu!
)
del x.bat
)
)
If I have not missed something, this could be the equivalent to your code
#echo off
setlocal enableextensions disabledelayedexpansion
set "pfad=%CD%"
for /d %%A in ("%pfad%") do (
for /f "delims=" %%B in ('
dir /b *.s19 ^| findstr /v "appl"
') do for %%D in ("%%~nB*.*") do (
set "progAlt=%%D"
setlocal enabledelayedexpansion
for %%E in ("!progAlt:%%~nB=!") do (
endlocal
echo move /y "%%D" "%%~nxA%%~E"
)
)
)
I have removed almost all the inner variables that are simply using the values that the for replaceable parameters already hold, and used the adecuated modifiers to retrieve the needed part from the file names.

Batch only show directorys and their subdirectorys

I am having an question
How can i loop through an folder and only show folders and their subfolders without the full path.
example
if i use dir /b /s /ad
i will see e:\Mainfolder\Folder1\Subfolder
And i only whant to see
Folder1\Subfolder
the reason i want it is so i can put it inside an txt field and then with another loop check if the folder/subfolder exist somewhere else if not then it needs to create it.
With kind regards,
Thomas de Vries
#echo off
setlocal enableextensions disabledelayedexpansion
set "startingFolder=%cd%"
:: Determine the length of the starting path to remove
:: it from output
for /d %%a in ("%startingFolder%\"
) do for /f "skip=1 tokens=1 delims=:" %%b in (
'(echo(%%~fa^&echo(^)^|findstr /o "^"'
) do set /a "cutPoint=%%b-3"
:: Recurse folders from starting point and echo the
:: full path without the starting folder
for /r "%startingFolder%" /d %%a in (*) do (
set "line=%%a"
setlocal enabledelayedexpansion
echo(!line:~%cutPoint%!
endlocal
)
This will recurse over the tree structure, starting at the indicated folder (change startingFolder variable to what you need). For each folder found its relative path is echoed to console. Redirect output of batch to generate the required .txt file
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir /b /s /ad') do (
set "name=%%a"
echo !name:*e:\Mainfolder\=!
)

Resources