I need Dos batch file.

October 11, 2011 at 20:39:20
Specs: Windows Vista
I have to rename files.
I need dos batch file which increment the number in files name. The file names are work0.txt, work1.txt, work2.txt. After batch run, files name should be work1.txt, work2.txt, work3.txt.

See More: I need Dos batch file.

Report •


#1
October 12, 2011 at 09:28:27
I have been doing some research on this topic myself. I have come up with this code:

@echo off
setLocal EnableDelayedExpansion
set /p name=Enter Name:
for /f "tokens=* delims= " %%a in ('dir/b/a-d *.txt') do (
set /a N+=1
ren %%a %name%!N!.txt
)

I have used it and it seems to work pretty well, the only issue I have found is that if the name has a space in it the code will not change the name. Still working on the fix for it. If anyone can add to this it would be greatly appreciated. Hope this helps. One thing to note, there is no directory setup so this file needs to be in the same directory as the files.


Report •

#2
October 12, 2011 at 20:47:19
My working folder at this path:- D:\test.
When i run the rename.bat with this code
@echo off
setLocal EnableDelayedExpansion
set /p name=Enter Name:
for /f "tokens=* delims= " %%a in ('dir/b/a-d *.txt') do (
set /a N+=1
ren %%a %name%!N!.txt
)

Files are not incremented.


Report •

#3
October 13, 2011 at 08:18:22
I don't have much time this morning so I made a quick fix. It will rename them to new(x) and then go back to work(x). I have tested it on a few text files and it works on my end so give it a shot and let me know how it works. Not the perfect fix but should work:

@echo off
setLocal EnableDelayedExpansion

:first
for /f "tokens=* delims= " %%a in ('dir/b/a-d *.txt') do (
set /a N+=1
ren %%a new!N!.txt
)

:second
for /f "tokens=* delims= " %%a in ('dir/b/a-d *.txt') do (
set /a Z+=1
ren %%a work!Z!.txt
)


Report •

Related Solutions

#4
October 17, 2011 at 05:36:06
It's appending the number like work1.txt after run the program work11.txt.

@echo off
setLocal EnableDelayedExpansion
set /p name=Enter Name:
for /f "tokens=* delims=" %%a in ("D:\test\*.txt") do (
set /a N+=1
ren %%a %name%!N!.txt
)


Report •

Ask Question