I am looking to batch rename a large number of files by adding a suffix of an alpha character, but only in a specific range. For example: File 126.pdf should be renamed 126A.pdf, File 127.pdf should be renamed 127B.pdf, File 128.pdf should be renamed 128A.pdf, File 129.pdf should be renamed 129B.pdf, etc. Any existing software that is capable of doing this is also appreciated.
Thanks!
setlocal EnableDelayedExpansion
rem Define the set of replacements
for %%a in ("126=126A" "127=127B" "128=128A" "129=129B") do set %%~a
rem Achieve the replacements
for %%a in (*.pdf) do (
if defined %%~Na (
ren "%%a" "!%%~Na!.pdf"
)
)
Previous program is based on the examples given in your request. Perhaps if you define the rename rules in a different, more precise way, the program may be entirely different!
You could try the following:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET suffixes=AB
FOR %%I IN (*.pdf) DO (
RENAME "%%I" "%%~nI!suffixes:~0,1!.*"
SET suffixes=!suffixes:~1!!suffixes:~0,1!
)
The suffixes variable is set to an "array" of all the possible suffixes to use with renaming.
When iterating over the files, the first suffix in the array is picked to form a new name. Then the suffixes are swapped in the array, so the other one becomes first at the next iteration.
This solution will work with an arbitrary number of suffixes.
You could use a FOR loop to examine each single filename.
setlocal EnableDelayedExpansion
for %%A in (*.pdf) DO (
echo Filename=%%A
set name=%%~nA
set "prefix="
if !name! == 123 set prefix=A
if !name! == 999 set prefix=B
echo rename "%%A" to "!name!!prefix!.pdf"
ren "%%A" "!name!!prefix!.pdf"
)
Related
I have a custom service that automatically generates files every 60 mins into a particular directory with part of the filename incrementing numerically, Eg:
File_00004.job
File_00003.job
File_00002.job
File_00001.job
Currently I have an issue where on occasion a file isn't generated, which results in gaps in the file sequence. This issue then causes a number of issues if not identified ASAP.
I'd like a batch file to identify if I have a gap in the file name sequence.
Tried looking for solutions from existing posts, but haven't found something that fits, so apologies if this has been covered elsewhere.
#ECHO OFF
SETLOCAL enabledelayedexpansion
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
SET "mask=file_??????.job"
SET "lowest="
SET "highest="
FOR /f "delims=" %%a IN (
'dir /b /a-d /on "%sourcedir%\%mask%" '
) DO (
IF NOT DEFINED lowest SET "lowest=%%~na"
SET "highest=%%~na"
)
SET "lowest=%lowest:*_=1%"
SET "highest=%highest:*_=1%"
ECHO checking range %lowest:~1% to %highest:~1%
:: See whether an entry in the range is missing; report&create an empty file if so.
FOR /L %%a IN (%lowest%,1,%highest%) DO SET "name=%%a"&SET "name=file_!name:~1!.job"&IF NOT EXIST "%sourcedir%\!name!" echo !name! missing&(copy nul "%sourcedir%\!name!" >nul)
GOTO :EOF
Alternative structure for the for /L loop:
FOR /L %%a IN (%lowest%,1,%highest%) DO (
SET "name=%%a"
SET "name=file_!name:~1!.job"
IF NOT EXIST "%sourcedir%\!name!" (
echo !name! missing
copy nul "%sourcedir%\!name!" >nul
copy "d:\path to\template.file" "wherever\!name!" >nul
copy "d:\path to\template.file" "anotherplace\!name!" >nul
echo Batch is fun and powerful
copy "d:\path to\template.file" "a third place\!name!" >nul
)
)
The critical point is the positioning of the ( - must be directly after and on the same line as do or else or the logical comparison clause of if and must be matched by a ) (which doesn't need to be on its own line - I find it easier that way, to align indentation.) )s that are not intended to close a block need to be escaped with ^, thus: ^)
There are two folders holding equal amounts of files between them.
I'd like to apply the names from one set ; to the other set of files in no particular order. Inherit the name, but retain the extension .
Input files are .bmp Output files are .ini ( has a gear symbol ).
Example :
folder 1- Flowers.bmp ; folder 2- blank.ini
. To this:
folder 1- Flowers.bmp ; folder 2- Flowers.ini
There would be more files , but equal .
The .ini files are all copies . So they may have a generic name and numbered if that matters to know . Those would all receive one name each from the other .bmp files in the other folder.
Normally I have both folders situated on the Desktop .
I make sure both folders have equal number of files between them . That would be a constant .
I'm trying to stream line some menial repetitive daily tasks .
I did search and what I have found does not really help.
#ECHO OFF
SET "vers=%~1"
IF "%vers%" == "" SET /P "vers=Enter Vers: "
FOR %%F IN (file_XX_*.*) DO CALL :process "%%F"
GOTO :EOF
:process
SET "name=%~nx1"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "name=!name:_XX_=_%vers%_!"
RENAME %1 "%name%"
ENDLOCAL
Hoping to find a solution to this finally .
Ok, here is a slightly long version, but it makes sure it gets the content of each folder.
Note You must ensure that the path to both folders are correctly specified in the third and fourth line:
#echo off
setlocal enabledelayedexpansion
set "source=%userprofile%\Desktop\folder 1"
set "destination=%userprofile%\Desktop\folder 2"
set /a num=0
for %%a in ("%source%\*") do (
set /a num+=1
set "fr!num!m=%%~na"
)
set /a oldn=!num!
set /a num=0
for %%b in ("%destination%\*") do (
set /a num+=1
set "to!num!r!=%%~nxb"
set "ext=%%~xb"
)
pushd "%destination%"
for /l %%i in (1,1,!oldn!) do ren "!to%%ir!" "!fr%%im!%ext%"
popd
pause
You can remove pause at the bottom of the script, once you are happy with the output.
This is not the most effective way, but considering your limited batch experience, I guess an understandable approach is better than an optimized one (using array-like variables).
#echo off
setlocal enabledelayedexpansion
(for %%A in ("folder 1\*.bmp") do echo %%~nA)> "%temp%\names.txt"
<"%temp%\names.txt" (
for %%A in ("folder 2\*.ini") do (
set /p "name="
ECHO ren "%%A" "!name!.ini"
))
The first for loop creates a list of the desired names (from Folder 1). The modifier %%~nA returns the name only.
The second for processes each file in Folder 2 and takes a name from the previously generated file for each file. This depends on the fact that the number of files in both folders are the same, else you may get undesired results.
I disabled the actual ren command by just echoing it. Check the output and only when you are satisfied, remove the ECHO.
So I am using an application that creates a series of .tif files with very annoying filenames that do not sort correctly in alphanumerical order. I have no way to change the behavior of this commercial software, so I must use my own creativity to rename the files so they may be processed externally in the correct order. For example, the files created look like this:
image.tif, image(2).tif, image(3).tif, ..., image(9).tif, image(10).tif, ..., image(n).tif
Where n is an integer not less than 2. The first file has no number in parenthesis. Sorted alphanumerically, for example, the list would look like:
image.tif, image(10).tif, image(100).tif, image(1000), image(2).tif, ...
Obviously while this is the correct alphanumerical order, it's the incorrect numerical order. So with the aid of some batch commands, I created this:
#echo off & setlocal EnableDelayedExpansion
ren "image.tif" "a.tif"
ren "image(1).tif" "b.tif"
ren "image(2).tif" "c.tif"
ren "image(3).tif" "d.tif"
ren "image(4).tif" "e.tif"
ren "image(5).tif" "f.tif"
ren "image(6).tif" "g.tif"
ren "image(7).tif" "h.tif"
ren "image(8).tif" "i.tif"
ren "image(9).tif" "j.tif"
set a=1
for /f "delims=" %%i in ('dir /b *.tif') do (
ren "%%i" "!a!.tif"
set /a a+=1
)
This at least corrects the names of the first ten files, as I often process less than 100 files. The for loop is a snippet I found here on stackoverflow, it renames the alphanumeric files into integers starting with 1 up to the number of files.
Problem I run into is that this loop still creates 1.tif, 10.tif, and 100.tif that would come before 2.tif.
I've seen the use of %02d to format numbers into 01, 02, 10, etc. Does anyone know how to syntactically include that in the loop shown above?
Or if anyone can figure a more elegant method to rename these files into a suitable alphanumeric order, it would be greatly appreciated. I have the feeling my solution is very kludgey.
#echo off
setlocal EnableDelayedExpansion
rem Initialize "i" variable with the desired number of digits as zeros
set i=1000
rem Process the files in creation date-time order
for /F "delims=" %%a in ('dir /A-D /OD /B *.tif') do (
rem Increment the number: "1001"...
set /A i+=1
rem Omit first digit in number: "1", use the rest: "001"...
ren "%%a" "image(!i:~1!).tif"
)
#echo off
setlocal EnableDelayedExpansion
set /a max=99&set /a numlen=2
if exist "image(100).tif" set /a max=999&set /a numlen=3
for /L %%a in (1,1,%max%) do if exist "image(%%a).tif" set /a newnum=1000+%%a&if not exist "image(!newnum:~-%numlen%!).tif" echo(ren "image(%%a).tif" "image(!newnum:~-%numlen%!).tif"
set /a max+=1
echo(ren "image.tif" "image(!newnum:~-%numlen%!).tif"
[untested]
If "image(100).tif" exists, then we need to rename to (001)..(999). If not, to (01)..(99), so then number-length will be 3 or 2.
for each of the names 1..max, if the file exists, calculate 1000+the file number;take the last 3 or 2 characters and rename. Only attempt to rename if the destination name does not exist, so (6) will be renamed to (006) or (06), (15) to (05) or skipped as (15)=(15) and (123) will also be skipped as (123)=(123)
Finally, rename the image.tif file to (000) or (00)
Note that this will simply echo the required commands to allow verification. Change the echo(ren to ren when verified to actually perform the rename.
The %02d convention for padding numbers is a printf thing, not really applicable to the Windows cmd environment or batch scripting. PowerShell has something similar, but not pure batch. However, you can zero-pad numerals pretty easily using variable substring manipulation.
set /a a=0
for /f "delims=" %%I in ('dir /b *.tif') do (
set "idx=00!a!"
ren "%%~I" "!idx:~-3!%%~xI"
set /a a += 1
)
If you'd prefer to keep the naming convention of image(idx).tif while ensuring the TIF files remain in their intended sequential order, a lazy solution might be to use prename.bat and rename with regexps.
prename /v "s/\((\d)\)/(00\1)/" *.tif
prename /v "s/\((\d\d)\)/(0\1)/" *.tif
In cmd there are no format strings like %02d. However, you can use dir /B to retrieve a plain list of files, findstr to filter for those having a parenthesised numeric suffix in their names or no such suffix at all, for /F to capture the resulting file names and split them into tokens relying on (, ), and sub-string expansion to do the actual left-zero-padding, like in the following example:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_LOCATION=." & rem // (directory containing the files to rename)
set "_PATTERN=*.tif" & rem // (search pattern for the files to rename)
set /A "_DIGITS=4" & rem // (number of proposed digits from 1 to 10)
set "_REGEX1=^[^()][^()]*([0-9][0-9]*)\.tif$" & rem // (filter expression)
set "_REGEX2=^[^()][^()]*\.tif$" & rem // (filter expression)
rem // (the filter expressions ensure wrongly named files not to be processed)
rem // Build zero-padding string:
set "PAD=0000000000" & call set "PAD=%%PAD:~-%_DIGITS%%%"
rem // Loop through all matching files:
for /F "tokens=1-3 delims=()" %%A in ('
dir /B /O:N "%_PATTERN%" ^| findstr /I /R /C:"%_REGEX1%" /C:"%_REGEX2%"
') do (
rem // Check if current file name contains a parenthesised numeric suffix:
if not "%%B"=="" (
rem // Numeric suffix found, so store file name parts and pad number:
set "LEFT=%%A" & set "MID=%PAD%%%B" & set "RIGHT=%%C"
setlocal EnableDelayedExpansion
ECHO rename "!LEFT!(%%B)!RIGHT!" "!LEFT!(!MID:~-%_DIGITS%!)!RIGHT!"
endlocal
) else (
rem // No numeric suffix encountered, so append zero-suffix:
ECHO rename "%%A" "%%~nA(%PAD%)%%~xA"
)
)
endlocal
exit /B
After having successfully verified the appropriate output of the script, remove the upper-case ECHO command to actually rename any files.
The script renames a file like image.tif to image(0000).tif (supposing there is no file named image(0).tif, originally). If you do not want that to happen, simply remove the /C:"%_REGEX2%" part from the findstr command line.
Happy Friday Think-Tank!
I need some assistance with a Batch .BAT script. Specifically I need help with some "IF statement syntax"
I have a script that is renaming files. There are two files, one ending in four digits and the other ending in five digits. The files will be renamed with variables I have already pre-set earlier within my script.
So here is a scenario: We have two files in a directory located at
c:\Users\username\Desktop\test-dir
There are two files within test-dir:
file1.12345
file2.1234
A four digit ending is one variable type (VAR1), whereas a file ending in five digits is another variable type (VAR2).
I need an if statement to:
a) read all the files(s) with the chosen directory (without using a wildcard if possible).
b) determine based on the number of digits after the "." which variable to use.
c) once making that determination rename the file with the appropriate variables.
The final re-naming convention is as so: yyyymmddtype.1234/12345
So basically it would use the datestamp variable I already created, the type variable I already created to be injected by the if statement, and append with the original ending digits of the file.
I know this seems like a lot, but I am more so a bash script guy. I have all the elements in place, I just need the if statement and what feels like a for loop of some kind to tie it all together.
Any help would be great!
Thank you!
Sorry, not the option you where asking for. Instead of iterating over the full list checking each file for extension conformance, iterate over a list of patterns that will filter file list, renaming matching files with the asociated "type"
for %%v will iterate over variable list, for %%a will split the content of the variable in pattern and type, for %%f will generate the file list, filter with findstr using the retrieved pattern and rename matching files with the corresponding "type"
Rename command is preceded with a echo to output commands to console. If the output is correct, remove the echo to rename the files.
#echo off
rem Variables defined elsewhere
set "folder=c:\somewhere"
set "timestamp=yyyymmdd"
rem Rename pattern variables in the form pattern;type
set "var1=\.....$;type1"
set "var2=\......$;type2"
set "var1=\.[^.][^.][^.][^.]$;type1"
set "var2=\.[^.][^.][^.][^.][^.]$;type2"
setlocal enableextensions disabledelayedexpansion
for %%v in ("%var1%" "%var2%") do for /f "tokens=1,* delims=;" %%a in ("%%~v") do (
for /f "tokens=*" %%f in ('dir /a-d /b "%folder%" ^| findstr /r /c:"%%~a"') do (
echo ren "%folder%\%%~f" "%timestamp%%%~b%%~xf"
)
)
endlocal
#ECHO OFF &SETLOCAL
set "yyyymmdd=yyyymmdd"
set "VAR1=VAR1"
set "VAR2=VAR2"
for /f "delims=" %%a in ('dir /b /a-d^|findstr /re ".*\....."') do echo(ren "%%~a" "%yyyymmdd%%VAR1%%%~xa"
for /f "delims=" %%a in ('dir /b /a-d^|findstr /re ".*\......"') do echo(ren "%%~a" "%yyyymmdd%%VAR2%%%~xa"
remove echo( to get it working.
If I understand you then this will rename the two files using preset variables for each one:
for %%a in ("%userprofile%\Desktop\test-dir\*") do (
if "%%~xa"==".12345" ren "%%a" "%variableA%-%variableB%%%~xa"
) else (
ren "%%a" "%variableC%-%variableD%%%~xa"
)
)
The code below works fine, here is a list of it's functions:
It moves files based on the fist 4 characters to a pre-created folder with the same first 4 characters
If the folder does not exist, it will not move the file, as there is no folder with the same fist 4 chars.
#echo on
setlocal enabledelayedexpansion
cls
pushd R:\Contracts\Sites
for /f "tokens=*" %%1 in ('dir /a-d /b *') do (
set filename=%%1&set dirname=!filename:~0,4!
for /f "tokens=*" %%A in ('dir /ad /b') do (
set dirid=%%A & set dirid=!dirid:~0,4!
if "!dirid!" equ "!dirname!" move %%1 %%A
)
)
I would like to add one extra function to this code please. Pleas have a look at the example below.
I have 5 files
X32A-test.docx or X32A-test.pptx (there will only be one docx or pptx, "NEVER two with the same name")
X32A-test.pdf
X32A-test.avi
X32A-test-eng.sub
X32A-test-small.jpg
I would like the code to CREATE a folder if it does not exist, based on the file name if it has the extension docx or pptx.
So with the above example it would create a folder named: "X32A-test". Then all the other files with "X32A" in the front of the name will be moved to that newly created folder "X32A-test".
I hope it is clear enough. If not please ask me for more information.
Thank you
It is much simpler and more efficient to use the simple FOR instead of FOR /F in your case.
And rather than looping through every file and moving them individually, it is easier and more efficient to use wildcards.
The first loop finds the .pptx and .docx files and creates folders as needed
The second loop finds all the directories and moves all files that start with the directory name into the directory.
#echo on
setlocal enableDelayedExpansion
cls
pushd R:\Contracts\Sites
for %%F in (*.docx *.pptx) do (
set "folder=%%F"
2>nul md !folder:~0,4!
)
for /d %%F in (*) do move %%F* %%F
popd
If needed, you can protect yourself against directory names shorter than length 4.
#echo on
setlocal enableDelayedExpansion
cls
pushd R:\Contracts\Sites
for %%F in (*.docx *.pptx) do (
set "folder=%%F"
set folder=!folder:~0,4!
if !folder:~0,3! neq !folder! 2>nul md !folder!
)
for /d %%F in (????) do (
set "folder=%%F"
if "!folder:~0,3!" neq "%%F" move %%F* %%F
)
popd
Note that this solution may fail if a file name contains !. If that arises then you need to toggle delayed expansion on and off within the loop(s).
I can see the entire process (including the part already implemented) like this:
All the files that are not yet in their "home" directories are moved there.
For all .docx and .pptx files left, create directories based on the files' names.
Obviously, the step #2 creates new "homes" and those will still be "uninhabited" this far. So all that is left to do now is to repeat the step #1.
So I would probably reorganised your process and, with the additional requirement, it could be implemented this way:
…
PUSHD your_root_directory
FOR /D %%D IN (*) DO (
CALL :movefiles "%%D"
)
FOR %%F in (*.docx *.pptx) DO (
MKDIR "%%~dpnF"
CALL :movefiles "%%~dpnF"
)
…
GOTO :EOF
:movefiles
SET "dirname=%~n1"
SET "mask=%dirname:~0,4%*"
MOVE "%~dp1%mask%" %1
Note: The steps #2 and #3 could be either implemented as separate loops or combined in one. The above script uses the latter approach.
You can use negative offsets in the !var:~offset,len! evaluation as follows:
set fspec=X32A-test.docx
echo !fspec:~-10!
echo !fspec:~0,-10!
That second line above gives you -test.docx and you can simply check that against your two desired possibilities with an if statement (or two).
Then you can use the third line to get the rest of the name for creating a directory.
The following example script shows how this could be done:
#setlocal enableextensions enabledelayedexpansion
#echo off
set fspec=X32A-test.docx
set bit1=!fspec:~-10!
set bit2=!fspec:~0,-10!
if .!bit1!.==.-test.docx. echo mkdir !bit2!
if .!bit1!.==.-test.pptx. echo mkdir !bit2!
endlocal
I'm echoing the mkdir command rather than executing it so you need to take out the echo. You'll also need to integrate the set and if statements into your loop but, based on what you have so far, you should have little trouble with that.
If, as you seem to indicate in a comment, the first four characters are the key and the last five decide on whether to make the directory, as in:
x32s-test.docx
a21w-production.pptx
xxxx-whatever_the_blazes_you_want.some_other_rubbish.docx
Then you're really only interested in the first four and last five:
#setlocal enableextensions enabledelayedexpansion
#echo off
set fspec=a12b-whatever_the_blazes_you_want.some_other_rubbish.docx
set bit1=!fspec:~-5!
set bit2=!fspec:~0,4!
if .!bit1!.==..docx. echo mkdir !bit2!
if .!bit1!.==..pptx. echo mkdir !bit2!
endlocal
This checks the correct extensions and outputs:
mkdir a12b
as expected.