Windows bat file to rename multiple files with custom names - batch-file

I have multiple folders with multiple files in each of them. The file names are as following: static-string-1.wav static-string-2.wav .... static-string-10.wav ... static-string-99.wav static-string-100.wav ... and so on. The static string remains same but the number before .wav changes from 1 - 999. Now the problem is if I want to play this on any media player or want to join all the files, windows will sort like 1,10,100,2,20,200 not 1,2,3,4,5,6,7,8,9,10 which messes up the playback. So to fix this, I have to rename each file from static-string-1.wav to static-string-0001.wav and so on.
Currently, I am doing a dir command to an output file and then I copy the file list in excel from where I do some playing around with concatenate and text to columns and come up with two columns of old name and new name which I then again convert to a .bat file with and run it. The bat file has multiple rows with individual rename commands something like this:
#echo off
rename <oldname1> <newname0001>
rename <oldname2> <newname0002>
.
..
exit
It is getting the work done but I know there is and easier and more efficient way to use for loops. I saw few example and previous answers but they dont have a similar requirement as me. Any help will be appreciated.

Leading zeros can be added and (later) truncated if not needed. This question is a possible duplicate, but I don't like how files are sorted like that either.
Delaying the expansion allows b to change within the for loop instead of being static (haha puns) throughout the whole program. Therefore you can increment b each loop and rename the files. This is a simple example:
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /l %%a in (1,1,99) do (
set b=00%%a
rename static-string-%%a.wav static-string-!b:~-2!.wav
)
This should work. Contact me if you need more help

Below is a significant improvement to Clayton's answer
Only numbers less than 100 need be modified
The script automatically works with any static prefix. See How does the Windows RENAME command interpret wildcards? for an explanation of how this works.
The script reports any file names that could not be renamed
#echo off
setlocal enableDelayedExpansion
for /l %%N in (1 1 99) do (
set "n=00%%N"
for %%F in (*-%%N.wav) do ren "%%F" *-!n:~-3!.* || >&2 echo ERROR: Unable to rename "%%F"
)
Or, you could grab my JREPL.BAT regular expression renaming utility and simply use:
jren "-(\d{1,2})(?=\.wav$)" "'-'+lpad($1,'000')" /j

Related

Batch - Rename multiple files to sequential numbers

I need to rename multiple files at once. Lets say I have a these files:
episode1.mkv
e1.mkv
s01e01.mkv
As you see, the file names have nothing in common.
How can I change the names of all the files to numbers (1.mkv - 2.mkv - 3.mkv ...) using batch.
I want the first file to be renamed to 1.mkv (no letters or spaces or anything else in the name) the second file to be renamed to 2.mkv, and so on.
I've looked around the internet a lot and I still didn't find anything that does exactly this.
TIA
Batch file version
#echo off
setlocal ENABLEDELAYEDEXPANSION
set/a fileNum = 1
for %%f in (*.mp4) do (
ren %%~nf%%~xf !fileNum!%%~xf
set/a fileNum += 1
)
GUI version
FreeCommander can do this for you.
Run FreeComander
Navigate to the folder.
Select all the files you want to rename.
Hit F2
The rename dialogue will appear. To rename all files names to be numeric do this:
Click Rename to make the change.
I suppose you could make a FOR /L loop with a limit of the amount of files in your directory, feed it a DIR /B |FINDSTR .mkv line by line renaming each file to an index variable you keep. Since you are going to name them into just numbers your DIR command will spit the same movie back as the first item over and over so maybe write the output of DIR /B|FINDSTR .mkv to a text file and work off of that? Or move the file you rename to another folder?
Once you make a rough project we could help you refine it? Or use FreeCommander and make a batch to do the rest you want to do after that.

Windows command line renaming files in subfolders?

