bat文件根据文件名将文件移动到月份文件夹

时间:2017-06-22 03:39:08

标签: batch-file

我在文件夹(C:/location1)中有文件。文件如下:

 A-14-0005 - Title1 - 06202017.pdf
 B-14-1111 - Title2 - 06202017.pdf
 B-15-7676 - Title3 - 06202017.pdf

我需要将它们移到另一个位置(c:/location2)。如果当前月份是1月,则创建文件夹2017(今年),然后创建子文件夹JANUARY,如果当前月份是6月,则创建子文件夹JUNE。然后将3个文件移动到此文件夹中。

我之前没有编写过一个批处理文件,并且正在寻找一些关于创建一个批处理文件以启动此任务的帮助/想法。任何其他链接作为我开始的教程也将是伟大的。感谢

2 个答案:

答案 0 :(得分:1)

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Set "Dir1=c:\location1"
Set "Dir2=c:\location2"

:: Build Mon[01..12] array
Set Cnt=100
For %%A in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
  ) Do Set /A Cnt+=1&Set Mon[!Cnt:~-2!]=%%A
::Set Mon[

For /f "delims=" %%A in (
  ' Dir /B/A-D "%Dir1%\?-??-???? - * - *.pdf" ^|findstr "[01][0-9][0-3][0-9]20[0-9][0-9]\.pdf$" '
) Do (
  Set "File=%%~nA"
  Call Set "MoveTo=%Dir2%\!File:~-4!\%%Mon[!File:~-8,2!]%%\"
  MD "!MoveTo!" >Nul 2>&1
  Move "%%~fA" "!MoveTo!"
)

结果的示例树:

> Tree /F .
C:\LOCATION2
└───2017
    └───Jun
            B-15-7676 - Title3 - 06202017.pdf
            B-14-1111 - Title2 - 06202017.pdf
            A-14-0005 - Title1 - 06202017.pdf

答案 1 :(得分:0)

@echo off
set month-num=%date:~3,2%
set year-num=%date:~6,10%
IF "%month-num:~0,1%"=="0" SET month-num=%month-num:~1%
FOR /f "tokens=%month-num%" %%a in ("January February March April May June July August September October November  December") do set mo-name=%%a

if not exist "C:\location2\%year-num%" md C:\location2\%year-num%\%mo-name%"
copy "C:\location1\A-14-0005 - Title1 - 06202017.*" "C:\location2\%year-num%\%mo-name%\" /y
copy "C:\location1\B-14-1111 - Title2 - 06202017.*" "C:\location2\%year-num%\%mo-name%\" /y
copy "C:\location1\B-15-7676 - Title3 - 06202017.*" "C:\location2\%year-num%\%mo-name%\" /y
pause