如何将文件名追加到TXT内容中,如何基于原始输入文件名命名输出文件,以及循环遍历200,000个输入文件 [英] How to Batch Append Filename to TXT Contents, Name Output File Based on Original Input Filename, and Loop Through 200,000 Input Files

查看:411
本文介绍了如何将文件名追加到TXT内容中,如何基于原始输入文件名命名输出文件,以及循环遍历200,000个输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个HR数据项目,经过大量研究甚至是反复试验,我改编了以下批处理文件(Windows 8.1环境),该文件成功地从凌乱的文本文件中提取了员工的开始日期-非常感谢向用户dbenham提供该先前的解决方案(同样,我如下修改):

I am working on an HR data project and after much research and even more trial and error, I have adapted the following Batch File (Windows 8.1 environment) that successfully pulls an employee’s start date from a messy text file - with many thanks to user dbenham for that prior solution (again which I adapted as follows):

@echo off
setlocal disableDelayedExpansion
set "cnt=1"
>OUTPUT.txt (
  for /f "skip=219 tokens=24,25,26 delims= " %%B in (MVANHOUTEN.txt) do (
    echo(%%B %%C %%D
    set /a "1/(cnt-=1)" 2>nul || goto :break
  )
)
:break

哪里 MVANHOUTEN.txt是输入文件

Where MVANHOUTEN.txt is the input file

OUTPUT.txt文件仅包含: 1991年1月21日

OUTPUT.txt file contains only: January 21, 1991

我对批处理文件编程的复杂性知之甚少,尽管我已经确认dbenham的代码的天赋,并且上面所做的更改已100%奏效,但我不知道足以改变它而不破坏它.我需要该批处理文件来完成三件事,但是在不破坏上述代码功能的情况下,我似乎无法使其工作.具体来说,我需要:

I have a limited understanding of the intricacies of batch file programming, and despite the genius of dbenham's code which I have confirmed works 100% with my changes above, I do not know enough to alter this without breaking it. I need this batch file to do three more things but I cannot seem to make it work without destroying the functionality of the above code. Specifically, I need to:

  1. 我需要在数据提取后将原始文本文件名添加到输出文件的内容中.也就是说,我需要我的输出文件包含: MVANHOUTEN 1991年1月21日
  2. 我需要使用与输入文件相同的文件名来命名输出文件,而不是OUTPUT.txt,即MVANHOUTEN.txt.如果这不可能或太笨拙,则可以将其添加到原始文件名中,例如MVANHOUTEN-Processed.txt)
  3. 我需要一个do循环,因为我的目录中有近200,000名现任和前任雇员,并且我需要对每个文件批量执行上述操作-因此在逻辑上代替上面的(MVANHOUTEN.TXT)" ,我需要找到一种方法来遍历同一目录中的许多* .txt文件.结果将是单独的文件MVANHOUTEN.txt,CMONTYBURNS.txt,DISCOSTU.txt等,等等(或MVANHOUTEN-Processed.txt,CMONTYBURNS-Processed.txt,DISCOSTU-Processed.txt等).
  1. I need to add the original text filename to the contents of the output file after the data extract. That is, I need my output file to contain: MVANHOUTEN January 21, 1991
  2. Instead of OUTPUT.txt, I need my output file to be named with the same filename as the input file – that is, MVANHOUTEN.txt. If this is not possible or too unweildy, adding to the original filename would be an okay alternative – e.g. MVANHOUTEN-Processed.txt)
  3. I need a do loop as I have a directory of nearly 200,000 current and former employees and I need to batch perform the above operations for each and every file - so logically in place of "(MVANHOUTEN.TXT)" in the above, I need to find a way to loop through many *.txt files in the same directory. The results would be separate files MVANHOUTEN.txt, CMONTYBURNS.txt, DISCOSTU.txt, etc. etc. (or MVANHOUTEN-Processed.txt, CMONTYBURNS-Processed.txt, DISCOSTU-Processed.txt, etc.).

有人可以帮助我增强我的上述批处理文件来完成上述工作,而又不会破坏我成功地从dbenham改编的原始磨砂膏吗?提前非常感谢!

Can anybody please help me enhance my batch file above to accomplish the above without breaking the original scrub I successfully adapted from dbenham? Many thanks in advance!

推荐答案

未测试,因为我没有您的数据文件:

untested, as I don't have your datafiles:

@echo off
setlocal disableDelayedExpansion

for /f "delims=" %%a in ('dir /b *.txt') do (
  call :process %%a
)

:process
set "cnt=1"
>"%~n1-Processed.txt" (
  for /f "skip=219 tokens=24,25,26 delims= " %%B in (%~nx1) do (
    echo( %~n1 %%B %%C %%D
    set /a "1/(cnt-=1)" 2>nul || goto :eof
  )
)

这篇关于如何将文件名追加到TXT内容中,如何基于原始输入文件名命名输出文件,以及循环遍历200,000个输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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