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
Related
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
...
I wanted to make a batch script that would go into the folder %~dp0\Playlists\%list% and rename each .mp3 file in there to the next number.
For example, the top file would be renamed to "1" and the second would be renamed to "2". I looked everywhere but I couldn't find out why my code wouldn't work.
set playnmbr=0
for /f "usebackq delims=|" %%f in (`dir /b "%~dp0\Playlists\%list%"`) do (
rename %~dp0\Playlists\%list%\%%f %playnmbr%.mp3
set /A playnmbr=%playnmbr%+1
)
You are missing setlocal enabledelayedexpansion (as npocmaka wrote):
setlocal enabledelayedexpansion
set playnmbr=0
for /f "usebackq delims=|" %%f in (`dir /b "%~dp0\Playlists\%list%"`) do (
rename %~dp0\Playlists\%list%\%%f !playnmbr!.mp3
set /A playnmbr=!playnmbr!+1
)
Also, %list% is not defined in your code.
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
)
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
I've been working on a batch script (.bat) to change file name to the same folder name 3 levels up but no luck. I can only get it to rename 1 level up. There are multiple files in multiple folders.
Any help would be greatly appreciated.
I don't completely understand the intent of your current code. But the following will rename a given file based on the folder 3 levels up. Note that you can only rename one file with a given extension per folder using this strategy.
#echo off
setlocal
set "file=H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\abc.avi"
for %%A in ("%file%") do for %%B in ("%%~A\..\..\..") do ren "%%~A" "%%~nxB%%~xA"
Note that I include the extension of the folder name because folder names can include dots.
EDIT
Based on OP's comments, I believe the following will properly rename all relevant .avi files. The code has been tested, and works in my hands. Simply set the root and files values as appropriate.
#echo off
setlocal
set "root=H:\Techs\Exported Videos\Scenario"
set "files=*.avi"
for %%A in ("%root%\.") do set "root=%%~fA"
for /f "delims=" %%A in ('dir /b /s /a-d "%root%\%files%"') do (
for %%B in ("%%~A\..\..\..") do (
if /i "%%~dpB" equ "%root%\" ren "%%~A" "%%~nxB%%~xA"
)
)
you might try this:
#echo off &setlocal
set /a PathLevel=3
for %%a in (
"H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi"
"H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi"
) do (
call:doit "%%~a"
)
goto:eof
:doit
set "fname=%~1"
set /a cnt=0
for %%a in ("%fname:\=","%") do set /a cnt+=1
set /a cnt-=PathLevel
for %%a in ("%fname:\=","%") do (
set /a cnt-=1
setlocal enabledelayedexpansion
if !cnt! equ 0 (
endlocal
set "nname=%%~a"
) else endlocal
)
echo ren "%fname%" "%nname%%~x1"
goto:eof
output is:
ren "H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi" "1-CAS IED.avi"
ren "H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi" "2-SAF.avi"