0

I have a task about creating a script to move files in a folder to follow the last modified date. I have done the task with the below script:

@echo off

set "source=C:\Users\xxxxx\test-bash\ARCHIVE"

for %%F in ("%source%\*") do (
  for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
        if not exist "%source%\%%C_%%A" mkdir "%source%\%%C_%%A"
        move "%%~fF" "%source%\%%C_%%A"
    )
)

pause

The issue happens when I try to move the files only before the current time 2 months. Example: If the current time is: 20/4/2021, only files from backward 1/2021 will be moved to folder. I have tried to set %%A and %%C to handle some case but It can't run properly, like this:

set /a file_year = %%C

Could please explain why I can't assign this value? Thank you so much!

Koro
  • 9
  • 1
  • 1
    The `~t`-modifier returns the last modification date (depending on the locale settings) but not the creation date, though the former makes more sense in most situations anyway. Anyway, take a look at [`forfiles`](https://ss64.com/nt/forfiles.html) and particularly its `/D` option… – aschipfl Apr 20 '21 at 09:21
  • You don't use `file_year` in your code (or you didn't show), but I have a strong feeling, you fell into the [delayed expansion trap](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Apr 20 '21 at 14:11

0 Answers0