我想编写批处理文件,该文件将遍历包含备份目录的所有目录并删除其中早于 X 天的文件。
在我想运行我的脚本的计算机上,没有“forfile”命令。
没有 PowerShell,因此 CMD 或 VBScripts 似乎是完成此任务的唯一方法。
目前我对“set”语句有问题 - 似乎当我调用 %checkpath% 时我没有收到预期的文件夹。
rem we will memorize current directory
pushd %cd%
set folder="C:\Documents and Settings\myname\Desktop"
cd %folder%
rem loop only folders with five chars within their names (unfortunately on less also
for /D %%g in (?????) DO (
set checkpath="%cd%\%%g\backup"
if exist %checkpath% (
for %%a in ('%%g\backup\*.*') do (
set FileDate=%%~ta
set FileDate=%%FileDate:~0,10%%
rem here I want to compare file modification data with current date
)
)
popd
pause
最佳答案
您需要使用延迟扩展来读取您在 for
循环中设置的变量。
试试这个
setlocal enabledelayedexpansion
rem we will memorize current directory
pushd %cd%
set folder="C:\Documents and Settings\myname\Desktop"
cd %folder%
rem loop only folders with five chars within their names (unfortunately on less also
for /D %%g in (?????) DO (
set checkpath="%cd%\%%g\backup"
if exist !checkpath! (
for %%a in ('%%g\backup\*.*') do (
set FileDate=%%~ta
set FileDate=!%FileDate:~0,10%!
rem here I want to compare file modification data with current date
)
)
popd
pause
在您创建的变量上用
%
替换 !
将指示它使用延迟扩展。关于batch-file - Windows CMD - 在 for 循环中设置不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12423238/