4

バックアップ ディレクトリを含むすべてのディレクトリをループし、その中の 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
4

2 に答える 2

11

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

%作成した変数の'sを'sに置き換えると、!代わりに遅延展開を使用するように通知されます。

于 2012-09-14T11:17:20.480 に答える
1

バリさんの回答は少しミスがあります。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
于 2012-09-15T02:51:59.760 に答える