Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


rename files based on zip date

rename files based on zip date

rename files based on zip date

I have a couple of scripts which for the most part work but I need to change it up a bit based on new requirments. Here is what is currently happening.

Get 80 zip files in named yyyymmdd-(location).zip
Unzip each zip to a folder based on location code in zip name.
Rename the files to include yesterdays date.
Zip up files and move to anyother directory based on location code.

All the above works fine if I only get one zip file per location per day. This is an issue when I get more than one zip file per location. So if I get 20080323-1.zip and 20080324-1.zip because the code I am using will just unzip the first one then unzip the second to the same folder which will over write the first ones before I am able to rename them.

What I need to do is unzip and rename the files based on the date of the zip file then zip all of the files into one zip based on yyyymmdd.zip.

Here is my current code (which came with a lot of help from this forum :) )


@echo off
cd po\test\users\test
dir /b *.zip | awk -f  C:\test\unzip.awk  C:\stores.txt - > C:\unzip.cmd
The above calls the awk code below


        UnZipCommand1 = "c:\\progra~1\\winzip\\wzunzip.exe -e -s C:\\po\\test\\users\\test\\"
        UnZipCommand2 = " @list.txt C:\\po\\test\\users\\test\\"
# Making the field separator -, ., or TAB makes it work for both
# the file name and the tab delimited map file.
        FS = "[-.\t]"
        print "@echo off"


        if( NR == FNR ) {
# The file containing the location map.
                Table[ $1 ] = $2
        else {
# The directory listing on STDIN

# The source is the filename in the DIR listing.
                Fname = $0
# The target is the given parent directory followed by the location
# from the map file corresponding to the location number in the
# file name (the second field).
                Target = Table[ $2 ]
# It may be desirable to ad a trailing "\\" to the command.
                Command = UnZipCommand1 Fname UnZipCommand2 Target
                print Command


Then I run the following to rename the files based on the date, zip and move to the correct directory.



timetab(z,time() - 86400)
while (i < 2){
yesterday = time()-(i*86400)
while ( (getline < "Stores.txt") > 0)
printf("REN c:\\po\test\users\test\\%s\\emp.dbf em%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\gndtndr.dbf td%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\gndline.dbf ln%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\gnditem.dbf it%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\itm.dbf i%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\trans.log t%s%02s%02s.log\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\cit.dbf ci%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\cat.dbf ca%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\gndrevn.dbf gr%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\rev.dbf rv%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\gndturn.dbf gt%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\tdr.dbf t%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\pro.dbf p%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"
printf("REN c:\\po\test\users\test\\%s\\cmp.dbf c%s%02s%02s.dbf\n",$2,substr(y["YEAR"],3,2),y["MONTH"],y["DAY"])  > "C:\\test\\doit.bat"


while ( (getline < "Store.txt") > 0)

printf("c:\\progra~1\\winzip\\wzzip.exe -m -s C:\\po\test\users\test\\%s\\%s%02s%02s.zip C:\\po\test\users\test\\%s\\*.*\n",$1 ,z["YEAR"],z["MONTH"],z["DAY"], $1) > "C:\\test\\doit.bat"


while ((getline <"Store1.txt") > 0)
printf("XCOPY /s C:\\po\test\users\test\\%s\\*.zip C:\\iFtpSvc\\info\\users\\download\\test\\%s\\\n",$1,$1) > "C:\\test\\doit.bat"


while ((getline <"Store2.txt") > 0)
printf("del C:\\po\test\users\test\\%s\\*.zip\n",$1) > "C:\\test\\doit.bat"
printf("del C:\\po\test\users\test\\*.zip -w 10\n")  > "C:\\test\\doit.bat"


Any ideas?

RE: rename files based on zip date

Why not use timetab() in unzip.awk to obtain the date you are interested in, and simply skip over files that do not match it?


RE: rename files based on zip date

Re-reading the question, I figure you don't really want to ignore files, just because they came in a day later than expected.

I think you'll need to combine your two scripts into one to handle that situation cleanly.  Have you tried using system() calls to run the zips and unzips directly from awk, rather than creating a batch file that you run later?  That would give you more immediate control over the logic in the process.  I haven't used awk under Windows, so don't know how well that works.


RE: rename files based on zip date

Correct, I want to process the files no matter when they came in I just want to rename the extracted files so they have the date of the zip files.

Combining them would be ideal I just do not know how at this time. I have not tried system calls. I did think that if I could do a dir/b *.zip > zip.txt I could get the zip file names. I would then somehow need to get that into the names of the extracted files.

RE: rename files based on zip date

Try it and see I guess.

This kind of syntax should also work (it would under Unix anyway):


    CMD="dir /b *.zip"
    while (CMD | getline) {
        system("ren " $1 " " $1 ".newname")

What version of awk are you using?


RE: rename files based on zip date

I have two versions of awk. Tawk and Gawk.

I get the CMD="dir /b *.zip" line. Can you walk me through what the rest of it does?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close