Solved how to increment numbers in a batch file using dos?

December 21, 2015 at 23:17:06
Specs: Windows 7
I created many files like 501.xls, 502.xls, 503.xls upto 1200.xls.
I'd like to increase the number like 502.xls instead of 501.xls, 503.xls instead of 502.xls.
Just need to increase the number by one only.
Using F2 is bit hard for such large number of files.
Really appreciate your help in advance

See More: how to increment numbers in a batch file using dos?

Report •


✔ Best Answer
December 23, 2015 at 22:43:11
The output is moved to the "processed" folder within the same directory as the script. You can manually cut/copy and paste the files in the original folder to overwrite them.

Another way around is deleting the old files first, then copying the new ones.

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)



#1
December 22, 2015 at 07:04:03
set /a number=%number%+1

This increases the number by one. To use the number with a file name use

%number%.xls

If you meant renaming the files, then this should work:
(Assuming all of the files are in its own folder)

@Echo off
setlocal enabledelayedexpansion
cd "%~dp0"
mkdir processed
mkdir temp
for /f "tokens=*" %%A in ('dir /b *.xlsx') do (
	set num=%%~nA
	set /a num=!num!+1
	:: xcopy "%%A" "temp" > nul
	:: ren "temp\%%A" "!num!.xlsx" > nul
	:: move "temp\!num!.xlsx" "processed" > nul
	Echo %%A -^> !num!.xlsx
)
Echo Processing complete
pause > nul

To make it actually process, you'd have to remove the "::", just for safety.
It creates a folder called "processed" in which all the processed files are put. This script must run in the folder where the files are located.
You can delete the "temp" folder after the processing is complete.

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ


Report •

#2
December 22, 2015 at 10:16:59
HI ,
Many thanks for reply.
I checked but it only create processed folder and i also delete the ::
But the name couldn't increase by just one number.

This is my file path F:\04-Dec-2015 and contains files from 499.xlsx to 700.xlsx
I am technically some weak. Kindly make the program as per above path and above number. So that i would copy and paste the program on cmd on same folder and it will immediate change the file starting from 500.xlsx. to 701.xlsx in one shot.

Hope you understand my problem.
Again just set the program on above basis so that i can use the same immediately.

Appreciate your help in this matter.


Report •

#3
December 22, 2015 at 10:22:50
Copy the script into any plain-text editor (like notepad) and save it as a .bat file in F:\04-Dec-2015, then run it.

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ


Report •

Related Solutions

#4
December 22, 2015 at 20:56:21
I did the same.
Copy the text and save as .bat extensions.
But it show file not found processing complete.

Report •

#5
December 22, 2015 at 22:50:41
Just noticed you went from saying .xls in your original question to .xlsx in the replies so I've modified the script in my first reply to handle .xlsx files, try running the new script.

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)


Report •

#6
December 22, 2015 at 22:55:39
Excellent now job done. Just one question. Is output move to a new processed folder??
How to avoid it??

Report •

#7
December 23, 2015 at 22:43:11
✔ Best Answer
The output is moved to the "processed" folder within the same directory as the script. You can manually cut/copy and paste the files in the original folder to overwrite them.

Another way around is deleting the old files first, then copying the new ones.

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)


Report •

#8
December 23, 2015 at 23:12:16
Thanks dear for help.

Excellent service by you and excellent platform computing.net
Definitely we will visit again kindly considered it solved

message edited by arjun0014


Report •

Ask Question