如何批量移动文件日期文件名文件夹?

时间:2015-04-12 20:20:07

标签: date batch-file directory filenames move

我有很多名为YYYYMMDD_HHmm.jpg的图像文件

如何将这些文件移动到:target_directory \ YYYY \ MM \ YYYYMMDD_HHmm.jpg?

谢谢

2 个答案:

答案 0 :(得分:3)

您可以使用for循环,子字符串和mkdir

@echo off
setlocal enabledelayedexpansion

for /f %%a in ('dir /b /a-d') do (
    set filename=%%a

    ::exclude this file
    if not "!filename!" == "%~nx0" (

        ::substr to grab characters 0-4 and 4-6 as year and month
        set year=!filename:~0,4!
        set month=!filename:~4,2!

        ::make dirs for the files if they don't already exist
        if not exist !year! mkdir !year!
        if not exist !year!\!month! mkdir !year!\!month!

        ::move the files there
        move !filename! !year!\!month!
    )
)

for循环运行dir /b /a-d,它返回当前目录中除文件夹之外的所有文件。子串表示法是!variable:~start,length!

What is the best way to do a substring in a batch file?

答案 1 :(得分:0)

命令提示符的单行:

for %a in (*.jpg) do @set "FName=%~a"&call xcopy "%FName%" "%FName:~0,4%\%FName:~4,2%\"&&del "%~a"&set "FName="