Solved

DOS increment alpha filename

Posted on 2009-05-10
11
592 Views
Last Modified: 2012-05-06
Our department processes filenames that are currently in YYMMDD## format where the first file for today May 10 is uploaded as 09051001. The next file is uploaded as 09051002. After they are uploaded they go in a folder called archive. at the end of the day a folder is created called aryymmdd and the files are moved there from folder archive leaving folder archive empty.

Multiple new departments are being added and the filename is changing.
New filenames must be written in the format AAYmmddAAA format
The first two alpha characters will change according to the type of job.
The last three characters are first the alphabet and then 1-999

The first file is written to AA90510A.DAT and then placed in the archive folder.
When the next job is processed, it must look in the archive folder and find the highest letter.
For the second file it would write out AA90510B.DAT and move it to the archive folder
Today
First file             IC90510A.DAT
Second file        IA90510B.DAT
Third file            IA90510C.DAT
26thfile              IB90510Z.DAT
27th file              IC905101.DAT
28th file              IA905102.DAT

The first two alpha charcters do not influence the sequencing order of the files.
IA, IB, IC do not have to be kept separate and the order is random /mixed.
Slight modification of current code, but it is not working.
 

@echo off

rem * Batch job designed to create a sequence number for the IA records used

rem * to upload contractuals at time of billing to PBAR. tammie sherwood 05/05/09

rem * Delete all text files once sequence has reached 99999 and start over with 0

if exist 19999.txt del 1*.txt

Copy .\10000.LOG .\10000.txt

rem * Control batch sequence with Set FILE command

set LIST=

for %%I in (10*) do set LIST=%LIST%%%~nI

ECHO %LIST% 

set /A FILE=%LIST%+1

echo %FILE%

Copy .\10000.txt .\%FILE%.txt

del 10000.txt

echo %LIST%

del %LIST%.txt

Open in new window

0
Comment
Question by:avgplusguy
  • 6
  • 5
11 Comments
 
LVL 21

Expert Comment

by:AmazingTech
Comment Utility
Here's some code to figure out the last part of the filename but as more files gets into the archive folder the longer the script takes. A way to reduce the time is if the files are also in order by date/time then we could do a dir sort with date/time and take the first filename as the last letter/number and increment by 1.
SETLOCAL ENABLEDELAYEDEXPANSION

Set letters=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z

Set archivefolder=c:\archive
 

set LastFilename=

set LastPart=

for /f "tokens=*" %%a in ('dir /a-d /b "%archivefolder%"') do (

    Set LastFilename=%%~na

    echo !LastFilename:~7! | FINDSTR /i [%letters%]

    if not errorlevel 1 (

        echo !LastPart! | FINDSTR /i [%letters%]

        if not errorlevel 1 if /i "!LastPart!" LSS "!LastFilename:~7!" Set LastPart=!LastFilename:~7!

    ) ELSE (

        echo !LastPart! | FINDSTR /i [%letters%]

        if not errorlevel 1 (

            Set LastPart=!LastFilename:~7!

        ) else (

            if !LastPart! LSS !LastFilename:~7! Set LastPart=!LastFilename:~7!

        )

    )

)
 

if defined LastPart (

    echo !LastPart! | FINDSTR /i [%letters%]

    if not errorlevel 1 (

        Set NextLetter=

        Set LastPart2=

        for %%b in (%letters%) do if not defined nextletter (if /i "%%b" == "!LastPart!" Set NextLetter=True) else (Set LastPart2=%%b&& Set NextLetter=)

        if defined LastPart2 (

            Set LastPart=!LastPart2!

        ) else (

            Set LastPart=1

        )

    ) else (

        Set /a LastPart+=1

    )

) else (

    Set LastPart=A

)
 

ECHO !LastPart!

ECHO !LastPart!>%archivefolder%\IA90511!LastPart!.dat

Open in new window

0
 

Author Comment

by:avgplusguy
Comment Utility
AmazingTech,
I know just enought to be dangerous. I am trying to help someone else.
I will check to see if we can sort by date because that makes the most sense.
I think this is almost it. I am not sure how many files will be handled daily. File 27 is AAYmmdd1.dat
File 36 is AAYmmdd10.dat File 126 is AAYmmdd100.dat. I think your code handles that, but have ot tested...
0
 
LVL 21

Expert Comment

by:AmazingTech
Comment Utility
OK.

I tested it by creating a file with

ECHO !LastPart!>%archivefolder%\IA90511!LastPart!.dat
0
 

Author Comment

by:avgplusguy
Comment Utility
I have confirmed that the files will be in date order therefore the pseudo code would be
IF C:\archive is empty (outside of . and .. files)
REN firstfile.txt  AAYmmddA.dat
FTP AAYmmddA.dat
MOVE AAYmmddA.dat to c:\archve

