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

后端 未结 2 1694
难免孤独
难免孤独 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:11

    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.

提交回复
热议问题