Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract zips into correct folder

Status
Not open for further replies.

tcdrake

Technical User
May 20, 2003
16
0
0
US
We recieve zip files in the following format...yyyymmdd-#.zip where #
starts at 1 and increments up to 79. I need to unzip these files into
seperate folders based on a specified folder name. Here is what I
have....
Code:
BEGIN{ 
timetab(z,time() - 86400) 


printf("c:\\progra~1\\winzip\\wzunzip.exe -e -s %s%02s%02s-.zip c:\ 
\test\\1\n",z["YEAR"],z["MONTH"],z["DAY"])  > "test.bat" 
printf("c:\\progra~1\\winzip\\wzunzip.exe -e -s %s%02s%02s-.zip c:\ 
\test\\2\n",z["YEAR"],z["MONTH"],z["DAY"])  > "test.bat" 



}

As you can see I have the date portion down its the # after the dash in the zip name I don't know how to do. I'm pretty sure I need the ++ but not sure how.


I think I need to add more info....

We have 80 zip files coming into one FTP directory each day labled yyyymmdd-(store number).zip. These will then need to be extracted to the folder associated to each location(in the code I have it 1 and 2). The location folder name is random and does not match the store number in the zip name. So from what I gather one way would be to scan the FTP incoming directory to get the ZIP names, not sure how to do this in awk. I also need to figure out how to match the zip to the correct folder it needs to be
extracted to. Any ideas?

 
I'm presuming timetab() is a function implemented elsewhere?

tcdrake said:
So from what I gather one way would be to scan the FTP incoming directory to get the ZIP names, not sure how to do this in awk.

Why not cd "FTP Directory and then dir /b *.zip > listing.txt. Then you can read in that file using awk.

Incidentally, you'll need to use >> test.bat to append to the batch file rather than overwriting it each time.

tcdrake said:
The location folder name is random ... I also need to figure out how to match the zip to the correct folder it needs to be extracted to.

If it is random... how can you know which is the "correct" folder?

Annihilannic.
 
Let's see if I can make it a little clearer.

For example the zip coming in is 20080305-1.zip this should be unzipped into a folder called 09087000. The 1 in the zip name is the location name. The folder 0987000 is a system folder which corresponds with the location name. This is the same for all 80 zips which come in. So by random I mean that zip 20080305-1.zip doesn't unzip into folder 1.

timetab I was using to pickup the date in the zip file. I like the idea of using the listing.txt file to read the zip file names. I'm not very proficient as you can tell so I could use as much help as possible in this and I do appreciate it.

 
Do you have some kind of table that cross-references the number in the zip name with system folder, or were you planning to hard-code those relationships in your script or something?

I suggest you create a file containing those mappings, say location_system_mapping.txt, containg lines like:

[tt]1 09087000
2 13061000
3 68483000
..[/tt]

And then:

[tt]awk -f unzip.awk location_system_mapping.txt listfile.txt[/tt]

And in unzip.awk:

Code:
NR==FNR { system[$1]=$2 ; next }
{
    location=$1
    sub("^.*-","",location)
    sub("[.].*$","",location)
    printf("c:\\progra~1\\winzip\\wzunzip.exe -e -s %s c:\
\test\\%s\n",$1,system[location]) >> "test.bat"
}

Annihilannic.
 
Thanks again for helping out.

I do have a list of all locations mapped to the specified folder. This is a txt document like you suggested.

What is the listfile.txt in the first awk script?

When I ran your script without changing anything I recieved the following errors...

awk: error in unzip.awk line 1: expecting () after builtin function: system
awk: error in unzip.awk line 6: expecting () after builtin function: system

I changed your code around a bit just to see what I got. This is what I changed it to...
Code:
NR==FNR  {system($1=$2) ; next }
{
    location=$1
    sub("^.*-","",location)
    sub("[.].*$","",location)
    printf("c:\\progra~1\\winzip\\wzunzip.exe -e -s %s c:\\test\\%s\n",$1,system(location)) >> "test.bat"
}

Which then gave me this output...

Bad command or file name
Bad command or file name
awk: warning: Can not open file: "listfile.txt"

Any suggestions?
 
Replace system with anything non reserved:
Code:
NR==FNR { [!]s[/!][$1]=$2 ; next }
{
    location=$1
    sub("^.*-","",location)
    sub("[.].*$","",location)
    printf("c:\\progra~1\\winzip\\wzunzip.exe -e -s %s c:\
\test\\%s\n",$1,[!]s[/!][location]) >> "test.bat"
}

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I will work on what you just said. What about the list.txt you had in the first awk example. What is that?
 
Ran it the way you had it and get...

C:\tawk>awk -f unzip.awk list.txt listfile.txt
awk: error in unzip.awk line 6: function s undefined
awk: aborting due to compilation errors

I changed s to e for the heck of it just to make sure that was what was causing the issue and it was. Hopefully you still have time to help me out.
 
In my post on 6 Mar 08 17:26 I suggested you create a list file containing the filenames using dir /b *.zip > listing.txt. This is the listfile.txt I was referring to (apologies for changing the name).

Sorry about the system() reserved word thing, strangely I had changed that in my copy of the script but posted the wrong version.

I suspect you still have round brackets in your script where they should be square, i.e. s($1)=$2 instead of s[$1]=$2. You are assigning a value to an array member, not calling a function.

Annihilannic.
 
Making progress but have another question. In your post on 6 Mar 08 20:42 you reference making a file called location_system_mapping.txt. I created this file and added data. I don't think the awk script actually does anything with that file though. I deleted everything but one number in the txt file, ran the script and the test.bat file looked the same. Below is the test.bat file before and after I made the changes to the txt file.

Code:
c:\progra~1\winzip\wzunzip.exe -e -s 20080123-30.zip c:\test\30
c:\progra~1\winzip\wzunzip.exe -e -s 20080124-30.zip c:\test\30
c:\progra~1\winzip\wzunzip.exe -e -s 20080125-30.zip c:\test\30
c:\progra~1\winzip\wzunzip.exe -e -s 20080128-30.zip c:\test\30
c:\progra~1\winzip\wzunzip.exe -e -s 20080305-30.zip c:\test\30
c:\progra~1\winzip\wzunzip.exe -e -s 20080307-1.zip c:\test\1

What I think should occur is the location_system_mapping.txt is referenced and a new folder is created under c:\test\ with the location code on our side not what is in the zip. So if the zip location code is 1 and that is mapped to our location code of 8998767 then a folder with name 8998767 is created under c:\test\ or if it already is there then the zip is just unzipped to the existing folder.

Also, is there a way to get this all into one awk script I can make into an exe?

Hopefully all that makes sense.
 
Could you post your actual code please?

I wouldn't be surprised if wzunzip.exe can create the destination directories for you, perhaps by using a command-line switch. Failing that you can simply add if not exist dirname mkdir dirname commands to the batch file before each unzip operation.

I don't use awk under Windows and don't know anything about converting awk scripts to .exes, are you sure it's possible? Seems like unnecessary obfuscation to me when a .bat would do the job.



Annihilannic.
 
With your help and help from others I was able to get this working using the following scripts.

Batch file
Code:
@echo off 
dir /b *.zip | awk -f c:\test\test.awk c:\test\list.txt - > }{.cmd

test.awk
Code:
BEGIN{ 
        UnZipCommand1 = "c:\\progra~1\\winzip\\wzunzip.exe -e -s " 
        UnZipCommand2 = " c:\\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 
        } 



}

}{.CMD
Code:
@echo off
c:\progra~1\winzip\wzunzip.exe -e -s 20080305-30.zip c:\test\9896657
c:\progra~1\winzip\wzunzip.exe -e -s 20080307-1.zip c:\test\88998

Thanks for all your help and hopefully this helps someone else down the road.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top