Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Renaming File Sequentially Using Dos Script


  • Please log in to reply
7 replies to this topic

#1 Qwerty0907

Qwerty0907

  • Members
  • 22 posts
  • OFFLINE
  •  
  • Local time:05:56 AM

Posted 03 November 2004 - 08:07 PM

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

BC AdBot (Login to Remove)

 


#2 Grinler

Grinler

    Lawrence Abrams


  • Admin
  • 43,622 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:USA
  • Local time:06:56 AM

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

#3 penmore

penmore

    Malware Sniffer


  • Members
  • 757 posts
  • OFFLINE
  •  
  • Location:West Coast of Scotland
  • Local time:11:56 AM

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 :thumbsup:

#4 RichardWilliams20

RichardWilliams20

  • Members
  • 1 posts
  • OFFLINE
  •  
  • Local time:06:56 AM

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

#5 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:05:56 AM

Posted 26 April 2009 - 01:35 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


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 DemiReticent

DemiReticent

  • Members
  • 17 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Texas
  • Local time:05:56 AM

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

@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)

Edited by DemiReticent, 03 May 2009 - 08:17 PM.


#7 HelenBach

HelenBach

  • Members
  • 10 posts
  • OFFLINE
  •  
  • Location:United Kingdom
  • Local time:10:56 AM

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.
@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%2
This 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%2
In both cases you can change jpg on line two to your desired filetype.

#8 HelenBach

HelenBach

  • Members
  • 10 posts
  • OFFLINE
  •  
  • Location:United Kingdom
  • Local time:10:56 AM

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.
@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').




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users