批处理脚本清理存储空间 [英] Batch script to clean up storage space
问题描述
我的文件夹结构如下:
- D:\文件夹
- test1
- opt
- test1.zip (10 MB)
- D:\folder
- test1
- opt
- test1.zip (10 MB)
- opt
- test2.zip (10 MB )
- opt
- test2.zip (10 MB)
- opt
- test3.zip (10 MB)
- opt
- test3.zip (10 MB)
同一文件在一个列表中
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
我有一个优化zip文件的脚本。我需要在批处理文件中做的基本上是在
opt
文件夹中找到这些优化的文件,然后用较小的文件覆盖较大的版本。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:
@echo off for /D %%I in ("D:\folder\*") do ( if exist "%%I\%%~nxI.zip" ( if exist "%%I\opt\%%~nxI.zip" ( call :CompareFiles "%%I\%%~nxI.zip" "%%I\opt\%%~nxI.zip" ) ) ) goto :EOF rem The loop runs on each subdirectory of directory D:\folder. It first rem checks if there is a *.zip file in the subdirectory with same name as rem the subdirectory. Next it checks if in the current subdirectory there rem is a subdirectory with name "opt" with having also a *.zip file with rem same name as the subdirectory. If this second condition is also true, rem the subroutine CompareFiles is called with the names of the 2 ZIP files. rem The subroutine compares the file size of the two ZIP files. rem The optimized ZIP file is moved over the ZIP file in directory rem above if being smaller than the ZIP file in directory above. rem Otherwise the optimized ZIP file being equal or greater as the rem ZIP file above is deleted. rem Finally the subdirectory "opt" is deleted which works only if the rem subdirectory is empty. The error message output by command RD in rem case of "opt" is not empty is redirected from STDERR to device NUL rem to suppress it. rem goto :EOF above results in exiting processing this batch file after rem finishing the loop and avoids a fall through to the subroutine. The rem goto :EOF below would not be really necessary as it is at end of the rem batch file. But it is recommended to end each subroutine with goto :EOF rem or alternatively exit /B in case of one more subroutine is added later. :CompareFiles if %~z1 GTR %~z2 ( move /Y %2 %1 ) else ( del /F %2 ) rd "%~dp2" 2>nul goto :EOF
您可以通过在命令
move
和<$ c $左侧插入命令echo
来测试批处理文件。 c> del 并在命令提示符窗口中运行批处理文件以查看输出。当结果符合预期时,再次运行批处理文件,而不添加两个echo
。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
.注意:
Windows命令处理器仅支持带符号的32位整数。因此,此批处理代码不适用于2 GiB(= 2.147.483.650字节)或更高的ZIP文件。
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
通常引用文件名和文件扩展名。 Windows命令处理器将最后一个反斜杠之后的所有内容解释为文件或目录的名称。在这里,分配给循环变量I
的字符串是驱动器和路径为D:\folder\
不以反斜杠结尾。因此,%%〜nI
在D:\folder\
中引用当前子目录的名称。文件扩展名定义为最后一点之后的所有内容。目录通常没有目录名,因此%%〜nI
通常也足以作为目录名。但是也可以使用目录名称中的点来创建目录。因此,使用%%〜nxI
可以更安全地使用任何目录名称。%%~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.注意:具有隐藏或系统属性的子目录将被命令 FOR 忽略。
Note: Subdirectories with hidden or system attribute are ignored by command FOR.
仅使用
CompareFiles
中的>%1 和%2
而不是%〜1
和%〜2
,因为两个文件名都必须已经用双引号传递给包含空格的子例程或以下字符之一:&()[] {} ^ =;!'+,`〜
。因此,从执行角度来看,在move
和del
中指定参数(文件名)没有意义。和%〜1
和%〜2
。但是当然可以使用%〜1
和%〜2
来获得更好的语法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.在不测试两个ZIP文件是否存在的情况下,可以简化该批处理文件。
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.
@echo off for /D %%I in ("D:\folder\*") do ( move /Y "%%I\opt\%%~nxI.zip" "%%I\%%~nxI.zip" 2>nul rd "%%I\opt" 2>nul )
将优化的ZIP文件不存在的错误消息输出通过将其重定向到设备 NUL 而得到抑制。
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.
-
通话/?
-
del /?
-
echo /?
-
用于/?
-
转到/?
-
如果/?
-
移动/?
-
rd /?
call /?
del /?
echo /?
for /?
goto /?
if /?
move /?
rd /?
另请参阅Microsoft文章使用命令重定向运算符以了解详细信息在
2> nul
上。See also the Microsoft article Using command redirection operators for details on
2>nul
.这篇关于批处理脚本清理存储空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- opt
- test1
- opt
- test1