批量复制文件夹月份到文件夹日的文件

时间:2017-05-21 09:12:17

标签: batch-file

我有公司代码文件夹,在子文件夹公司代码中有月份文件夹,在月份文件夹中有文本文件等于月份日期(exp.folder 01 textfile = 31,文件夹01 textfile = 28)

    0300003 Boots -------------> month  

                                (01)-----> Text file = 31 file
                                (02)-----> Text file = 28 file
                                (03)-----> Text file = 31 file
                                (..)-----> Text file = day of the month
                                (12)-----> Text file = 31 file 

enter image description here

 0300004 AsiaBook ---------> month (01,02,03..12)
 0300005 FamilyMart --------> month (01,02,03..12)

我想将月份文件夹中的文件复制到另一天文件夹

     0300003_20170101144116.txt to c:\jan\01 
     0300003_20170102144034.txt to c:\jan\02
     0300003_20170103144748.txt to c:\jan\03
       ..........
     0300003_20170131154443.txt to c:\jan\31

'

      0300003_20170201145125.txt to c:\fab\01
      ..........
      0300003_20170228144741.txt to c:\fab\28

和其他公司

      0300004_20170101144255.txt to c:\jan\01

因为我想在每个月的每一天汇总所有公司

谢谢你的回复......

1 个答案:

答案 0 :(得分:1)

如果前几年的格式相同,例如

X:\PathTo\2017\KPT_BU\0300003 Boots\Send\11\0300003_20171131154119.txt
X:\PathTo\2015\KPT_BU\0300005 FamilyMart\Send\09\0300005_20150915144813.txt

然后将以下脚本放在X:\PathTo中并调用它应该创建您每年要查找的目录和文件结构,例如

%dstRoot%\2017\Nov\31\0300003_20171131154119.txt
%dstRoot%\2015\Sep\15\0300005_20150915144813.txt

其中%dstRoot%是您为新目录结构和复制文件选择的目标。 (我从初始位置更改了它,因为通常在C:的根目录中创建目录的权限受限制)。您可以在第3行(不要用反斜杠关闭它)将该位置更改为更适合您的位置。

@Echo Off
SetLocal DisableDelayedExpansion
Set "dstRoot=C:\Users\liptnait"
Set "m=JanFebMarAprMayJunJulAugSepOctNovDec"
For /F "Delims=" %%A In ('Where /R . ???????_??????????????.txt') Do (
    Set "_o=%%~dpnA"
    SetLocal EnableDelayedExpansion
    Set "_t=!_o:~-8,2!
    For /F "Tokens=1,5 Delims=\" %%B In ("!_o:%CD%\=!") Do (Set/A "_i=3*(%%C-1)"
        Call Set "_m=%%m:~!_i!,3%%"
        If Not Exist "%dstRoot%\%%B\!_m!\!_t!" MD "%dstRoot%\%%B\!_m!\!_t!"
        Copy "%%A" "%dstRoot%\%%B\!_m!\!_t!">Nul)
    EndLocal)
GoTo :EOF