I didn't find so far a similar question, so I try to explain the problem:
I have large number of files that are in subfolders inside "C:\images"
I have a list of names in two columns, so that 1st column is old filename and 2nd is a new filename. I want to change that list into a batch file.
Names are pretty unique so I want to make batch file - so that will be one command for every file to be renamed.
RENAME "C:\images\768e11ab.jpg" "4ca5d042.jpg"
RENAME "C:\images\5402c708.jpg" "b802820b.jpg"
RENAME "C:\images\1c039e0e.jpg" "80ce9797.jpg"
etc...
It is rather simple, only, files are scattered across subfolders. Is there any way to make a command so it will look for that specific file in all subfolders in "C:\images" and rename it.
Following some similar questions tried this, with no result:
for /r "C:\images\" "%%~G" (768e11ab.jpg) do "4ca5d042.jpg"
Also, tried to use some renaming application for this but they freeze when I try to rename big list of files, so I would avoid them and use batch file. Also, I would like to use this way where there is one line in batch file for every file because it is simpler for me to use it (and change it later). I appreciate your help.
Approach the problem from the other side. Instead of looping over the image files, loop over the text file.
Assuming your textfile to be something like
"768e11ab.jpg" "4ca5d042.jpg"
"5402c708.jpg" "b802820b.jpg"
"1c039e0e.jpg" "80ce9797.jpg"
Then the code could look like:
#echo off
REM read each line; %%A is the first column, %%B is the second one
for /f "tokens=1,2" %%A in (list.txt) do (
REM search the image file %%A
for /R "C:\Images\" %%C in ("%%~A") do (
REM %%C now holds the full path of %%A
ECHO ren "%%~C" "%%B~%%~xC"
)
)
If your list looks different, the tokens and perhaps the delims for the for /f loop have to be adapted.
NOTE: the ren command is just echoed for security reasons. Once you verified it does exactly what you want, remove the ECHO to enable the * ren` command.

How to generate multiple files similar to a file kept in one particular location using batch script

I want to generate multiple files similar to the one which I have kept it in some particular location.
For example the file name is "MM_MTL_IM_20180726123109", only last two digits of the file name should keep on changing.
Can we achieve this with batch script or please suggest me any other technique
Here is a basic example script which is designed not for efficiency or to be the best method but to show some elementary batch file commands with their syntax in a logical progression.
#Echo Off
Set "FullFileName=MM_MTL_IM_20180726123109.ext"
If Not Exist "%FullFileName%" Exit /B
For %%A In ("%FullFileName%") Do (
Set "FileNameOnly=%%~nA"
Set "FileExtension=%%~xA"
)
Set "FileNameBarLastTwoChars=%FileNameOnly:~,-2%"
Set "LastTwoFileNameChars=%FileNameOnly:~-2%"
For /L %%A In (100,1,159) Do (
Set "Seconds=%%A"
If Not "%%A"=="1%LastTwoFileNameChars%" (
Call Copy "%FullFileName%" "%FileNameBarLastTwoChars%%%Seconds:~-2%%%FileExtension%"
)
)
Pause
You only requested 'multiple' files with no explanation, so you will need to make adjustments to the code if you don't wish this script to copy your file 59 times so that you have sixty matching files with different names in the same directory, each having valid seconds.Your question also states 'generate' not copy, so you will have to adjust the code if the copy assumption was incorrect.
#echo off
setlocal
set "name=%~1"
set "stem=%name:~,-2%"
for /L %%i in (10,1,59) do copy %name% %stem%%%i
This batchfile takes the original filename as parameter and creates 49 copies of it, appending "10" to "59" instead of the last 2 characters.
In this simple form this will work only if original file and copies are in the same, the current, directory.

Batch file to copy files based on part of filename from one server to another

I'm trying to create a batch file (via Windows XP Pro) that copies 2 files (.zpl) who's filename's vary in length. ZPL files relate to label printer code. File names are as follows:
FillXferDataPBHAMFill###########.zpl
FillFormatsPBHAMFill############.zpl
The pound signs represent a number associated with a particular label/job to be printed. These numbers are identical per job. From one job to the next the numbers vary in length and always change. The directory I'm trying to pull these from contains ZPL files from multiple locations, however, I just want the BHAM ones.
The batch will copy from: \Server\C:\Directory1\Directory2\Directory3
To be copied to: \Server\Directory1\Directory2
Not sure if this will complicate things further, but the batch file will be ran from a 3rd machine. Furthermore, I do not need to copy every file everytime. Whenever new print jobs are sent, supervisors will run the batch to copy the new print jobs within the last X amount of time. X being minutes. Here is what I have so far...
#echo off
SETLOCAL enableExtensions enableDelayedExpansion
SET sourceDir=Server\C:\Directory1\Directory2\Directory3
SET targetDir=Server\Directory1\Directory2
FOR %%a (FillFormatsPBHAM*.bat) DO (
SET "filename=%%a"
SET "folder=%targetDir%"
XCOPY "%%a" !folder!
)
FOR %%b (FillXferDataPBHAM*.bat) DO (
SET "filename=%%b"
SET "folder=%targetDir%"
XCOPY "%%b" !folder!
)
:END
I apologize for a lengthy post; just wanting to be as thorough as possible. I'm learning this on the fly so bare with any ignorance on my part. Thank you in advance for ANY help!!
StackOverFlow Material Reviewed: Reference1, Reference2 -- I've been looking everywhere over the past week and these were the 2 most helpful so far.
I see some ways to fix or improve your BAT script.
The FOR command syntax is FOR %%a IN (*.bat) DO (
The sourcedir variable is set Server\C:\Directory1\Directory2\Directory3, which is not a correct path in Windows.
you initialize but don't use %sourcedir% variabe neither in your FOR loop nor in your copy commnand
you should either change the current drive and dir with a pushd %sourcedir% command, or specifying it in the FOR command.
your FOR loop assigns a %filename% variable that is never used, you may skip this assignment.
you FOR loop assigns a %folder% variable that is only then used in the copy command, you can skip this assignment and simply use %targetdir%
but, to just copy all files from one folder to the other you don't need FOR to iterate over all of them, you might just copy them right.
So, take a look at this simple script to get you started..
SET sourceDir=\\servername\sharename\Directory1\Directory2\Directory3
SET targetDir=\\anotherserver\sharename\Directory1\Directory2
xcopy %sourceDir%\FillFormatsPBHAM*.bat %targetDir%
xcopy %sourceDir%\FillXferDataPBHAM*.bat %targetDir%

dos batch file find missing sequential filenames

Here is the situation, I have directories that have 10,000 files in each directory named like this:
FileName.XX0000.csv
FileName.XX9999.csv
The Size of the filename itself can change ie:
FileName.XXX0000.csv
FileName.XXX9999.csv
FileName.XXXX0000.csv
FileName.XXXX9999.csv
Where the X's are yet another parent number, but these ones should be ignored anyway.
Regardless though all directories are supposed to have exactly 10,000 files no more and no less. The trouble is some are missing and I need to know which ones are missing.
I tried the scripts in this post:
batch script to find missing sequence no
I even messed around with the string sequnce for several hours to no avail. Neither of these options works very well the 2nd script comes close but ends up in an endless loop, or spits out files that do exist.
I need to come up with a solution that will easily tell me which numbers or file names are missing. In my Dream world I would love a script where I can place it inside a parent directory and it would crawl through all the sub directories and print a single file of the missing ones.
It would be nice to not have to tell the script what the beginning part of the file name is, since in theory it should only be looking to see if the last four numbers are in existance from 0000-9999.
Since this is my first post I am hoping I doing this correctly by creating a new topic, I was going to ADD my question to the previous topic until I saw the "But avoid …" in the post box!
Let's see if you can write some code.
FOR /L can count from 0 to 9999
IF NOT EXIST can test if a file is missing, and it supports wild cards.
> and >> can redirect output to a file
For your dream world:
FOR /D /R %%F IN (.) will iterate an entire folder hierarchy
Edit
You probably will need to left pad a number with zeros so that the width is always 4 digits. Since this will probably be within a loop, you will want to use delayed expansion. Assume %%N is a FOR variable that contains a number.
set num=000%%N
set num=!num:~-4!
Here is a script that will find all the missing files in a continuous sequence:
#echo off
setlocal ENABLEDELAYEDEXPANSION
set max=1208
set cnt=0
for /L %%f in (0,1,%max%) do (
set "num=00000%%f"
set "num=!num:~-4!"
if not exist "img!num!.png" (
echo img!num!.png
set /A "cnt=!cnt!+1"
)
)
echo NUMBER MISSING: !cnt!
endlocal
Notice inside the loop you need to use !x! instead of %x% to get a local delayed variable.

Resources