简体   繁体   中英

Batch script to move home videos into correct folders based off filenames

i am trying to sort a large selection of home movies into a folder structure where the pictures have been sorted to via an app called robofolder. folder structure looks like this Year---Month--Day---HHMM.jpg

The format of the movies look like this 20150504_144132.mp4 this is my first time writing some code, i have got the below working on a single file, as long as i set the variable %filename%, what i need the script to do is look in a specified folder for *.mp4 and then process each one in turn it finds. code is below. ( any pointers greatly appreciated)

@echo off
set filename=20150504_144132.mp4
set filepath=c:\Temp\%filename%
REM get the date from the file name
for /f "delims=." %%A in ("%filename%") do set fdate=%%A
REM Y=YEAR; M=MONTH; D=DAY
set Y=%fdate:~0,4%
set M=%fdate:~4,2%
set D=%fdate:~6,2%

REM echo %Y% %M% %D%
if "%M%" == "01" set Mo=January
if "%M%" == "02" set Mo=February
if "%M%" == "03" set Mo=March
if "%M%" == "04" set Mo=April
if "%M%" == "05" set Mo=May
if "%M%" == "06" set Mo=June
if "%M%" == "07" set Mo=July
if "%M%" == "08" set Mo=August
if "%M%" == "09" set Mo=September
if "%M%" == "10" set Mo=October
if "%M%" == "11" set Mo=November
if "%M%" == "12" set Mo=December
REM echo %Mo%
REM get the time from the file name
for /f "delims=." %%A in ("%filename%") do set ftime=%%A
REM H=HOUR; Mi=MIN; S=SEC; E=Ext
set H=%ftime:~9,2%
set Mi=%ftime:~11,2%
set S=%ftime:~13,2%
REM echo %H% %Mi% %S%
REM check to see if directories exist
if not exist V:\%Y% mkdir V:\%Y%
if not exist V:\%Y%\%Mo% mkdir V:\%Y%\%Mo%
if not exist V:\%Y%\%Mo%\%D% mkdir V:\%Y%\%Mo%\%D%
REM copy the file to the newly created destination folder
move %filepath% V:\%Y%\%Mo%\%D%\%H%"-"%Mi%"-"%S%.mp4

To process each file, use a simple for loop (`for %%a in (*.mp4) do ...')
I took the freedom to shorten your Code a bit.

@echo off 
setlocal enabledelayedexpansion
REM just genererating some sample files for testing:
break>20171224_173012.mp4
break>20180224_164024.mp4
break>20180524_155004.mp4
break>20180924_140013.mp4
break>20181124_070559.mp4

REM set "array" of month names:
set i=100
for %%m in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) do (
  set /a i+=1
  set "Mo[!i:~-2!]=%%m"
)

REM process each file:
for %%f in (*.mp4) do (
  REM get elements:
  set dt=%%~nf
  set Y=!dt:~0,4!
  set M=!dt:~4,2!
  set D=!dt:~6,2!
  set H=!dt:~9,2!
  set Mi=!dt:~11,2!
  set S=!dt:~13,2!
  call set Mo=%%Mo[!M!]%%
  REM no need for "if exist", just suppress errormessages:
  md "!y!\!Mo!\!D!\" 2>nul
  move "%%f" "!y!\!Mo!\!D!\!h!-!Mi!-!S!%%~xf"
)

NOTE: adapt the Folders to your Needs with md , move and the for /f

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