Reputation: 763
There are four files in directory: 12_1.txt, 12_2.txt, 12_3.txt, 12_4.txt. I need to rename them (using command "ren" or "rename") so, that:
12_1.txt would be the 12-4.txt
12_2.txt would be the 12-3.txt
12_3.txt would be the 12-2.txt
12_4.txt would be the 12-1.txt
I'm tried to do something like that:
set /a pos=0 & set /a length=5 & for %x in (*) do
@(set /a pos+=1 & set /a length-=1 & ren 12_%length%.* 12-%pos%.* >nul)
But it did not work and only one file was renamed, befause of "ren" command always saw one value for the variables the last one that the loop gave. I think I wrote a lot of superfluous there. My knowledge in cmd is at the level of 7% of 100.
Upvotes: 1
Views: 397
Reputation: 173
I had this issue but with 20 or so files in each folder, and ended up making a .bat file, which runs in the same folder as the files that need renaming. In my case the filenames were like IMAGE_0001.JPG, IMAGE_0002.JPG etc... but some batches of images started somewhere arbitrary like IMAGE_0127.JPG. I had to preserve the same prefixes and same numbers, just reversing the order.
@echo off
setlocal enabledelayedexpansion
:: Set the prefix and extension
set "basename=IMAGE_"
set "extension=.JPG"
:: Set starting and ending numbers
set /a start=1
set /a end=50
:: Calculate the total count of files
set /a total=end - start + 1
:: Create a temp folder
mkdir temp
:: Move all files to the temp folder
for %%F in (%basename%*.JPG) do (
move "%%F" temp
)
:: Rename files in reverse order
set /a newNumber=end
for %%F in (temp\%basename%*.JPG) do (
set "oldFile=%%F"
set "newFile=%basename%!newNumber!%extension%"
echo Renaming !oldFile! to !newFile!
move "!oldFile!" "!newFile!"
set /a newNumber-=1
)
:: Cleanup
rmdir temp
echo Done
endlocal
Upvotes: 0
Reputation: 291
This can at least sort the names in reverse order and output it to a text file,
then you can copy over those lines for renaming or copieing purposes:
dir /b /O:-N > files2.txt
Upvotes: -1