将cmd compact与文件夹名称模式一起使用

时间:2019-12-27 12:11:41

标签: windows cmd

我的文件夹结构如下:

enter image description here

如何仅在名称以“ Holder”结尾的文件夹上使用compact /s /c来递归压缩这些文件夹中的所有文件?谢谢

1 个答案:

答案 0 :(得分:0)

Windows 10 64位

使用compact和cmd for loop设置文件夹和/或文件的compress属性。

为每个名称以holder结尾的文件夹设置compress属性。压缩文件夹名称以holder结尾的每个文件夹中的非系统文件或隐藏文件。如果COMPACT遇到错误,则停止。

CMD:

FOR /f "tokens=*" %g in ('DIR /ad /b /s "*holder"') do COMPACT /c "%g" 
FOR /f "tokens=*" %g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %h in ('DIR /a-d /b "%g"') do COMPACT /c "%g\%h"
rem un-set un-compress
FOR /f "tokens=*" %g in ('DIR /ad /b /s "*holder"') do COMPACT /u "%g" 
FOR /f "tokens=*" %g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %h in ('DIR /a-d /b "%g"') do COMPACT /u "%g\%h"

脚本:

FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do ECHO COMPACT /c "%%g" 
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %%h in ('DIR /a-d /b "%%g"') do ECHO COMPACT /c "%%g\%%h"
rem un-set un-compress
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do ECHO COMPACT /u "%%g" 
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %%h in ('DIR /a-d /b "%%g"') do ECHO COMPACT /u "%%g\%%h"

测试脚本:

FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do COMPACT /c "%%g" 
echo. 
echo Set compress attribute of folders whose name ends in holder. 
echo.
pause 
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %%h in ('DIR /a-d /b "%%g"') do COMPACT /c "%%g\%%h"
echo. 
echo Compress and set compress attribute of files in folders whose folder name ends in holder. 
echo.
pause 
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do COMPACT /u "%%g" 
echo. 
echo Unset compress attribute of folders whose name ends in holder.
echo.
pause 
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %%h in ('DIR /a-d /b "%%g"') do COMPACT /u "%%g\%%h"
echo. 
echo Un-compress and un-set compress attribute of files in folders whose folder name ends with holder.
exit /b