I have a .txt file which contains a very long generated list of third level folders for which I want to zip up the contents of each one. So a simplified example, this would be the contents of list.txt
F:\Folder\2005-05-11\[ABC #1] FolderA
F:\Folder\2005-05-11\[ABC #2] FolderB
F:\Folder\2005-05-26\[ABC #1] FolderA
If I do this
for /F "delims=" %%X in (list.txt) do "7z.exe" a -mx=0 "%%X.zip" "%%X\*"
The filename of each zip is just the final directory that is being zipped. So [ABC #1] FolderA.zip and so on. All of these zips are going to be later moved out of this context and so I need to more clearly name them.
In an absolutely ideal world I'd like them to take the 2nd level name (the date) and just the part in square brackets from the last folder (Which is always consistent). So 2015-05-11 [ABC #1].zip but I don't know if that is possible in some way.
The seemingly easier option therefore is just including that parent directory onto the zip, so 2005-05-11 [ABC #1] FolderA.zip, how could I do that?
Obviously I'm using 7zip in my current script but if it's easier with WinRAR or another tool that's fine.
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q42060809.txt"
for /F "usebackqdelims=" %%X in ("%filename1%") do (
FOR /f %%d IN ("%%~dpX.") DO (
FOR /f "delims=[]" %%b IN ("%%~nX") DO (
ECHO("7z.exe" a -mx=0 "%%~dpX%%~nxd[%%b].zip" "%%X\*"
)
)
)
GOTO :EOF
You would need to change the setting of sourcedir
to suit your circumstances.
I used a file named q42060809.txt
containing your data for my testing.
The required commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, remove the ECHO(
to actually zip the files.
Naturally, you don't need the usebackq
if you don't "quote the filename"
Step 2 - append .
to the ~dp
date and path portions of %%X
. The result in %%~nxd
is the Name and eXtension of the lowest level of directory.
Then, with the ~n
(Name) portion only of %%X
, tokenise it using the brackets as delimiters, so the part between the brackets will appear in %%b
Then mix and match the appropriate elements. Re-insert the brackets if required and add a space before the open bracket if that suits your fancy...