根据内容中的多个字符串重命名文本文件 [英] Rename text files based on multiple strings in their contents

查看:136
本文介绍了根据内容中的多个字符串重命名文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这里有两个类似的问题(基于文件内容批处理文件重命名文件是一个我试图从中得到一个答案-但我没有真正的线索我在做什么),但是我找不到满足我确切需求的任何东西,这是我第一次真正涉足批处理编程,因此语法对于我.

Firstly, there are a couple of similar questions on here to this (Rename file based on file Content batch file being the one I have tried to work an answer from - but I have no real clue what I'm doing), however I cannot find anything that meets my exact needs, and this is my first real foray into batch programming so the syntax is fairly new to me.

问题:

我有数百个名称不同的文本文件,其标题格式如下:

I have several hundred text files, with different names, where the header is formatted like so:

"Event Type : Full Histogram"
"Serial Number : xxxxxx"
"Version :  V 10.60-8.17 "
"File Name : W133FA0Z.580H"
"Histogram Start Time : 12:39:08"
"Histogram Start Date : 2014-04-11"

我想尽可能创建一个批处理文件,将文件夹中的所有文件重命名为:

I would like if possible to create a batch file to rename all the files in the folder to the format of:

StartDate StartTime

因此在此示例中:

2014-04-11 12:39:08 

我的问题在于,如果只是一行,我不确定如何实际将其指向找到字符串的地方(我已经尝试编辑上面发布的问题的答案).而且,我还不知道如何添加第二段代码来查找StartTime字符串,然后将其附加到StartDate.

My problems lie in the fact I'm not sure how to actually point it to where to find the string if it was for just one line (I've tried editing the answers in the question I posted above). And, futhermore, I have no idea how to add a second bit of code to find the StartTime string and then append that to the StartDate.

预先感谢

克里斯

推荐答案

这是一种非常有效的方法.

Here is a very efficient method.

@echo off
pushd "pathToYourFolderContainingFilesToRename"
for /f "tokens=1,3 delims=:" %%A in (
  'findstr /bc:^"\"Histogram Start Date :" *.txt'
) do for /f delims^=^"^  %%C in (
  "%%B"
) do for /f tokens^=4-6^ delims^=^":^  %%D in (
  'findstr /bc:^"\"Histogram Start Time :" "%%A"'
) do ren "%%A" "%%C %%D.%%E.%%F.txt"
popd

第一个循环有两个目的.它建立包含开始日期字符串的文件名,并为每个文件返回日期字符串.

The 1st loop serves two purposes. It establishes file names that contain the start date string, as well as also returning the date string for each file.

第二个循环从日期字符串中去除空格和引号.

The 2nd loop strips out spaces and quotes from the date string.

第3循环从文件中解析出开始时间.

The 3rd loop parses out the start time from the file.

第二和第三循环的语法非常笨拙,无法在分隔符列表中包含引号.第二个循环将DELIMS设置为引号和空格.第三组将DELIMS设置为引号,冒号和空格.

The 2nd and 3rd loops have very awkward syntax to enable including a quote in the list of delimiters. The 2nd loop sets DELIMS to a quote and a space. The 3rd set DELIMS to quote, colon, and a space.

这篇关于根据内容中的多个字符串重命名文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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