Windows CMD - set within for loop is not working

前端 未结 2 494
谎友^
谎友^ 2020-12-19 17:14

I want to write batch file which will loop through all directories containing backup directory and remove files older than X days within it. On computer which I want run my

2条回答
  •  庸人自扰
    2020-12-19 17:36

    You need to use delayed expansion to read a variable that you have set inside a for loop.

    Try this instead

    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
    

    Replacing the %'s with !'s on variables you have created will signal it to use delayed expansion instead.

提交回复
热议问题