I am working on a build system. The build system posts the results as a zip file in a directory. Unfortunately I have no easy way to know the name of the zip file, because it is timestamped. For the next operation, I must decompress this zip file to some specific location and then do some more file operations.
I guess I could change the build system so I specify the name of the result zip file from the command line, however, I though it might be easiest just to find out which one is the newest file and unzip it (if the previous process is successful).
How can I issue an unzip command that will only take effect on the newest zip file in the directory, ignoring all others?
EDIT: I decided to use the capabilities in ANT for this task instead. However, it is still a neat trick to have up the sleve… Thanks for the answer!
This should do it:
This works as follows:
DIR /B /O-D *.ZIPlists all ZIP files in reverse date order in a ‘bare’ – i.e. name only – format.FOR /F usebackqis used to loop over the output of the command.&& GOTO DONE || GOTO DONEmakes sure theUNZIPis only run for the first file. You need both&&(and) and||(or) in case the unzip fails for some reason.You’ll need to change
UNZIP %%ifor whatever unzip command you want to use.EDIT The above will work as long as the Zip filename doesn’t contain any spaces. If you want to handle filenames with spaces, use the following variant:
The differences are:
The ‘tokens=*’ option returns the whole of the filename even if it contains spaces.
The filename passed to UNZIP is quoted
This variant uses single quotes for the DIR command so doesn’t need the ‘usebackq’ option.