批处理脚本,根据文件名将家庭视频移动到正确的文件夹中

时间:2018-03-24 05:29:40

标签: batch-file automation batch-processing

我正在尝试将大量家庭电影分类到文件夹结构中,其中图片已经通过名为robofolder的应用程序进行了排序。 文件夹结构看起来像这样 年---月 - 日--- HHMM.jpg

电影的格式如下所示20150504_144132.mp4 这是我第一次编写一些代码,我有以下工作在一个文件,只要我设置变量%filename%,我需要脚本做的是在指定的文件夹中查找* .mp4然后它依次处理每一个。 代码如下。 (任何指针非常感谢)

@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

1 个答案:

答案 0 :(得分:1)

要处理每个文件,请使用简单的for循环(`for %% a in(* .mp4)do ...') 我自由地缩短了你的代码。

@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"
)

注意:使用mdmovefor /f

调整文件夹以满足您的需求