Trying to create a Bath file that will copy a specific directory path (no lower) and zip the directories in that path

815 views Asked by At

So I have a directory structure that contains log files. The path is something like the following:

C:\Path\TenantName\AppName\other\stuff\I\dont\care\about

What I need to do is to ZIP the AppName directory and everything under it into a ZIP file for each AppName under each Tenant folder.

So example is:

C:\Path\TenantName1\AppName1\
C:\Path\TenantName1\AppName2\
C:\Path\TenantName2\AppName1\
C:\Path\TenantName2\AppName2\

Would be (after the zip):

C:\Path\TenantName1\yyyy.mm.dd_AppName1.zip
C:\Path\TenantName1\yyyy.mm.dd_AppName2.zip
C:\Path\TenantName2\yyyy.mm.dd_AppName1.zip
C:\Path\TenantName2\yyyy.mm.dd_AppName2.zip

We are then moving the files to a Temp Directory that needs to have the following structure as well:

C:\Temp\TenantName\<ZipFiles>

The real issue for me is that I need to dynamically create the TenantName directory within C:\Temp as these will change. Once the files are in the C:\Temp directory, I have Rsync moving them to a centralized server for long term storage.

Any help would be appreciated.

I already for the zip files being created. Using the following command:

for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "C:\Temp-Log\%DATE:~-4%.%DATE:~7,2%.%DATE:~4,2%_%%X.zip" "%%X\"
2

There are 2 answers

6
PA. On

you are almost there, you can repurpouse your FOR command to create the folder structure

try this oneliner in the command line....

for /d %d in (*) do @echo md c:\temp\%d
0
Robert Addison On

Here is what I developed at the end.

for /d %%d in (*) do (
    md c:\temp\%%d
    cd %%d
    for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "C:\Temp\%%d\%DATE:~-4%.%DATE:~7,2%.%DATE:~4,2%_%%X.zip" "%%X\"
    cd ..
)   

This loops through a directory and makes a temporary directory structure copy and zips everything underneath their parent directory.