从XML文件中提取日期,并将文件移动到从日期创建的目录中 [英] Extract date from XML file and move file to directory created from date

查看:132
本文介绍了从XML文件中提取日期,并将文件移动到从日期创建的目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录,其中包含将近一百万个XML文件.不用说,它需要永久加载(20分钟以上).因此,我正在编写一个脚本,将文件划分为多个文件夹,顶级文件夹是year,每年文件夹数少于几个月.有4个主要文件名,可以从第三个令牌中提取日期,而从第二个令牌中提取其余文件名.即:

I have a directory that contains close to a million XML files. Needless to say, it takes forever to load (20+ minutes) So, I'm writing a script to divide the files into folders with the top level being year and having months under each year. There are 4 main filenames where the date can be extracted from the 3rd token and the rest from the second token. ie:

BA1253570001_BALMIS_20130617_TRC_0_109506738E.xml
BA1254260001_ACCTV21_20140430_AMR_0_1095611492.xml
BA1736330001_SWFTOUT_20140929_LIQ_1_MTBX553494.xml
BA1739240001_FEDOUT_20140904_LIQ_1_105633316M.xml

其余都是这样:

EODMESS_20140718_MTBX473286.xml
MSGCONF_20140410_109558667V.xml

我敢肯定有一种更简单的方法,但这是到目前为止的代码:

I'm sure there is an easier way to do it, but here is my code so far:

@echo on
setlocal enabledelayedexpansion
Set "starttime=%time%"
pushd C:\temp\xmls
for /f %%a in ('dir /b/o:d *.xml') do (
  call :ExtractDates %%a ret
  echo %%a - !ret!
  for /f "tokens=1" %%b in ("!ret!") do (
    for /f "tokens=1-3 delims=/" %%c in ("%%b") do (
        if not exist .\%%e md .\%%e
        if not exist .\%%e\%%c md .\%%e\%%c
        if %%b equ %%c/%%d/%%e (
        echo moving %%~nxa to .\%%e\%%c
        echo move %%~nxa .\%%e\%%c
        pause
        )
    )   
  )
)
echo Start time: %starttime%
echo End time: %time% 
popd
exit /b

:ExtractDates
@echo on
setlocal enabledelayedexpansion
Echo Starting ExtractDates
for %%a in (BALMIS ACCTV21 FEDOUT SWFTOUT) do (
  if not errorlevel 1 (set t=3) else set t=2
  Call :ExtractFunc %~1 %%a !t! ret
  endlocal&set "%~2=!ret!"&exit /b 0
)  
exit /b

:ExtractFunc
@echo on 
setlocal
Echo Starting ExtractFunc
for /f "tokens=%3 delims=_" %%a in (
    'echo %~1^|Findstr "%~2"'
    ) do ( 
    if not errorlevel 1 (
      endlocal&set "%~4=%%a"&exit /b 0
    ) 
)  
exit /b 

问题在于变量令牌没有返回正确的数字,我不确定为什么.任何建议表示赞赏.

The problem is that the variable token isn't returning the right number and I'm not sure why. Any suggestions appreciated.

推荐答案

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
PUSHD "%sourcedir%"
FOR /f "tokens=1*delims=" %%a IN (
  'dir /b /a-d "%sourcedir%\*_*_*.xml" '
 ) DO SET "filename=%%a"&CALL :process

POPD

GOTO :EOF

:process
FOR /f "tokens=2,3,6delims=_" %%m IN ("%filename%") DO SET "date1=%%m"&SET "date2=%%n"&SET "whichdate=%%o"
IF DEFINED whichdate SET "date1=%date2%"
IF NOT DEFINED date2 GOTO :eof
ECHO(MD .\%date1:~0,4%\%date1:~4,2%
ECHO(MOVE "%filename%" .\%date1:~0,4%\%date1:~4,2%\
GOTO :EOF 

您需要更改sourcedir的设置以适合您的情况.

You would need to change the setting of sourcedir to suit your circumstances.

所需的MD命令仅出于测试目的而ECHO. 确认命令正确无误后,将ECHO(MD更改为MD以实际创建目录.附加2>nul以禁止显示错误消息(例如,当目录已存在时)

The required MD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(MD to MD to actually create the directories. Append 2>nul to suppress error messages (eg. when the directory already exists)

所需的MOVE命令仅被ECHO用于测试目的. 验证命令正确无误后,将ECHO(MOVE更改为MOVE以实际移动文件.附加>nul以禁止显示报告消息(例如1 file moved)

The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)

仅提取两个可能的日期字符串,并使用第六个令牌的存在来表示要选择两个位置中的哪个位置来生成目标目录.如果没有第三个令牌,则跳过(未指定合适的掩码)

Simply extract the two possible datestrings and use the presence of the sixth token to signal which of the two positions to select for generation of the destination directory. Skip if there's no third token (fails to fit mask specified)

然后选择date1的必填字段并进行一些子字符串化.

Then select the required field to date1 and do some substringing.

这篇关于从XML文件中提取日期,并将文件移动到从日期创建的目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