Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a unique situation where I need the following folder structure (Windows)




Folders are created every morning.

Essentially I'd like to zip the contents of the date subdirectory and place it within that directory. Should this be done with 7z or with 'FORFILES'?

share|improve this question
What part of the path changes every day? Will there always be a X:\Logs\HS\YYYYMMDD folder? Are you wanting to take the YYYYMMDD folder and zip it up inside a new folder X:\LOGS\PRIMARY\YYYYMMDD, where the last folder is dynamically created? – Brett Oct 11 '12 at 21:35
Directories get created every day under X:\LOGS\HS*. I want to take the log files that get dumped in the newly created folder and zip them inside that same folder – Tal Oct 12 '12 at 13:17

If I understand correctly, you want to take a specific folder that is created in a YYYYMMDD format and zip up the contents. The generated zip file needs to be placed in this folder as well.

At the most basic level you can accomplish this with a command like the following

"c:\Program Files\7-Zip\7z.exe" a x:\Logs\HS\20121010\ X:\Logs\HS\20121010\*

NOTE: 7-zip must be installed to use the above command

With the above command you would have to manually fill in the YYYYMMDD every time you want to zip up a new date folder.

This can be taken a step further and you can modify the command to use the current YYYYMMDD for today's date. The key to this is to use the %date% variable in DOS. You can read more information about this variable by typing set /?. To format the date as YYYYMMDD we will need to take a sub-string of the %date% variable like so:

echo %date:~10,4%%date:~4,2%%date:~7,2%

The above command should output the date in YYYYMMDD.

Last we can put these two commands together to generate a one line command that will zip up all the files in the directory with today's date (in YYYMMDD format).

"c:\Program Files\7-Zip\7z.exe" a x:\Logs\HS\%date:~10,4%%date:~4,2%%date:~7,2%\%date:~10,4%%date:~4,2%%date:~7, X:\Logs\HS\%date:~10,4%%date:~4,2%%date:~7,2%\*

One additional step can be taken to automate this by adding the above command as a scheduled task in windows.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .