|
Answer» Hello,
I have the following structure: folder (level 1), contains many subfolders (level 2), each contains 3 sub folders (level 3) having names (fol1, fol2 and fol3), each folder in level 3 contains files.
structure: Main folder ---> contains folders (sub1, sub2, sub3, sub4.......) ------> each sub contains (fol1, fol2 and fol3)
What I want to do is: compress all files (to 7z) in fol1 from each folder in level 2.
I tried the following code:
@echo off setlocal enabledelayedexpansion
cd "C:\MainFolder" for /f "delims==" %%l in ('dir /b /ad') do ((pushd %%l\fol1) && (for /f "delims==" %%b in ('dir /b /a-d') do ((pushd "c:\program files\7-zip") && (7z a -t7z "%%l\fol1\%%b.7z" -ssw "%%l\fol1\%%b" -m0=LZMA:d=64m:mt=2:fb=64 -mx=9 -ms=4g)) && (popd)) && (popd))
but it doesn't work, can any one HELP me?
Why all those && symbols? After CLIMBING through all the smoke and dust, It seems to look like this:
@echo off setlocal enabledelayedexpansion
cd "C:\MainFolder" for /f "delims==" %%l in ('dir /b /ad') do ( pushd %%l\fol1 for /f "delims==" %%b in ('dir /b /a-d') do ( pushd "c:\program files\7-zip" 7z a -t7z "%%l\fol1\%%b.7z" -ssw "%%l\fol1\%%b" -m0=LZMA:d=64m:mt=2:fb=64 -mx=9 -ms=4g popd popd ) )
What does that red "a" represent? Is that a switch? a typo?
PS. When writing batch code readability counts for more than saving space.OP seems confused about the difference between & and &&.Hello,
These symbols && mean (and) , and the red a in "7z a -t7z "%%l\fol1\%%b.7z" -ssw "%%l\fol1\%%b" -m0=LZMA:d=64m:mt=2:fb=64 -mx=9 -ms=4g" mean (add) to 7z, others in yellow are the parameters that 7z take;
I test the same function on echo (not on add to 7z) as the following:
@echo off setlocal enabledelayedexpansion
cd "C:\MainFolder" for /f "delims==" %%l in ('dir /b /ad') do ((pushd %%l\fol1) && (echo %cd%>>C:\test.txt) && (popd))
Result, If we have 2 folders in the main folder, test.txt will be as the following
C:\MainFolder C:\MainFolder
It should be:
C:\MainFolder\sub1\fol1 C:\MainFolder\sub2\fol1
I will appreciate any help,
for 7z parameters, you can visit 7z help if you install it on your PC 1. && symbols represent the BOOLEAN AND. That is, in this statement:
Operation1 && Operation2
Operation2 will be executed only if Operation1 completed successfully
The SINGLE & is a command continuation character. In this statement:
Operation1 & Operation2
Operation1 will always execute, and when it has completed, Operation2 will always be executed.
2. I see that delayed expansion is enabled, yet nowhere is it used!
3. I think you are trying to be too clever with too many things all at once. Keep it simple and readable, as SIDEWINDER suggests. Hello,
It works now,
all what I did: add the full path (i.e. add (pushd MainFolder\%%l\fol1) instead of (pushd %%l\fol1))
Thanks
|