0
votes

i have writing a window batch to del the files order than 3 months in some directory, however, there are some problem on :Loop_Folder_Del_Old_Files. I don't know why i cannot assign the last modified date of the files, please help to find out the problem. Below are the source code and program result, thanks!

Source Code

:program_start
@ECHO ON
echo program_start

:Parameter_Settings
set filePath=c:\New Folder
set delPeriod=3
echo %date%
set curYYYY=%date:~10,4%
set curMM=%date:~7,2%
set curDD=%date:~4,2%

:Set_The_Date_Of_3_Months_Ago
set /A curMM=curMM - %delPeriod%
if "%curMM%" LEQ "0" (
        set /A curMM="(curMM + 12 - %delPeriod%)%%12"
        
if %curMM% == 0 (set curMM=12) ELSE (set curMM=%curMM%)
        set /A curYYYY=curYYYY - 1
)
set curMM=00%curMM%
set curMM=%curMM:~-2%

set curDate=%curYYYY%%curMM%%curDD%

:Loop_Folder_Del_Old_Files
for %%a IN ("%filePath%\*.*") DO (
        set ltdate=%%~ta
        set fileDate=%ltdate:~6,4%%ltdate:~3,2%%ltdate:~0,2%
        if "%fileDate%" LSS "%curDate%" Del /Q "%%a"
)
:end
echo program end
pause

Result

C:\>echo program_start
program_start

C:\>set filePath=c:\New Folder

C:\>set delPeriod=3

C:\>echo Fri 22/11/2013
Fri 22/11/2013

C:\>set curYYYY=2013

C:\>set curMM=11

C:\>set curDD=22

C:\>set /A curMM=curMM - 3

C:\>if "8" LEQ "0" (
set /A curMM="(curMM + 12 - 3)%12"
if 8 == 0 (set curMM=12 )  ELSE (set curMM=8 )
set /A curYYYY=curYYYY - 1
)

C:\>set curMM=008

C:\>set curMM=08

C:\>set curDate=20130822

C:\>for %a IN ("c:\New Folder*.*") DO (
set ltdate=%~ta
set fileDate=~6,4%ltdate:~3,2%ltdate:~0,2
if "" LSS "20130822" Del /Q "%a"
)

C:\>(
set ltdate=22/11/2013 05:36 PM
set fileDate=~6,4%ltdate:~3,2%ltdate:~0,2
if "" LSS "20130822" Del /Q "c:\New Folder\New Text Document (2).txt"
)

C:\>(
set ltdate=22/11/2013 05:36 PM
set fileDate=~6,4%ltdate:~3,2%ltdate:~0,2
if "" LSS "20130822" Del /Q "c:\New Folder\New Text Document.txt"
)

C:\>echo program end
program end

C:\>pause
Press any key to continue . . .

3
Can we use PowerShell? This would be, IMHO, easier to write and easier to maintain in PowerShell. Modern Windows systems come with PowerShell already. - chwarr
To format code, simply hilight the code and press the {} - Magoo
Where did the HTML come from? - foxidrive
@foxi OP's brave attempt at formatting. Batch code carried the same structures until a concerned editor went beyond the call...:) - Magoo

3 Answers

1
votes

maybe the use of forfiles to delete old files could be better in handle?

0
votes

You need to enable delayed expansion to make your variables work inside the for loop.

Add

 setlocal enabledelayedexpansion

at the top of your bat file

and then, refer to the variable using the !var! syntax (instead of %var%)

 if "!fileDate!" LSS "!curDate!" Del /Q "%%a"
0
votes

Further to PA's correct response,

set /A curMM=curMM - %delPeriod%
if "%curMM%" LEQ "0" (
        set /A curMM="(curMM + 12 - %delPeriod%)%%12"
        if %curMM% == 0 (set curMM=12) ELSE (set curMM=%curMM%)
        set /A curYYYY=curYYYY - 1
)

Is unreliable as currMM may be 08 or 09 which would generate a syntax-error as batch assumes that any numeric starting with 0 is OCTAL.

set /A curMM=1%curMM% - 100 - %delPeriod%
if %curMM% LEQ 0 (
        set /A curMM=curMM + 12
        set /A curYYYY=curYYYY - 1
)

is better. The comparison is executed against the NUMERIC value, not a string value. The value of curMM would be 0 for Dec, -1 for Nov, etc - so simply adding 12 and subtracting 1 from the year is sufficient and clearer.