Hi All,
Are there any books or websites that teach people how to write batch files?
I was playing with batch code I found on www.computing.net and the stupidest thing happen. I renamed Old1.jpg to File01.jpg, and can do that till I hit 09.
When I try that, instead of File09.jpg, it goes balistic and name it as File008.jpg. The more file I have the more leading 0's it adds.
Here is my code:
@echo off
set Count=00
dir /b | findstr "[0-9].jpg" > tmp1.txt
sort tmp1.txt > tmp2.txt
for /f %%a in (tmp2.txt) do (call :incr
call :ReNumber %%a %%Count%%)
erase tmp?.txt *.tmp
exit /b
:incr
set /a Count=%Count%+1
if %count% LSS 10 set Count=00%count%
goto :eof
:ReNumber %1 %2
if exist %1 ( rem echo renamed %1 File%2.jpg
ren %1 %1.tmp
ren %1.tmp File%2.jpg) else (rem echo ren renamed %1
File%2.jpg
ren %1 File%2.jpg)
goto :eof
And here is the Error:
renamed Old1.jpg File01.jpg
renamed Old2.jpg File02.jpg
renamed Old3.jpg File03.jpg
renamed Old4.jpg File04.jpg
renamed Old5.jpg File05.jpg
renamed Old6.jpg File06.jpg
renamed Old7.jpg File07.jpg
renamed Old8.jpg File08.jpg
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
renamed Old9.jpg File008.jpg
Thank you in advance for any help
Page 1 of 1
Renaming File Sequentially Using Dos Script Batch file
#2
Posted 03 November 2004 - 11:31 PM
The best site for information on batch files is by far the following one:
http://www.robvanderwoude.com/index.html
http://www.robvanderwoude.com/index.html
Lawrence Abrams
Circle BleepingComputer on Google+!
Become a BleepingComputer fan: Facebook
Follow us on Twitter!
How to detect vulnerable programs using Secunia Personal Software Inspector <- Everyone should do this!
Circle BleepingComputer on Google+!
Become a BleepingComputer fan: Facebook
Follow us on Twitter!
How to detect vulnerable programs using Secunia Personal Software Inspector <- Everyone should do this!
#3
Posted 05 November 2004 - 03:17 AM
Hi Qwerty0907,
If you didn't find the answer to your Numeric constants are either decimal... question then post back and I'll try and explain.
Peter
If you didn't find the answer to your Numeric constants are either decimal... question then post back and I'll try and explain.
Peter
#4
Posted 26 April 2009 - 01:20 PM
It is better to use a scripting language instead of DOS. I prefer biterscripting. It is a free scripting language. Use installation instructions at http://www.biterscripting.com/install.html .
Richard
Richard
#5
Posted 26 April 2009 - 01:35 PM
RichardWilliams20, on Apr 26 2009, 01:20 PM, said:
It is better to use a scripting language instead of DOS. I prefer biterscripting. It is a free scripting language. Use installation instructions at http://www.biterscripting.com/install.html .
Richard
Richard
If you run Win XP or above you can use PowerShell which MS designed to be as powerfull as linux shell scripts. It requires .net
#6
Posted 03 May 2009 - 06:18 PM
I would have gone with a simpler batch script and accepted the fact that there would be a space between the word and the number (since batch likes to rename in a type-over style), like so
Although you should note that this only works when the number of characters in the original filename is the same as the number of characters in the destination filename.
Scripting languages are probably the best answer for a problem like this because while the windows shell is good for some things, it always feels like a more clumsy answer to a problem than writing a program or a script, where you have all the documentation readily available and you already know what you can do (you just have to find out how). I suppose that it really depends on the kind of task at hand, but for something like this Python feels more comfortable (and easier).
IMHO you could spend a bunch of time learning how to write, and then actually writing batch scripts (I say a bunch of time because you pretty much have to fight against the idea of the language in order to do many things that you would probably try to do but which is better done with a different kind of program) or you could spend less time in general by learning a scripting language like Python or Ruby or even a programming language, Java or C++ and you will find that you can do a lot of things, not just manipulate files, by using those.
If you wanted to use Python:
@echo off ren "File*.jpg" "Old *.jpg" @echo on
Although you should note that this only works when the number of characters in the original filename is the same as the number of characters in the destination filename.
Scripting languages are probably the best answer for a problem like this because while the windows shell is good for some things, it always feels like a more clumsy answer to a problem than writing a program or a script, where you have all the documentation readily available and you already know what you can do (you just have to find out how). I suppose that it really depends on the kind of task at hand, but for something like this Python feels more comfortable (and easier).
IMHO you could spend a bunch of time learning how to write, and then actually writing batch scripts (I say a bunch of time because you pretty much have to fight against the idea of the language in order to do many things that you would probably try to do but which is better done with a different kind of program) or you could spend less time in general by learning a scripting language like Python or Ruby or even a programming language, Java or C++ and you will find that you can do a lot of things, not just manipulate files, by using those.
If you wanted to use Python:
import os
for i in range(10):
f='File{0:02d}.jpg'.format(i)
d='Old{0:02d}.jpg'.format(i)
os.rename(f,d)
This post has been edited by DemiReticent: 03 May 2009 - 08:17 PM
#7
Posted 15 January 2010 - 08:00 AM
I know this is an old topic but since it was posted to recently, heres a batch file which should work.
If you have up to 999 files with that extension then change it to this
@echo off & setlocal set "ext=jpg" & set "Count=100" for /f "tokens=*" %%a in ( 'dir/b/on/a-d *.%ext%^|findstr/ie "[0-9]\.%ext%"2^>nul') do ( set/a "Count +=1"&&call :ReNum %%Count:~-2%% %%~xa "%%~na") goto :eof :ReNum if not exist File%1%2 ren "%~3%2" File%1%2This will work only if you are aware that there are less than 100 files in that directory (the same one as the batch file is located within).
If you have up to 999 files with that extension then change it to this
@echo off & setlocal set "ext=jpg" & set "Count=1000" for /f "tokens=*" %%a in ( 'dir/b/on/a-d *.%ext%^|findstr/ie "[0-9]\.%ext%"2^>nul') do ( set/a "Count +=1"&&call :ReNum %%Count:~-3%% %%~xa "%%~na") goto :eof :ReNum if not exist File%1%2 ren "%~3%2" File%1%2In both cases you can change jpg on line two to your desired filetype.
#8
Posted 18 January 2010 - 04:27 PM
Since there appears no way for me to edit my previous post in order to append information, here is an updated if not long-winded method which is does the counting for you.
It is intended for those who'd prefer not to guess whether there are up to 10, 100, 1000 etc. files which meet the requirements.
It is intended for those who'd prefer not to guess whether there are up to 10, 100, 1000 etc. files which meet the requirements.
@echo off & setlocal
set "fnm=File" & set "ext=.jpg"
dir/b/on/a-d *%ext%|findstr/ie "[0-9]\%ext%">%tmp%\tmp.tmp 2>nul
for /f "delims=:" %%a in ('findstr/n ".*" "%tmp%\tmp.tmp"') do set "lc=%%a"
for /f %%a in ('cmd/u/cecho/"%lc%"^|find /v /c ""') do set/a "cc=%%a-5"
for /l %%a in (1,1,%cc%) do set nc=0%nc%
for %%a in (%nc%) do set "cnt=%%a"
for /f "tokens=*" %%a in ('findstr/n ".*" "%tmp%\tmp.tmp"') do call :PI "%cnt%%%~a"
del "%tmp%\tmp.tmp" & goto :eof
:PI
set "es=%~1"
set "fn=%es:*:=%"
call set "ln=%%es:%fn%=%%"
set "ln=%ln::=%"
call set "ln=%%ln:~-%cc%%%"
if not exist "%fnm%%ln%%ext%" ren "%fn%" "%fnm%%ln%%ext%"As you may notice I've once again allowed for the file extension to be specified as variable %ext%, (in this case '.jpg' note the required leading dot separator). I've also allowed the static part of the new filename to be specified as variable %fnm%, (in this case 'File').
Share this topic:
Page 1 of 1

Help


Back to top










