Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file Windows CMD-for循环中的设置不起作用_Batch File_Cmd - Fatal编程技术网

Batch file Windows CMD-for循环中的设置不起作用

Batch file Windows CMD-for循环中的设置不起作用,batch-file,cmd,Batch File,Cmd,我想写一个批处理文件,它将遍历包含备份目录的所有目录,并删除其中早于X天的文件。 在我想要运行脚本的计算机上,并没有“forfile”命令。 没有PowerShell,所以CMD或VBScripts似乎是完成此任务的唯一方法 目前,我对“set”语句有问题-当我调用%checkpath%时,似乎没有收到预期的文件夹 rem we will memorize current directory pushd %cd% set folder="C:\Documents and Settings\myn

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

%
替换为
,将向它发出使用延迟扩展的信号。

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

::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
可能重复的