renaming .jpg files in a folder using batch file - windows

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).

Related

Batch: Unable to read file name from a variable in the for loop

I have been trying to count number of line in the 1st .txt file generated in a folder. But %filename% is not being recognised inside for loop. It works when I echo it.
Any help please?
Summary - How do I read the %fileName% in the for loop here:
#echo off
Title: Count number of lines in the first TXT file in a directory
cls
#Echo off
cd /D "%~dp0"
Set /a _Lines=0
FOR %%F IN (*.TXT) DO (
set filename=%%F
goto tests
)
:tests
::Unable to read file name in the below for loop. Hard coding works fine
For /f %%j in ('Find "_nonExists_value_intentionaly" /v /c %filename%') Do Set /a _Lines=%%j
set /A Actual_Lines = %_Lines%-2
Echo %filename% has %Actual_Lines% number of lines

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 parts of files with Windows Batch

How could i rename a certain part of all files inside a directory.
Example
[HorribleSubs] File1 [720p].mkv
[HorribleSubs] File2 [1080p].mkv
Must be renamed into
File1.mkv
File2.mkv
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
FOR /f "tokens=2delims=[]" %%a IN (
'dir /b /a-d "%sourcedir%\[*]*[*].mkv" '
) DO (
SET "newname=%%a"
ECHO(REN "%sourcedir%\[*]%%a[*].mkv" "!newname:~1,-1!.mkv"
)
GOTO :EOF
This should fix your problem.
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.
This is a simple batch you can use to rename a single file manually.
#echo off
title Rename Bat
echo This bat must be in the folder that
echo contains the files to be renamed.
echo Enter File Name
set /p old=
echo Enter New Name
set /p new=
ren "%old%" "%new%"
echo Done
pause
this has a loop to do multiple files manually
#echo off
title Rename Bat
echo This bat must be in the folder that
echo contains the files to be renamed.
:begin
echo Enter File Name
set /p old=
echo Enter New Name
set /p new=
ren "%old%" "%new%"
echo Done
ping -n 3 127.0.0.1 >NUL
goto begin
its real simple hope it helps. fyi it works on windows XP
place file In directory you wanna rename
#echo off
setlocal enabledelayedexpansion
dir /b ¦ find /v /c "*">filescount.txt
for /f %%f in (filescount.txt) do (
set /a "nof=%%f-2"
for /L %%n in (1 1 !nof!) do (
for %%a in (*.mkv) do (
rename "%%a" "file%%n%%~xa"
)
)
)

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 #:

rename multiple jpg files

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

Resources