Computing.Net > Forums > Programming > Automated batch-file renaming

Automated batch-file renaming

Reply to Message Icon

Original Message
Name: Saeung
Date: September 22, 2005 at 02:00:04 Pacific
Subject: Automated batch-file renaming
OS: XP Pro SP2 / MS Server 20
CPU/Ram: P4 3.0, 1GB RAM
Comment:

While scanning invoices from our scanner to a catalog on the server the file(s) are saved as:

FAKT.PDF
FAKT(1).PDF
FAKT(2).PDF ... and so on ...

My goal is to rename theese files to the real
invoice-numbers.
I can setup the scanner-application to auto-execute a program (such as a .BAT-file)
when it detects an incoming scan.
I need to create a batch-file that automatically renames files as follows:

FAKT.PDF => 112817.PDF
FAKT(1).PDF => 112818.PDF
FAKT(2).PDF => 112819.PDF .. and so on ...

How can I set the "variable" for the batch file to remember where to start next time, or how do I call the filenames in the directy so it will understand at which number it should start it process?

I have tried at least 20 different freewares and sharewares and so far none of theese provide this function.

If this is not possible, is there any alternatives?

Please help me on this one, I really need to figure this out....


Report Offensive Message For Removal


Response Number 1
Name: Mechanix2Go
Date: September 22, 2005 at 07:16:19 Pacific
Reply: (edit)

Hi Fredde,

This may help.

It uses a file created in %TEMP%\#\ to keep track of where it is. You need to "initialize" it. So for the example you gave, where you want the next name to be 112817.PDF, you need to do this:

md %TEMP%\#
type nul > %TEMP%\#\112816


::::::::::::seq.bat
@echo off
if %1'==' goto :syntax

:main
set lastNUM=
set nextNUM=
for /f "tokens=*" %%F in ('dir /b/od %TEMP%\#') do set /a lastNUM=%%F
if %lastNUM%'==' echo no counterfile && goto :eof
del %TEMP%\#\%lastNUM%
set /a nextNUM=%lastNUM%+1
type nul > %TEMP%\#\%nextNUM%
ren %1 %nextNUM%.pdf
goto :eof

:syntax
echo you need to call %0 with the file to rename

:eof
::::::::::::::::::::::::::::


If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Automated batch-file renaming

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge