rename multiple jpg files - windows-7

I have a directory full with time stamped webcamfiles. It start at midnight, and take a picture every minute.
0001webcamimage.jpg
0002webcamimage.jpg
0003webcamimage.jpg
...
0059webcamimage.jpg
0100webcamimage.jpg
Now i want to convert all the files with a batch-file to this format. The sequence is important.
0001.jpg
0002.jpg
0003.jpg
...
0060.jpg
0061.jpg
Can someone help me?

Assuming Windows & 4 digit prefixes
setlocal enabledelayedexpansion
for %%f in (*webcamimage.jpg) do (
set name=%%f
ren "!name!" "!name:~0,4!.jpg"
)

The following is similar to #Alex K.'s suggestion but doesn't extract numbers from the original names. Instead, it uses a counter and forms the new names using the counter's values:
#ECHO OFF
SET /A num=10000
FOR %%I IN (*webcamimage.jpg) DO (
SET /A num+=1
SETLOCAL EnableDelayedExpansion
RENAME "%%I" !num:~-4!.*
ENDLOCAL
)
Note that the new names will always start with 0001.jpg.
UPDATE
If the batch file is not in the same directory as the images, you can specify the path to them like this:
#ECHO OFF
SET /A num=10000
FOR %%I IN (D:\path\to\images\*webcamimage.jpg) DO (
SET /A num+=1
SETLOCAL EnableDelayedExpansion
RENAME "%%I" !num:~-4!.*
ENDLOCAL
)
Alternatively, you could add the CD /D command before the loop to change to the directory where the images are:
#ECHO OFF
CD /D D:\path\to\images
SET /A num=10000
FOR %%I IN (*webcamimage.jpg) DO (
SET /A num+=1
SETLOCAL EnableDelayedExpansion
RENAME "%%I" !num:~-4!.*
ENDLOCAL
)
If, after finishing the job, you need to change back to the directory that was active before invocation of the batch file, use PUSHD and POPD:
#ECHO OFF
PUSHD D:\path\to\images
SET /A num=10000
FOR %%I IN (*webcamimage.jpg) DO (
SET /A num+=1
SETLOCAL EnableDelayedExpansion
RENAME "%%I" !num:~-4!.*
ENDLOCAL
)
POPD
And, of course, you can parametrise the path as well so that you can specify it in the command line when invoking the batch file. Here's how:
#ECHO OFF
SET "imagepath=%~1"
SET /A num=10000
FOR %%I IN ("%imagepath%\*webcamimage.jpg") DO (
SET /A num+=1
SETLOCAL EnableDelayedExpansion
RENAME "%%I" !num:~-4!.*
ENDLOCAL
)
Now you can invoke the batch file at the command prompt or from another batch file like this:
batchname.bat D:\path\to\images
(In case you are not aware: if you call a batch file from another batch file, you'll likely need to add CALL before the name of the batch file being called, i.e. CALL batchname parameters.)

With linux, use rename :
rename 'webcamimage' '' *.jpg

Related

How can I modify this batch file so that it will add a number before the name of file and keep the caption as it is.?

#echo off
setlocal EnableDelayedExpansion
set i=0
for %%a in (*.jpg) do (
set /a i+=1
ren "%%a" "!i!.new"
)
ren *.new *.jpg
I have this batch file to rename all files in folder as 1,2,3...n But the problem is its removing the caption, I want to modify it such that it will keep the caption as it is and will just add a number before a caption.
#Echo Off
SetLocal EnableDelayedExpansion
Set "i=0"
For %%A In (*.jpg) do (Set /A i+=1
Ren "%%A" "!i!%%~nA.new"
)
Ren *.new *.jpg
The filename plus extension above is %%A and the filename without extension is %%~nA. So I used Ren "%%A" "!i!%%~nA.new". Please refer to the help usage of the For command for a full explanation, enter For /? at the Command Prompt to do so.
You can separate the !i! from the %%~nA with a character of your choosing too!

renaming .jpg files in a folder using batch file

I have a folder that has bmp files , they may be 4 in a folder or 50 in a folder, but they are
image.bmp
image1.bmp
image2.bmp
I started a batch file with the below code:
#echo off
setlocal enableDelayedExpansion
SET counter=0
SET /P filename=Please enter the filename:
for %%G in (C:\Test_Folder) do (
ren image*.bmp "%filename%""%counter%".bmp
SET /A counter=%counter%+1;
echo "%counter%"
)
pause
but the counter does not increment, can some one give some light to my code?
#echo off
setlocal enableDelayedExpansion
SET counter=0
SET /P filename=Please enter the filename:
for %%G in (C:\Test_Folder\image*.bmp) do (
ren "%%~G" "%filename%!counter!.bmp"
SET /A counter+=1
echo "!counter!"
)
pause
Changes:
using delayed expansion for the counter variable.
forprocesses matching files in the folder instead of the folder itself.
use ren to rename single files instead of wildcard usage.
SET /A counter+=1 instead of SET /A counter=!counter!+1 (does the same, but improved readabilty).

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.

Combining 2 bat files with enable and disable delayedexpansion

I have 2 bat files working perfectly for my data. One to copy and move some files. The other to group files based on a pattern in their names. I need to combine both in a file or I need to make them work with running one command.
bat 1:
#echo off
setlocal enabledelayedexpansion
rem set language pre-requisites
rem purpose can be 'test' or 'deploy'
SET "purpose=test"
SET "language=English-Indian"
SET "lang_code=EnIn"
rem Since we have only 2 options test or deploy
if "%purpose%"=="test" (
SET "folder=%lang_code%P101M2Tsub"
)
if "%purpose%"=="deploy" (
SET "folder=%lang_code%P101M2DFull"
)
rem set required paths here. Don't edit unless necessary
SET Source101="D:\..path..\*.wav"
SET SourceLive="D:\..path..\M2"
SET Destination="D:\..path..\M3\*"
rem copying the data
xcopy %SourceLive% %Destination% /e /i /h
rem copying 101 files into the other 100 folders
for /d %%a in (%Destination%) do copy %Source101% "%%a"
rem renaming M3 folders
cd %Destination%
for /d %%a in (*) do (
set "p=%%a"
set "fp=!p:~0,8!" & set "tp=!p:~10!"
ren %%a !fp!M3!tp!
)
bat 2:
#echo off
rem Prepare environment
setlocal enableextensions disabledelayedexpansion
rem configure where to start
set "root=some path"
rem For each file under root that match indicated pattern
for /r "%root%" %%f in (*_*_*.wav) do (
rem Split the file name in tokens using the underscore as delimiter
for /f "tokens=2 delims=_" %%p in ("%%~nf") do (
rem Test if the file is in the correct place
for %%d in ("%%~dpf.") do if /i not "%%~p"=="%%~nd" (
rem if it is not, move it where it should be
if not exist "%%~dpf\%%~p" md "%%~dpf\%%~p"
move "%%~ff" "%%~dpf\%%~p"
)
)
)
I need to combine these two bat files into one. As in I need one working bat file. Can someone help?
One simple solution
setlocal enabledelayedexpansion
....
the work in batch file 1
....
setlocal disabledelayedexpansion
the work in batch file 2
....
endlocal
endlocal
Anyway, the only reason for the disabledelayedexpansion in second batch file is to avoid problems with special characters in file/folder names. First batch file doesn't care of it so, if it work, remove the disabledelayedexpansion from second batch file.
Could be as simple as
call batch1
call batch2
BUT
perhaps the cd before the final for in batch1 is changing to a directory that batch2 does not expect.
if so, change the cd to a pushd and add a popd after the current last line of batch1.
OR
since the only part of batch1 that appears to REQUIRE delayedexpansion is the final FOR loop, change batch1 to disabledelayedexpansion mode, then add
setlocal enabledelayedexpansion
directly before the final for loop and
endlocal
on the next line after the final close-parenthesis.

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