Batch file to rename files by adding different suffix to each file - batch-file

I want to create a batch file that will rename files from a folder my adding a different suffix to each file
An example would be like this,
file1.mp4
file2.mp4
file3.mkv
file4.mkv
to these
file1 sandwich.mp4
file2 hot dog.mp4
file3 apple.mkv
file4 toast.mkv
I am hoping to put all these words in the batch file but putting it in a separate txt file would be more preferable
Note: I will put the same number of suffix in the txt file as there are files on the folder.
I just want a faster way of adding these suffix than doing it one by one manually
I have limited knowledge about these codes

The program below rename the files in the order given by dir command with the suffixes given in suffixes.txt file. If there are more files than suffixes, the last suffix will be used several times.
#echo off
setlocal EnableDelayedExpansion
< suffixes.txt (
for /F "delims=" %%a in ('dir /B folder\*.*') do (
set /P suffix=
ECHO ren "%%~Fa" "%%~Na !suffix!%%~Xa"
)
)
For example:
C:\> type suffixes.txt
sandwich
hot dog
apple
toast
C:\> test.bat
ren "file1.mp4" "file1 sandwich.mp4"
ren "file2.mp4" "file2 hot dog.mp4"
ren "file3.mkv" "file3 apple.mkv"
ren "file4.mkv" "file4 toast.mkv"
If the ren commands looks correct, remove the ECHO part in the last command in order to execute the ren commands.

Related

How to rename the folders with batch?

I have a file folder.txt file with a list of folder names on the HDD that have to be renamed with the names from gbv.txt. So the first line from folder.txt should be renamed with the first line from gbv.txt and so on. The folders are in the same directory from where the script will be started.
folder.txt
F-93-B-109
F-93-B-122
F-93-B-148
F-93-B-157
gbv.txt
GBV529357402
GBV52935795X
GBV529360799
GBV529362236
I'm slightly new to batch and don't know how to use rename in this situation in the loop
Essentially to read two files at a time you need to use a FOR /F command to read one file and stream the other file into the FOR /F command block. When you do this, you then can use the SET /P command to capture the line of text from the file being streamed into the command block.
#echo off
setlocal enabledelayedexpansion
< gbv.txt (FOR /F "delims=" %%G IN (folder.txt) DO (
set /p newname=
echo rename "%%G" "!newname!"
)
)
pause
The output on the screen will look like this.
C:\BatchFiles\SO>so.bat
rename "F-93-B-109" "GBV529357402"
rename "F-93-B-122" "GBV52935795X"
rename "F-93-B-148" "GBV529360799"
rename "F-93-B-157" "GBV529362236"
Press any key to continue . . .
When the output looks good, remove the word echo before the rename in the script.

How to change file names to name of directory that they're in?

I have a folder named "Projects". In that folder, I have multiple folders for my music projects, that have different names, and in each one of these I have an FL Studio project file (.flp), along with project's data, like Wave and MP3 files. But all of the FLP files have the default name ("untitled.flp"). Is there a way to change file names to names of directories that they're in?
I thought that I can make a batch or VBS file, but I'm not sure how should I program it.
Pseudo batch code:
:loop
for each "untitled.flp" in "D:\Google Drive\Projects" goto change
exit
:change
set variable="untitled.flp directory"
ren "untitled.flp" %variable%
goto loop
I'm running Windows 10.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /r "%sourcedir%" %%a IN (untitled.flp) DO IF EXIST "%%a" (
FOR %%s IN ("%%~dpa.") DO ECHO REN "%%a" "%%~nxs.flp"
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
The first for assigns the directoryname from the entire tree to %%a and appends the filename. The if gates out directorynames where the file is absent and the inner if constructs the full pathname of %%a and appends ., treating the entire resultant name as a filename; then the ren command renames the full filename of the file found to the name and extension part of the file's parent directoryname.

Comparing and changing, if needed, lines of an ini file, with a batch file

I need a batch file to compare two lines, in two different files.
FILE1:
CONFIG1(TRUE / FALSE)
CONFIG2(TRUE / FALSE)
CONFIGFOCUS,99999,
FILE2:
CONFIG2(TRUE / FLASE)
CONFIG2(TRUE / FALSE)
CONFIGFOCUS,999999,
So what i would like to do, is a batch file that would
check throught every INI files of many folders, and would compare
the line of FILE1 'CONFIGFOCUS' and compare it with the config of
FILE2 'CONFIGFOCUS'. And I would like it to check IF CONFIGFOCUS == '99999',
change it for '999999'. for every INI files in different folders. IS this doable?
If yes, could you please explain how? (i am a beginner in Batch file, and I would like
to understand how to do it)
To change CONFIGFOCUS,99999, to CONFIGFOCUS,999999, in a tree of files, this should work:
This uses a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
Put repl.bat in the same folder as the batch file, which is in the main folder of your tree of INI files.
#echo off
for /r %%a in (*.ini) do (
type "%%a" |repl.bat "CONFIGFOCUS,99999," "CONFIGFOCUS,999999," >"%%a.tmp"
move /y "%%a.tmp" "%%a" >nul
)
#echo off &setlocal
set "startfolder=x:\inifiles"
:: change 'CONFIGFOCUS' from '99999' to '999999'
for /d /r "%startfolder%" %%a in (*) do sed -i.bak "/CONFIGFOCUS/Is/99999/999999/" "%%~a\*.ini"
:: list 'CONFIG1', 'CONFIG2' and 'CONFIGFOCUS' from all files to compare
for /d /r "%startfolder%" %%a in (*) do findstr /bic:"CONFIG1" /c:"CONFIG2" /c:"CONFIGFOCUS" "%%~a\*.ini"
sed for Windows

I need a batch file to change one line of text in one directory to the same text file in a different directory

Basically, I have a text file with one line of text SetNumber=01 in 5 folders
C:\Documents and Settings\User\Desktop\Test\test.txt
C:\Folder\Test\test.txt
C:\Test\test.txt
etc.
I need to change this SetNumber=01 to different numbers monthly, for instance SetNumber=01 to SetNumber=02, in all these folders, and would like to run a batch file that would copy and replace this line of text from
C:\Documents and Settings\User\Desktop\Test\test.txt
into
C:Folder\Test\test.txt.
etc.
Any help would be greatly appreciated!!
OK, here we go. This copies the file C:\Documents and Settings\User\Desktop\Test\test.txt to all ..\test\test.txt files on the current volume (they were erased). Remove the echo command, if the output is OK:
#echo off&setlocal enabledelayedexpansion
set "sourcefile=C:\Documents and Settings\User\Desktop\Test\test.txt"
for /f "delims=" %%i in ('dir /s /b /a-d \test.txt') do (
set "fpath=%%~fi"
if "!fpath:*test\test.txt=!"=="" if not "%sourcefile%"=="%%~fi" (
echo copy "%sourcefile%" "%%~fi"
)
)
The Batch file below change all files named test.txt in any folder in the disk by inserting this line "SetNumber=%1":
#echo off
for /R \ %%a in (test.txt) do echo SetNumber=%1> "%%a"
For example, if previous Batch file is called SetNumber.bat, you may change all the files to SetNumber=02 with this command:
setnumber=02
Antonio

Assistance with coding change in Windows batch script?

I have a batch script which unzip and renames each file.
Unfortunately I now need to keep the filename of the zip file it came from.
Example Jazz1.zip now unzips and the outcoming text file becomes 1.Jazz1.zip.txt.
So I want %%F to become %%F - 4- characters.
Unfortunately I want it to be Jazz1.txt.
::Setup the stage...
SETLOCAL ENABLEDELAYEDEXPANSION
SET folder=C:\P\DataSource2_W
SET count=1
::Action
CD "%folder%"
FOR %%F IN ("*.zip") DO (
"C:\Program Files (x86)\WinZip\wzunzip" %%F
MOVE *.txt "C:\P\DataSource2_W\TextFiles\!count!%%F.txt"
SET /a count=!count!+1
)
ENDLOCAL
I do not understand what you are trying to do with the COUNT variable, nor do I understand how you are handling a ZIP file with multiple .TXT files.
But I do understand that you want the base name of each ZIP file, (name without the extension). That is easy - simply use the ~n modifier (type HELP FOR from the command prompt for more info).
So if %%F = Jazz1.zip, then %%~nF yields Jazz1

Resources