I have a folder structure like below:
- D:\folder
- test1
- opt
- test1.zip (10 MB)
- test1.zip (100 MB)
- opt
- test2
- opt
- test2.zip (10 MB)
- test2.zip (100 MB)
- opt
- test3
- opt
- test3.zip (10 MB)
- test3.zip (100 MB)
- opt
- test1
Same files in a flat list:
D:\folder\test1\test1.zip
D:\folder\test1\opt\test1.zip
D:\folder\test2\test2.zip
D:\folder\test2\opt\test2.zip
D:\folder\test3\test3.zip
D:\folder\test3\opt\test3.zip
I have a script that optimizes zip files. What I need to do in a batch file is to basically find these optimized files in opt
folders and overwrite the larger version with the smaller one.
Take a look on this commented batch code:
You can test the batch file by inserting command
echo
left to the commandsmove
anddel
and run the batch file from within a command prompt window to see the output. When the result is as expected, run the batch file once again without the two addedecho
.ATTENTION:
Windows command processor supports only signed 32-bit integer numbers. So this batch code does not work for ZIP files with 2 GiB (= 2.147.483.650 bytes) or more.
%%~nxI
references usually file name and file extension. Windows command processor interprets everything after last backslash as name of a file or directory. Here the string assigned to loop variableI
is the name of the subdirectory with drive and pathD:\folder\
not ending with a backslash. For that reason%%~nI
references the name of the current subdirectory inD:\folder\
. The file extension is defined as everything after last point. Directories usually don't have a point in directory name and so%%~nI
is often also enough for a directory name. But it is possible to create directories also with a point in directory name. Therefore using%%~nxI
is more safe as working for any directory name.Note: Subdirectories with hidden or system attribute are ignored by command FOR.
It is 100% safe to use just
%1
and%2
in subroutineCompareFiles
instead of"%~1"
and"%~2"
as both file names must be passed already enclosed in double quotes to the subroutine on containing a space or one of these characters:&()[]{}^=;!'+,`~
. So it does not make sense from an execution point of view to specify onmove
anddel
the arguments (file names) with"%~1"
and"%~2"
. But it is of course possible to use"%~1"
and"%~2"
for example for better syntax highlighting in text editor or for uniformed file name references passed as arguments to a batch file or subroutine.The batch file can be simplified on not testing if the two ZIP files exist at all and the optimized ZIP file is really smaller.
The error message output in case of optimized ZIP file not existing is suppressed by redirecting it to device NUL.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
del /?
echo /?
for /?
goto /?
if /?
move /?
rd /?
See also the Microsoft article Using command redirection operators for details on
2>nul
.