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
numerically indexed files (Read 5443 times)
Enad
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 8
numerically indexed files
Oct 19th, 2004 at 3:01am
 
Gert,
I'm kind of getting stuck on a subroutine that would handle numerically indexed files that pro engineer uses.  It's difficult to explain, except by example.

Let's say I have a file:
"54321.prt.4"

I want to move this file to the "database directory," but, I need the intenger on the end to be at least one more than what might currently be in that directory, say:
"54321.prt.197"
"54321.prt.198"
"54321.prt.199"

So I would need to rename "54321.prt.4" to "54321.prt.200" then move it there.

But I just can't figure out the trick to knowing that the number I need to know is "199."

The only thing I can think of at the moment...maybe....for instance, taking a sorted dir 54321.prt.* in the database to a temporary file then doing string operations to catch it, converting to integer and adding one.

Would you have any suggestions?

--Danny
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2224
The Netherlands
Re: numerically indexed files
Reply #1 - Oct 19th, 2004 at 3:51am
 
Hello Danny,

It *should* be simple, and after a lot of messing I think I've managed it...
Code:
@echo off
if "%wd_reason%" neq "FILENEW" goto :quit

set DBDIR=C:\Temp

SET HIGHESTEXT=0

FOR %%F in ("%DBDIR%\%WD_FILE_B%*.*") do (
	SET CH_IN=%%F
	Call :CheckHighest
)

rem now HIGHESTEXT contains the highest extension found
rem add 1 to it
SET /a HIGHESTEXT=%HIGHESTEXT% + 1

rem and move the file
move "%WD_FILE%" "%DBDIR%\%WD_FILE_B%.%HIGHESTEXT%

goto :quit


:CheckHighest
call :GetExtension "%CH_IN%"
if %out_ext% gtr %HIGHESTEXT% set HIGHESTEXT=%out_ext%
goto :eof

:GetExtension
rem get just the extension
set out_ext=%~x1
rem remove the period (first char)
set out_ext=%out_ext:~1%
goto :eof

:quit
 



Notes:
set out_ext=%~x1
this will get the eXtension of the file passed as the first parameter.

Good luck, let me know if this is what you wanted.

Gert
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
 
Enad
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 8
Re: numerically indexed files
Reply #2 - Oct 19th, 2004 at 11:57pm
 
Wow, thanks a lot, it works as advertised.  Quite honestly it was mostly over my head and all I could do is test it out.  Quick question though.  When you're doing the:

FOR %%F in ("%DBDIR%\%WD_FILE_B%*.*") do (

is there anyway to peek in subdirectories too to find a potential match?  Or are you restricted to an explicit path here?

Many many thanks!

Danny
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2224
The Netherlands
Re: numerically indexed files
Reply #3 - Oct 20th, 2004 at 12:06am
 
Glad it works Danny,

FOR doesn't traverse subdirectories, so you need to do some extra trickery....

Untested code follows:
Code:
FOR /d %%D in ("%DBDIR%\*.*") do (
    SET THEDIR=%%D
    FOR %%F in ("%THEDIR%\%WD_FILE_B%*.*") do (
		    SET CH_IN=%%F
		    Call :CheckHighest
    )
)
 


Btw, did you get that Shell Scripting book I told you about?

Gert
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
 
Enad
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 8
Re: numerically indexed files
Reply #4 - Oct 20th, 2004 at 12:13am
 
Not yet, but now that you mention it, I think I'll go online and get it now.  That's one thing about the net...you really can't find an excuse to procrastinate, can you?

I'm gonna go try that untested code...wow...took you only seconds to write it, and I have to buy a book to try and understand it....

You're awesome--Thanks,
Danny
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2224
The Netherlands
Re: numerically indexed files
Reply #5 - Oct 20th, 2004 at 12:30am
 
The /d in the first FOR command tells it to only return directories. Without /d it will only return files.

I had my shell scripting book still open at the FOR pages, so it was easy enough to lookup.

Gert
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
 
Enad
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 8
Re: numerically indexed files
Reply #6 - Oct 20th, 2004 at 9:06pm
 
Gert,
If you wouldn't mind helping me with one other little thing, I'll have it working just like I want.

I want to set DBTYPE conditionally:
(example whole file name "54321.asm.127")

set DBTYPE=BAD
if wd_file_b is *.asm then set DBTYPE=STD_ITEM
if wd_file_b is *.prt then set DBTYPE=STD_ITEM
if wd_file_b is *.drw then set DBTYPE=STD_DRW
if DBTYPE=BAD then quit

Would you mind showing me the correct syntax?  My book hasn't come in yet Wink

Danny


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



Posts: 2224
The Netherlands
Re: numerically indexed files
Reply #7 - Oct 20th, 2004 at 9:16pm
 
Hello Danny,

It's best to use GdPUtil for that:
Code:
set DBTYPE=BAD
GdPUtil -matches "%WD_FILE_B%" *.asm
If %ERRORLEVEL% == 1 SET DBTYPE=STD_ITEM
GdPUtil -matches "%WD_FILE_B%" *.prt
If %ERRORLEVEL% == 1 SET DBTYPE=STD_ITEM
GdPUtil -matches "%WD_FILE_B%" *.drw
If %ERRORLEVEL% == 1 SET DBTYPE=STD_DRW

if %DBTYPE% == BAD GOTO :Quit
 



Btw, I hope you asked for express delivery, because I am going on a trip for a few days  8)

Gert
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
 
Page Index Toggle Pages: 1