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

how to batch move files date filename folder?

我有很多名为 YYYYMMDD_HHmm.jpg

的图像文件

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

谢谢

您可以使用 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 其中 returns 当前目录中除文件夹之外的所有文件。子字符串表示法是 !variable:~start,length!.

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

命令行提示符:

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