根据文件名中的日期创建批处理文件并移动文件 [英] create batch file based on date in file name and move files

查看:253
本文介绍了根据文件名中的日期创建批处理文件并移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的excel文件,它们的文件名都以这样的时间戳结尾:

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

examplefile_2018_08_24_110222.xlsx

我想根据时间戳的月份和年份移动所有这些文件,但是我希望文件夹名称为上个月.因此对于上面的示例,我想创建一个名为July2018的文件夹并将该文件移到该文件夹​​中.可以使用批处理文件来做到这一点吗?

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?

推荐答案

我认为这可以满足您的需要.我添加了一些注释,所以如果您不懂代码中的一行,请告诉我.

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\"
        )
    )
)

这篇关于根据文件名中的日期创建批处理文件并移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