1

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 of these files based on the month and year of the timestamp, but I would like the folder name to be the previous month. So for the above example, I would want to create a folder named July2018 and move that file into that folder. Is it possible to do this with a batch file?

2
  • 1
    So are you saying you want to rename the file: examplefile_2018_08_24_110222.xlsx to July2018.xlsx and move it into a folder named July2018? That is how I am interpreting your description. Commented Jun 11, 2018 at 19:56
  • no sorry I mistyped -- edited my post to fix that. I don't need to rename the file. I want to make a folder based on the date in the file name, but name the folder the previous month Commented Jun 11, 2018 at 20:04

2 Answers 2

2

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\"
        )
    )
)
Sign up to request clarification or add additional context in comments.

1 Comment

that worked perfectly, thank you! Thanks for the comments in the code -- I'm VERY new to batch scripts and the breakdown helped a lot
0

You should be able to follow this previous question: How to rename file by replacing substring using batch in Windows

That question is more of a "find and replace" batch file question but might serve you well. In your case, it seems like you may want to find and replace:

2018_08 with July2018,

2018_09 with August2018

etc.

The issue here is the amount of cases and loops needed.


For example this would be 3 months:

@echo off
Setlocal enabledelayedexpansion

Set "Location=C:\Users\Example"

Set "Pattern=2018_08"
Set "Replace=July2018"

For %%# in ("%Location%\*.xlsx") Do (
    Set "File=%%~nx#"
    Ren "%%#" "!File:%Pattern%=%Replace%!"
)

Set "Pattern=2018_09"
Set "Replace=August2018"

For %%# in ("%Location%\*.xlsx") Do (
    Set "File=%%~nx#"
    Ren "%%#" "!File:%Pattern%=%Replace%!"
)

Set "Pattern=2018_10"
Set "Replace=September2018"

For %%# in ("%Location%\*.xlsx") Do (
    Set "File=%%~nx#"
    Ren "%%#" "!File:%Pattern%=%Replace%!"
)

Pause&Exit

Note that C:\Users\Example would be replaced with your desired folder.

1 Comment

thank you for your answer -- I mistyped in my question. I'm not trying to change the filename but rather use the filename to get the date, then create a folder that is the month previous to the one in the filename. Then I want to move that file into that folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.