简体   繁体   中英

Batch file for moving files to folders based on filenames

I use Dropbox to automatically upload all the photos/videos I take from my phone to a folder "My Dropbox\\Camera Uploads". So this is full of files like:

2015-06-09 10.11.19.jpg

2015-09-11 09.28.46.mp4

I'd now like a batch file to move these to the correct folder (creating it if necessary) "..\\Photos\\Family\\YYYY-MM" where YYYY-MM is the year and month of the photo (ie the first seven characters of the filename).

(It has to be a relative rather than absolute path as this Dropbox folder is shared across machines with XP, Vista and Windows 7 OSs, so the first part of the path is different on each.)

I've found similar batch files and tried to tweak them, but just can't get it to work. Many thanks for your help.

You can use this script (put it in a file with extension .bat) and start it:

@echo off
setlocal enabledelayedexpansion
rem For each file in your folder
for %%a in (*.*) do (
    echo filename=%%a
    rem check if the it is not our script
    if "%%a" NEQ "%0" (
        set foldername=%%a
        set foldername=..\Photos\Family\!foldername:~0,7!
        echo foldername=!foldername!
        rem check if forlder exists, if not it is created
        if not exist "!foldername!" mkdir "!foldername!"
        rem Move (or change to copy) the file to directory
        move "%%a" "!foldername!\"
    )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM