WatchDirectory home page
WatchDirectory Startseite (Deutsche Version)
Site WatchDirectory (Français)
  Welcome, Guest. Please Login or Register
YaBB - Yet another Bulletin Board
   
  HomeHelpSearchLoginRegister  
 
Page Index Toggle Pages: 1
Calculate a new file name - based on orig. name (Read 4523 times)
ojan
YaBB Newbies
*
Offline



Posts: 5
Calculate a new file name - based on orig. name
Feb 7th, 2012 at 10:18am
 
Hi
I regularly receive files that need to be renamed.
The files are named like:

A000001_#some_name.txt
A000002_#another_name.tx
A000211_#something_different.txt

I need to increase the number in the file names with 1.
So the new names for these three files would be:

A000002_#some_name.txt
A000003_#another_name.tx
A000212_#something_different.txt

Any ideas on how this could be done?

Best regards
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2239
The Netherlands
Re: Calculate a new file name - based on orig. nam
Reply #1 - Feb 7th, 2012 at 10:25am
 
Suppose we do this....

Won't that cause endless loops? The newly detected file has its number incremented (file renamed) and WD detects the file again....

Or are those renamed files moved to another directory, not monitored by WD (or at least not by this task)?
Back to top
 

Gert Rijs - gert (at) gdpsoftware (dot) com
Blog: http://blog-en.gdpsoftware.com/
End Alzheimer's: http://www.alz.org&&...
WWW WWW GdPSoftware  
IP Logged
 
ojan
YaBB Newbies
*
Offline



Posts: 5
Re: Calculate a new file name - based on orig. nam
Reply #2 - Feb 7th, 2012 at 10:32am
 
Wow - the answers are coming fast in this forum  Smiley

You are right, Gert.
I forgot to mention that the files are going to be moved to another directory

Best regards
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2239
The Netherlands
Re: Calculate a new file name - based on orig. nam
Reply #3 - Feb 7th, 2012 at 10:36am
 
Same timezone as DK, that helps. I used to live in Silkeborg, by the way. Until I found out Danish women are just as crazy as Dutch women.

I think the best approach for this is to use a little scripting and use the "run a bat file" task. I will create a small script for you and post it here in the next few hours.
Back to top
 

Gert Rijs - gert (at) gdpsoftware (dot) com
Blog: http://blog-en.gdpsoftware.com/
End Alzheimer's: http://www.alz.org&&...
WWW WWW GdPSoftware  
IP Logged
 
ojan
YaBB Newbies
*
Offline



Posts: 5
Re: Calculate a new file name - based on orig. nam
Reply #4 - Feb 7th, 2012 at 10:58am
 
Bat file is the answer.
I can't use {sequence}.
Because I'm not sure that the original files allways will be sequential.
I'm looking forward to seing your bat file - I haven't got the skills to write one my self.

Are you sure it's the women that are crazy  Wink

By the way I live in Aarhus - close to Silkeborg

Best regards
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2239
The Netherlands
Re: Calculate a new file name - based on orig. nam
Reply #5 - Feb 7th, 2012 at 11:41am
 
The script below should help, only slightly tested. Make sure to change the first line (the target directory).

If the target directory is on the network, make sure to use an UNC name (\\server\share\somedir). If you run the task as a Windows service, make sure to read http://blog-en.gdpsoftware.com/2010/04/watchdirectory-tasks-as-windows-service.html

[code]rem - settings for this script
SET TARGETDIR=F:\testing\target


rem check if we are called for FILENEW
IF "%WD_REASON%" NEQ "FILENEW" GOTO :EOF
rem filename should start with the letter A
IF /I "%WD_FILE_B:~0,1%" NEQ "A" GOTO :EOF

rem get the numeric part and the remainder of the filename (_somethingelse)
Call :GetNumFromFile
IF "%OK%" EQU "NO" GOTO :EOF

SET /A NEWFILENUM=%NUM% + 1
rem make sure there are enough leading zeroes
SET NEWFILENUM=0000000000000000000%NEWFILENUM%
rem that was obviously to much, take the last 6 digits
SET NEWFILENUM=A%NEWFILENUM:~-6%
SET TARG=%TARGETDIR%\%NEWFILENUM%%REMAINDER%.%WD_FILE_E%

MOVE /Y "%WD_FILE%" "%TARG%"
GOTO :EOF






:GetNumFromFile
SET NUM=0
SET OK=NO
SET CHECK=%WD_FILE_B:~1%
SET REMAINDER=
:GetNumFromFile_again
IF "%CHECK%" EQU "" GOTO :EOF
SET POS1=%CHECK:~0,1%
SET CHECK=%CHECK:~1%
IF "%POS1%" EQU "_" GOTO :GetNumFromFile_done
SET /A NUM=%NUM% * 10 + %POS1%
GOTO :GetNumFromFile_again

:GetNumFromFile_done
SET OK=YES
rem remainder has everything starting from the _
SET REMAINDER=%CHECK%
GOTO :EOF
[/code]
Back to top
 

Gert Rijs - gert (at) gdpsoftware (dot) com
Blog: http://blog-en.gdpsoftware.com/
End Alzheimer's: http://www.alz.org&&...
WWW WWW GdPSoftware  
IP Logged
 
ojan
YaBB Newbies
*
Offline



Posts: 5
Re: Calculate a new file name - based on orig. nam
Reply #6 - Feb 7th, 2012 at 2:17pm
 
Thanx, Gert

It's almost right.
Some where the underscore "_" disapears so a file named


A000001_#some_name.txt

is renamed to

A000002#some_name.TXT

Trying to figure out why

Best regards
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2239
The Netherlands
Re: Calculate a new file name - based on orig. nam
Reply #7 - Feb 7th, 2012 at 2:21pm
 
find this line in the script
[code]SET TARG=%TARGETDIR%\%NEWFILENUM%%REMAINDER%.%WD_FILE_E%[/code]
and replace it with this line
[code]SET TARG=%TARGETDIR%\%NEWFILENUM%_%REMAINDER%.%WD_FILE_E%[/code]

That should do the trick.
Back to top
 

Gert Rijs - gert (at) gdpsoftware (dot) com
Blog: http://blog-en.gdpsoftware.com/
End Alzheimer's: http://www.alz.org&&...
WWW WWW GdPSoftware  
IP Logged
 
ojan
YaBB Newbies
*
Offline



Posts: 5
Re: Calculate a new file name - based on orig. nam
Reply #8 - Feb 7th, 2012 at 3:32pm
 
Works like a charm - thanx Smiley

Great software and great support

Best regards
Back to top
 
 
IP Logged
 
Page Index Toggle Pages: 1