create batch file based on date in file name and move files

后端 未结 2 1692
难免孤独
难免孤独 2021-01-24 10:53

I have a large number of excel files with filenames that all end in a timestamp that looks like this:

examplefile_2018_08_24_110222.xlsx

I would like to move all

2条回答
  •  北海茫月
    2021-01-24 11:25

    I think this will do what you need it to do. I added some comments so please let me know if you do not understand a line of the code.

    @ECHO OFF
    
    REM get a list of the files
    FOR %%F IN (*.xlsx) DO (
        REM GET 2nd, 3rd and 4th parts of file name: examplefile_2018_08_24_110222.xlsx
        FOR /F "tokens=2,3,4 delims=_" %%G IN ("%%~F") DO (
            REM GET previous month and/or year
            FOR /F "delims=" %%J IN ('powershell "(Get-Date %%H/%%I/%%G).AddMonths(-1).ToString('MMMMyyyy')"') DO (
                REM make the directory
                md "%%J" >nul 2>&1
                REM move the file
                move "%%~F" "%%J\"
            )
        )
    )
    

提交回复
热议问题