Solved Batch script to rename using sequence number

September 25, 2012 at 14:43:04
Specs: Windows 7
I will have a set of filenames (in some pre-determined directory like C:\files) formatted like the following:

1213C-1.jpg
1213C-2.jpg
1214C-1.jpg
1215C-1.jpg
1215C-2.jpg
1215C-3.jpg

and want to rename these files by putting the sequence numbers at the end of the extension like so:
1213C.jpg_1
1213C.jpg_2
1214C.jpg_1
1215C.jpg_1
1215C.jpg_2
1215C.jpg_3


See More: Batch script to rename using sequence number

Report •

✔ Best Answer
September 26, 2012 at 10:19:08
then i think this might work (switching to multi-line mode):

@echo off & setlocal
set destdir=c:\directoryB
cd /d C:\Users\user1\Desktop\Test
for /f "tokens=1-3 delims=-." %%a in ('dir /b') do move %%a-%%b.%%c %destdir%\%%a.%%c_%%b
pause
:: end

as for script #2, you might have left the dot in the delims, which would account for its failure. delims should just be: "delims=-"



#1
September 25, 2012 at 18:37:09
@for /f "tokens=1-3 delims=-." %%a in ('dir /b') do ren %%a-%%b.%%c %%a.%%c_%%b
:: end

or, if you know they're all .jpg extensions:

@for /f "tokens=1-2 delims=-" %%a in ('dir /b *.jpg') do ren %%a-%%b.jpg %%a.jpg_%%b
:: end

not tested! Test using "echo" before first launch! (ie: insert "echo" in front of "ren")


Report •

#2
September 25, 2012 at 18:44:52
Thanks but doesn't appear to be working. Just get a quick command prompt that goes away quickly and nothing happens.

Report •

#3
September 25, 2012 at 18:52:15
Forgot to add that after renaming, I need to move them to another directory. So, files would be in C:\DirectoryA, rename the files and move to C:\DirectoryB

Report •

Related Solutions

#4
September 25, 2012 at 21:22:52
append one line to end of batch:
pause

that should allow you to see what happened, i hope.
Now, moving the goalposts issue: do you want to rename the files in the current directory AND copy (with new name) to dest.dir? or do you want to MOVE the files to the new name in the dest.dir. (or, another possiblity: COPY the file from current directory to dest-dir\NEWNAME leaving the old original names/files in the source directory.) Knowing you want the files renamed and copied/moved still leaves these 3 options in source dir: delete (move), rename (rename & copy) , or leave as is (copy to dest\newname). Hope this is clear.
ps: my current interpretations is option 2: rename in cd and copy to dest\newname, but I'll wait for confirmation.


Report •

#5
September 26, 2012 at 05:34:31
OK...getting close on this. I ran it and saw that it was appending an extra .jpg to the end of the file names. Here's the output from the command prompt:

ren 11445-1.jpg.jpg 11445.jpg_1.jpg
They system cannot find the file specified...

With regard to your question above, I would like to move these files after they are renamed to another directory. Do not want to keep files in the original directory. Would prefer to move or cut/paste to the new dir.


Report •

#6
September 26, 2012 at 05:51:15
I tried your first script and it actually seems to work. These will always be .jpg files so I started out with script #2 which gave me the error I mentioned above. I made a minor change to set the working directory and ran the script and the files are renamed correctly. Here's the script as I have it now:

cd /d C:\Users\user1\Desktop\Test
@for /f "tokens=1-3 delims=-." %%a in ('dir /b') do ren %%a-%%b.%%c %%a.%%c_%%b
:: end
pause

Just need to figure out how to cut/paste to new dir and I think we'll be good to go!


Report •

#7
September 26, 2012 at 10:19:08
✔ Best Answer
then i think this might work (switching to multi-line mode):

@echo off & setlocal
set destdir=c:\directoryB
cd /d C:\Users\user1\Desktop\Test
for /f "tokens=1-3 delims=-." %%a in ('dir /b') do move %%a-%%b.%%c %destdir%\%%a.%%c_%%b
pause
:: end

as for script #2, you might have left the dot in the delims, which would account for its failure. delims should just be: "delims=-"


Report •

#8
September 26, 2012 at 17:03:12
Thanks! Think that does it. Appreciate the assistance!

Report •

Ask Question