Dakine83
Dakine83

Reputation: 707

Using a Batch File to clean up a directory by date

I am working on a batch file to read through the files in a folder, and if they are older than 4 days old, move them into an archive\YYYY\MM folder structure. Here's the code as it stands

::MOVE FILES THAT ARE IN THE ERROR FOLDER TO ARCHIVE ACCORDING TO FILES YEAR AND MONTH

@echo off

set "source=C:\Users\user\Desktop\test"
set "targetRoot=C:\Users\user\Desktop\test\archive"
For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
    set mm=%%A
    set dd=%%B
    set yyyy=%%C
)
set currdate=%yyyy%%mm%%dd%
::echo %currdate%
set /a currdate-=7
::echo %currdate%
for %%F in ("%source%\*") do (
    for /f "tokens=1,2,3 delims=/ " %%D in ("%%~tF") do (
    SET fileDT=%%F%%D%%E

    if /I %currdate% GTR %fileDT%  (
        if not exist "%targetRoot%\%%F" mkdir "%targetRoot%\%%F"
        if not exist "%targetRoot%\%%F\%%D" mkdir "%targetRoot%\%%F\%%D"
        move "%%~fF" "%targetRoot%\%%F\%%D"
        )

    )
)

the problem is that after I added the

if /I %currdate% GTR %fileDT%
line, it no longer knows what %%~fF is, and therefore which file to move.

I should note I'm brand new to batch files and I'm mostly modifying code I've found online.

Upvotes: 0

Views: 1549

Answers (2)

Aacini
Aacini

Reputation: 67216

The problem is in these two lines:

for %%F in ("%source%\*") do (
    for /f "tokens=1,2,3 delims=/ " %%D in ("%%~tF") do (

The first FOR use %%F replaceable parameter, but the second FOR use %%D, %%E and %%F parameters (the %%D is explicit and %%E and %%F are implicit because the TOKENS=1,2,3), so the first meaning of %%F is lost. Just change one of the two FOR parameters, for example:

for %%F in ("%source%\*") do (
    for /f "tokens=1,2,3 delims=/ " %%X in ("%%~tF") do (
    SET fileDT=%%Z%%X%%Y

EDIT: Answer to second question stated in comment.

In this line:

if /I %currdate% GTR %fileDT%  (

the %currdate% value is constant for all the values in the FOR, but %fileDT% changes with every value in the FOR. In order for this to work, that is, to get the current value of a variable that has changed inside a FOR or IF or parentheses, the Delayed Variable Expansion must be used, that is:

if /I %currdate% GTR !fileDT!  (

and you must include this line at beginnng of the program:

setlocal EnableDelayedExpansion

Upvotes: 2

John Einem
John Einem

Reputation: 51

what I think is that the %%~fF has fallen out of scope maybe there is some way of making it global or something? I am sorry I am pretty new to batch file processing So I hope I don't steer you wrong or something, well good luck I tried anyways...

It could be that in your code the part where it increments that variable is not being executed at all or the new data is not being entered because after the first loop , it keeps skipping the instruction that changes that value... or does not hit it at all, check to see where you are utilizing that part and if some index needs to increment make sure it does and is inside the loop, and is sure to get hit once each time, I hope this helps... :)

Upvotes: 1

Related Questions