2012-09-14 25 views
3

我想寫一個批處理文件,它將遍歷包含備份目錄的所有目錄,並刪除其中超過X天的文件。 在我想運行我的腳本的計算機上,沒有「forfile」命令。 沒有PowerShell,因此CMD或VBScripts似乎是完成此任務的唯一方法。Windows CMD - 在for循環中設置不起作用

目前我有「設置」語句的問題 - 似乎當我打電話%檢查路徑%我沒有收到預期的文件夾。

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

的[可能的複製爲什麼此批變量永遠不會改變,即使設置?](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不正確,否則他很好,但如果您沒有啓用延遲擴展,則可能無法正常工作。我修正了他的錯誤,並告訴你如何確保延遲擴展已啓用。我還做了一些其他的變化:

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