how to batch move files date filename folder?

随声附和 提交于 2019-12-12 06:12:52

问题


I have lots of image files named as YYYYMMDD_HHmm.jpg

How can I move these files to: target_directory\YYYY\MM\YYYYMMDD_HHmm.jpg ?

Thank you


回答1:


You can use a for loop, substrings, and 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!
    )
)

The for loop runs dir /b /a-d which returns all files in the current directory except folders. Substring notation is !variable:~start,length!.

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




回答2:


One-liner for the command prompt:

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


来源:https://stackoverflow.com/questions/29594200/how-to-batch-move-files-date-filename-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!