ELSE
Find most recent file in c:\archive
THE NEXT TWO LINES ARE THE HARDEST CODING PROBLEM
If file ends in A-Y increment to next letter
If file ends in Z increment to 1
If file ends in a # increment by 1

Hopefully this clarifys instead of confuses
0
 
LVL 21

Expert Comment

by:AmazingTech
Comment Utility
So you're going to have say 100 files in a folder and then you want to run the script to rename them and put them into the archive folder?

How does the script determine the preceeding AA?

My code will handle all that you asked above. Maybe I'll need to move my code around a bit to make it more efficient.

I'm not too sure what your date format is but I'll assume it's M/d/yyyy
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:avgplusguy
Comment Utility
Date format in the filename is YEAR (9) MONTH (0) MONTH(5) DAY(1) DAY(2).
Maybe 100-1000 files a day in three to five folders. One folder will have files starting with IA, one with IB, one with IC and others yet to be determined. The file type is determined by the two alpha characters. All files are transmitted, but must be sent sequentially. The first file today could be IA90512A.DAT or IB90512.DAT or IC90512A.DAT or AA90512A.DAT. The next file could be IA90512B.DAT, IB90512B.DAT, IC90512B.DAT or AA90512B.DAT. If the next file is not sequential, eg., IA90512D.DAT, the receiving end aborts for the day.

Coding the AA front end depends upon which folder our files are in.
IF it resides in the IA folder, we name the file IA.
Our primary concern is sending the files in A-Z, 1-999 order.
0
 
LVL 21

Expert Comment

by:AmazingTech
Comment Utility
So you're example would end up like this?

So the file IA90512A.DAT is already sitting in a folder and IA and Date doesn't need to be changed just the last bit?
IA90512A.DAT       to C:\Archive\IA90512A.DAT
IB90512.DAT       to C:\Archive\IB90512B.DAT
IC90512A.DAT       to C:\Archive\IC90512C.DAT
AA90512A.DAT       to C:\Archive\AA90512D.DAT
IA90512B.DAT       to C:\Archive\IA90512E.DAT
IB90512B.DAT       to C:\Archive\IB90512F.DAT
IC90512B.DAT       to C:\Archive\IC90512G.DAT
AA90512B.DAT       to C:\Archive\AA90512H.DAT
0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 500 total points
Comment Utility
The easiest would be to always figure out the last part from C:\Archive.


SETLOCAL ENABLEDELAYEDEXPANSION

Set letters=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z

Set archivefolder=c:\archive
 

for /f %%a in ('dir /a-d /od "c:\IAFolder\IA*.DAT"') do (

     CALL :GET_LASTPART

     Set Filename=%%na

     Set CodeAndDate=!Filename:~0,7!!LastPart!

     REN %%a !CodeAndDate!%%~xa 

     FTP !CodeAndDate!%%~xa

     MOVE "C:\IAFolder\!CodeAndDate!%%~xa" %archivefolder%

)
 

:GET_LASTPART 

set LastFilename=

set LastPart=

for /f "tokens=*" %%a in ('dir /a-d /b "%archivefolder%"') do (

    Set LastFilename=%%~na

    echo !LastFilename:~7! | FINDSTR /i [%letters%]

    if not errorlevel 1 (

        echo !LastPart! | FINDSTR /i [%letters%]

        if not errorlevel 1 if /i "!LastPart!" LSS "!LastFilename:~7!" Set LastPart=!LastFilename:~7!

    ) ELSE (

        echo !LastPart! | FINDSTR /i [%letters%]

        if not errorlevel 1 (

            Set LastPart=!LastFilename:~7!

        ) else (

            if !LastPart! LSS !LastFilename:~7! Set LastPart=!LastFilename:~7!

        )

    )

)

 

if defined LastPart (

    echo !LastPart! | FINDSTR /i [%letters%]

    if not errorlevel 1 (

        Set NextLetter=

        Set LastPart2=

        for %%b in (%letters%) do if not defined nextletter (if /i "%%b" == "!LastPart!" Set NextLetter=True) else (Set LastPart2=%%b&& Set NextLetter=)

        if defined LastPart2 (

            Set LastPart=!LastPart2!

        ) else (

            Set LastPart=1

        )

    ) else (

        Set /a LastPart+=1

    )

) else (

    Set LastPart=A

)

Open in new window

0
 

Author Comment

by:avgplusguy
Comment Utility
Essentially correct.
The files coming in could be named anything originally, but must be renamed like your example.
The folder name will dictact the AA first two letters and the sequence that it is sent will determine the last one to three characters.
0
 

Author Comment

by:avgplusguy
Comment Utility
AmazingTech. this looks fabulous and it answers what I originally asked.
THANK YOU. GREAT JOB
0
 

Author Closing Comment

by:avgplusguy
Comment Utility
Great job. Handled everything I asked and more.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now