Solved

DOS increment alpha filename

Posted on 2009-05-10
11
598 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24356530
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
ID: 24358264
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
ID: 24358753
OK.

I tested it by creating a file with

ECHO !LastPart!>%archivefolder%\IA90511!LastPart!.dat
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:avgplusguy
ID: 24367561
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
ID: 24368008
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
 

Author Comment

by:avgplusguy
ID: 24369405
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
ID: 24369624
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
ID: 24369847
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
ID: 24369883
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
ID: 24370039
AmazingTech. this looks fabulous and it answers what I originally asked.
THANK YOU. GREAT JOB
0
 

Author Closing Comment

by:avgplusguy
ID: 31579926
Great job. Handled everything I asked and more.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 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