2017-06-22 137 views
-3

我有一个文件夹中的文件(C:/location1)。文件如下:bat文件根据文件名将文件移动到月份文件夹

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

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

我之前没有写过一个批处理文件,并且正在寻找一些帮助/想法来创建一个来启动此任务。任何其他链接,作为我开始的教程也会很好。结果由于

+4

为什么月缩短到JAN但月保持在6月,而不是被缩短到JUN? – SomethingDark

+0

它可以是一月 – user396123

+3

您有具体问题吗?如果没有,那么让我建议开始收集你自己的想法,并开始实施基于它们的脚本;当你有问题回到这里时。如果你期望有人为你写脚本,那么你在这里完全错了。请学习[问]! – aschipfl

回答

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 
+0

假设文件名称只是一个示例,在批处理中将它们静态化并没有多大用处。还有一个举措是不需要副本。 – LotPings

+0

文件扩展名将为.pdf – user396123

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

干得好,Aacini! **'; **'Ops,你是不是Aacini!无论如何,做得好!我只是使用完整的'月份'名称作为数组,以便更清楚。 – Aacini

+0

非常感谢你! – user396123

+0

批次,当我试图从我的c:\用户\公共位置运行这个,它说不支持UNC路径。我确实需要从网络共享位置运行它。如何纠正这一点? – user396123

相关问题