【问题标题】:Windows CMD - set within for loop is not workingWindows CMD - 在 for 循环中设置不起作用
【发布时间】:2012-09-07 13:02:02
【问题描述】:

我想编写批处理文件,它将遍历包含备份目录的所有目录并删除其中超过 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

【问题讨论】:

标签: batch-file cmd


【解决方案1】:

您需要使用延迟扩展来读取您在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

将您创建的变量上的% 替换为! 将指示它改为使用延迟扩展。

【讨论】:

    【解决方案2】:

    巴厘岛的回答有一个小错误。第二个set filedate 不正确,否则他很好,但如果您没有启用延迟扩展,则可能无法正常工作。我修正了他的错误并向您展示了如何确保启用延迟扩展。我还做了一些其他更改:

    ::This command will ensure that the delayed expansion, i.e. the "!"s below, 
    ::  will work.  Unfortunately, it also means you loose the results of any
    ::  "set" commands as soon as you execute the "endlocal" below.
    setlocal ENABLEDELAYEDEXPANSION
    
    ::you might want
    ::set "folder=%USERPROFILE%\Desktop"
    set "folder=C:\Documents and Settings\myname\Desktop"
    
    rem we will memorize current directory
    ::If you ran this code with the current directory set to a directory on
    ::a drive other than C:, your previous code would not have worked to
    :: change to your desired target directory.  this slight change fixes that.
    pushd "%folder%"
    
    rem loop only folders with five chars within their names - unfortunately on less also
    ::Use capitals for your loop variables.  The var names are case sensitive, and 
    ::using capitals ensures there is no confusion between the var names and ~modifiers
    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
    endlocal
    pause
    

    但是,如果你这样写,你就不需要“setlocal”或延迟扩展:

    ::you might want
    ::set "folder=%USERPROFILE%\Desktop"
    set "folder=C:\Documents and Settings\myname\Desktop"
    
    rem we will memorize current directory
    ::If you ran this code with the current directory set to a directory on
    ::a drive other than C:, your previous code would not have worked to
    :: change to your desired target directory.  this slight change fixes that.
    pushd "%folder%"
    
    rem loop only folders with five chars within their names - unfortunately on less also
    for /D %%G in ( ????? ) DO (
        if exist "%%~G\backup" (
            for %%A in ('%%~G\backup\*.*') do (
                for /F "usebackq" %%T in ( '%%~tA' ) do (
                    echo File date is %%T, todays date is %DATE%
                )
            )
    )
    popd
    pause
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 2017-04-13
      相关资源
      最近更新 更多