2012-09-14 6 views
3

백업 디렉토리가있는 모든 디렉토리를 반복하고 배치 된 X 일보다 오래된 파일을 제거하는 배치 파일을 작성하고 싶습니다. 내 스크립트를 실행하고 싶은 컴퓨터에 "forfile"명령이 없습니다. PowerShell이 ​​없으므로 CMD 또는 VBScript가이 작업을 수행하는 유일한 방법 인 것처럼 보입니다.Windows CMD - for 루프 내에서 작동하지 않습니다.

현재 "설정"문에 문제가 있습니다 - 전화 할 때 % 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 
+0

가능한 중복 왜이 배치를 수행

::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 
변수가 설정 되어도 변경되지 않습니다?] (https://stackoverflow.com/questions/3949978/why-does-this-batch-variable-never-change-even-when-set) –

답변

8

지연 확장을 사용하여 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 

%!와 '의 대신 지연 확장을 사용하도록 신호를 보냅니다 만든 변수에 대한의.

1

발리의 대답은 약간의 실수가 있습니다. 두 번째 set filedate은 올바르지 않습니다. 그렇지 않으면 그의 확장은 유효하지만 지연 확장을 사용 가능하게하지 않으면 작동하지 않을 수 있습니다. 나는 실수를 수정하고 지연된 확장을 보장하는 방법을 보여주었습니다. 이처럼 작성하는 경우에는 "SETLOCAL"또는 지연 확장을하지 않아도,

::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 

:하지만 나는 또한 다른 변경 한

[의
관련 문제