Batch脚本开发需求:按文件名批量归类家庭视频至指定目录
Hey there! Great job getting the single-file handling working already—first time coding and you’re off to a solid start. Let’s expand that script to automatically loop through all your MP4 files and organize them into Robofolder’s directory structure.
Here’s a complete batch script that does exactly what you need, with comments explaining each step so you can follow along:
@echo off setlocal enabledelayedexpansion :: Update these paths to match your setup set "source_dir=C:\Your\Family\Videos\Raw" :: Folder with all your unorganized MP4s set "target_root=C:\Your\Family\Videos\Organized" :: Where you want the sorted folders to go :: Loop through every MP4 file in the source directory for %%f in ("%source_dir%\*.mp4") do ( :: Grab the filename without extension (e.g., "20150504_144132" from "20150504_144132.mp4") set "filename=%%~nf" :: Extract date/time components from the filename set "year=!filename:~0,4!" :: First 4 characters = year (2015) set "month=!filename:~4,2!" :: Next 2 = month (05) set "day=!filename:~6,2!" :: Next 2 = day (04) set "hourmin=!filename:~9,4!" :: Characters after underscore = hour + minute (1441) :: Build the Robofolder-style directory name set "target_dir=!year!---!month!---!day!---!hourmin!" :: Create the target directory (if it doesn't exist already) mkdir "%target_root%\!target_dir!" 2>nul :: Move the file to its new home (use "copy" instead of "move" if you want to keep the original) echo Processing "%%~nxf"... move "%%f" "%target_root%\!target_dir!\" ) echo Done! All MP4 files have been organized. pause endlocal
Key things to know:
- Delayed Expansion: The
setlocal enabledelayedexpansionline is critical here. When you modify variables inside aforloop, batch needs this to recognize the updated values immediately (otherwise it would use the original value for every iteration). - Path Customization: Make sure to change
source_dirto the folder with your unorganized videos, andtarget_rootto where you want the sorted folders to live. - Safety First: If you’re worried about accidentally moving files you don’t want to, replace
movewithcopyto test the script first—you can switch back once you’re sure it works. - Error Suppression: The
2>nulaftermkdirhides the "directory already exists" error message, so the script won’t spam you with unnecessary warnings.
Just save this as OrganizeVideos.bat, update the paths, and double-click it to run. It’ll handle all your MP4s automatically!
内容的提问来源于stack exchange,提问作者Mark Tomlinson
相关产品推荐
相关产品推荐

