Change file names with .bat - batch-file

Hi guys I'm new to batch and have a question for my .bat to rename files.
I looked at the following solution and tried to transfer this to my problem:
Renaming file names with a BAT file
So my .bat looks like this one:
setlocal enabledelayedexpansion
set /a count=1
set padded_count=000!count!
for /f "tokens=*" %%a in ('dir /b /od *.txt') do (
ren "%%a" !padded_count!.txt
set /a count+=1
)
And I have a file with random names for .txt data.
E.g.
abc.txt
def.txt
123.txt
456.txt
And I want to change these into:
0001.txt
0002.txt
...
But when I use my .bat its just the first .txt which changes its name.
Can you explain me why? And what should I do to get all of these.
Or is it possible to handle this problem with REN in cmd with something like "ren *.txt ___"

setlocal enabledelayedexpansion
set /a count=10001
for /f "tokens=*" %%a in ('dir /b /od *.txt') do (
ren "%%a" !count:-4!.txt
set /a count+=1
)
where !count:-4! selects the final 4 characters of count.

After your comment on the requirement, This is similar to #Magoo's answer, but I am not limiting it to 4 chars.
#echo off
setlocal enabledelayedexpansion
set count=10000
for /f "tokens=*" %%a in ('dir /b /od *.txt') do (
if "!count:~1!" == "9999" set count=100000
set /a count+=1
echo ren "%%a" !count:~1!.txt
)
In this instance, once we get to 9999 we set a new count variable so out files will continue with an additional digit.
ren "file9999.txt" 9999.txt
ren "file10000.txt" 00001.txt
ren "file10001.txt" 00002.txt
...

Related

Specify directory location in batch code that would normally work the same folder as the .bat

I have this code with works great if the .bat is located in the same folder as the files. Code came from: .bat for batch rename to increment numbers in fname
#echo off
setlocal enabledelayedexpansion
set /a count=0
for /f "tokens=*" %%a in ('dir /b /od *.cbr') do (
echo ren "%%a" !count!.cbr
set /a count+=1
)
I would like to specificity a directory where the above process can take place.
This is my code but I cannot get it to work.
%NewFolder% - it is specified in the code above this part.
setlocal enabledelayedexpansion
set /a count=0
for /f "tokens=*" %%a in ('dir /b /od "004 - Images Ready\%NewFolder%\"*.JPG') do (
ren "004 - Images Ready\%NewFolder%\%%a" "004 - Images Ready\%NewFolder%\"!count!.JPG
set /a count+=1
)

Batch file to rename file and add sequence number to the file name

I picked up the code below from another post. I believe it should pick up the current directory folder and include it in the renaming part of the process, however that doesn't seem to work for me.
#ECHO OFF
setlocal enabledelayedexpansion
PUSHD "%~1"
set inc=0
FOR /f "delims=" %%a in ('dir /b /a-d') DO (
set /a inc+=1
Echo Ren: "%%a" "%~n1!inc!%%~xa"
Ren "%%a" "%~n1!inc!%%~xa"
)
POPD
I have a .txt file that will be received into a folder each day named and time stamped. Example as below:
FileNameA_20170418153000.txt
Essentially I'd like to amend the code above to rename the file: filenam0001.txt and continue to update the sequence number (which works perfectly well).
i.e.
filenam0001.txt
filenam0002.txt
filenam0003.txt
Any help would be greatly appreciated.
you need to add the leading zeros manually (add some zeros, then cut the last x characters):
#echo off
setlocal EnableDelayedExpansion
set inc=0
for /l %%a in (1,1,50) do (
set /a inc+=1
set num=00000000!inc!
set num=!num:~-5!
echo !num!
)
I modified your code in order to insert the leading zeros in a simple way...
#ECHO OFF
setlocal enabledelayedexpansion
PUSHD "%~1"
set inc=10000
FOR /f "delims=" %%a in ('dir /b /a-d') DO (
set /a inc+=1
Echo Ren: "%%a" "%~n1_%%~na!inc:~1!%%~xa"
Ren "%%a" "%~n1_%%~na!inc:~1!%%~xa"
)
POPD

batch file remove X characters of filename

I did batch file which copy 3 files and need to rename it by removing last 33 characters. The copy works fine but removing last 33 characters not... I saw more then one answer on web and try it all but nothing work so far.
My batch file look like this:
for /f "delims=" %%i in ("my folder") do (
ren "%%i" "%i:~0,-33%".txt
)
I tried already:
set fName=%%i
ren "%fName%" "%fName:~0,-33%.txt"
From the information I got here, try this:
#echo off
setlocal enabledelayedexpansion
set "folderpath=[Your Folder Here...]"
cd %folderpath%
for /f %%a in ('dir /b "*.txt"') do (
set "fname=%%~na"
ren "%%a" "!fname:~0,-33!.txt"
)
endlocal
This is similar to the answer above. You should make sure the batch file is OUTSIDE the folder.
EDIT.
When dealing with variables formed inside FOR and IF's, use delayed expansion (i.e. !var!, instead of %var%). Anyway, this is the fixed code:
#echo off
setlocal enabledelayedexpansion
::NO Last Backslash...
set "sourcepath=C:\Users\tzahi.k\Desktop\testSource\source2"
set "folderpath=C:\Users\tzahi.k\Desktop\testSource\des"
for /F "delims=" %%a in ('dir /b /od "%sourcepath%\*.txt"') do (
set "youngest=%%a"
xcopy /y "%sourcepath%\!youngest!" "%folderpath%"
)
cd /d %folderpath%
for /f %%a in ('dir /b "*.txt"') do (
set "fname=%%~na"
ren "%%a" "!fname:~0,-33!.txt"
)
endlocal
pause
Here's the batch file you'd want to run:
#echo off
Setlocal EnableDelayedExpansion
#for /f "delims=" %%i in ('dir /b *.txt') do (
set fname=%%~ni
set fname=!fname:~0,-33!.txt
ren "%%i" "!fname!"
)
endlocal
This should work
#echo off
setlocal enabledelayedexpansion
set FOLDER_PATH=C:\Some\Path\
for %%f in (%FOLDER_PATH%*) do if %%f neq %~nx0 (
set "filename=%%~nf"
ren "%%f" "!filename:~0,-33!%%~xf"
)
PAUSE
Or better this
#echo off & setLocal enableDELAYedeXpansion
for /f "tokens=* delims= " %%a in ('dir /b *.txt') do (
set F=%%~Na
set F=!F:~0,33!
move /y "%%a" "!F!%%~Xa"
)

Changing folder name with names from a file using batch

I want to write a batch script to rename folders in a directory.
The way that would work is, I would have a file that contains names that I would like each folder to be renamed with. So basically the batch script would just pick names from the file (that contains names) and use it to rename each folder.
So if I have 20 folders, 20 names would exist in file to rename each folder.
What I have so far:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
< %new% (for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))
The above script didn't give me the desired result.
Not tested:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
set counter=0
(for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
set /a counter=counter+1
for /f "tokens=1* delims=:" %%a in ('findstr /R /N "^" "%new%"^|find "!counter!"') do set "newname=%%b"
ren "%%f" "!newname!"
)
The problem is that the dir /b %old% command generate a list of files with .txt extension. If you want to rename folders, then include /AD switch and eliminate the wild-card:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET new="c:\Users\user\Desktop\testing.txt"
< %new% (for /f "tokens=*" %%f in ('dir /b /AD') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))

Renaming files of a specific folder with numbering (script)

I need to rename all files in a folder with numbering, for example - i got 4 files (test.txt test(1).txt test.doc test(1).doc) and i need to rename them to name1.txt name2.txt name3.doc name4.doc
im currently trying this script but it does not work properly
#ECHO off
for /L %%n in (1,1,10) do rename D:\folder\*.* name%%n.*
end
any advices? thanks
#echo off
pushd c:\someDir
setlocal enableDelayedExpansion
set /a counter=0
for /f "delims=" %%a in ('dir /b /a-d *') do (
set /a counter=counter+1
ren "%%~nxa" "NAME!counter!%%~xa"
)
endlocal

Resources