批量重命名文件 - 追加第 1 行和第 2 行3

发布于 2024-11-01 19:13:02 字数 577 浏览 5 评论 0原文

我使用的是 Windows 7。我有一堆文本文件,每个文件都包含一封电子邮件。每个都是这样开始的:

FROM:Person 致:另一个人 日期:2011 年 1 月 1 日 18:12:00 主题: 不管怎样

我想重命名这些文件,使它们的名称如下所示:

2011-01-01 18.12 Email from Person to Another Person reWhatever.txt

批处理编程是我所知道的,但我不太了解。为了将其限制在我可以快速理解的项目中,我认为最好的解决方案是将基本数据提取到一个文本文件中,然后我可以将其合并到批量重命名文件中。

在这种情况下,我正在寻找一个批处理文件,它将数据提取到文本文件中的单行中,然后我可以通过全局编辑将其调整为形状。换句话说,我想我正在寻找这种格式的文本行:

[当前文件名] [提取的日期和时间字符串] [来自] [到] [主题]

示例:

file01.txt 01-Jan-11 at 18: 12:00 从一个人到另一个人无论什么

如果我有这样的行,我可以在 Excel 中很快地将它们解析为重命名命令。

谢谢!

I'm using Windows 7. I have a bunch of text files, each containing one email message. Each starts this way:

FROM: Person
TO: Another Person
DATE: 01-Jan-11 at 18:12:00
SUBJECT: Whatever

I want to rename these files so that their names look like this:

2011-01-01 18.12 Email from Person to Another Person re Whatever.txt

Batch programming is all I know, and I don't know it very well. For purposes of restraining this to a project that I can understand quickly, I think my best solution will be to extract the essential data into a text file that I can then massage into a batch renaming file.

In that case, what I'm looking for is a batch file that will extract the data into single lines in a text file that I can then massage into shape with global edits. In other words, I think I'm looking for text lines in this format:

[current filename] [extracted date and time string] [from] [to] [subject]

Example:

file01.txt 01-Jan-11 at 18:12:00 from Person to Another Person re Whatever

If I've got lines like that, I can parse them into renaming commands pretty quickly in Excel.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美不过初阳 2024-11-08 19:13:02

鉴于您使用的是 Windows 7,我想我会建议一个替代方案。 Windows Powershell 是一个非常有用的命令工具,可用于做很多事情。我想我解决了你的完整问题:

$folder = "C:\..."

$regex = "FROM: (.*) TO: (.*) DATE: (.*) at (.*) SUBJECT: (.*)"

$files = Get-ChildItem $folder *.txt 

ForEach ($file in $files) {  
        $line =  (Get-Content $file.FullName -TotalCount 1)
        $match =  ([regex]$regex).matches($line)[0]
        $date = [DateTime]($match.Groups[3]).Value + [TimeSpan]($match.Groups[4]).Value
        $from = ($match.Groups[1])
        $to = ($match.Groups[2])
        $subject = ($match.Groups[5])

        # You can change the naming format in the brackets below
        Rename-Item $file.FullName -NewName ( $date.ToString("yyyy-MM-dd_HH-mm-ss") + " Email From " + $from + " to " + $to + " RE " + $subject)
}

它做了一些假设(就像总会找到匹配项)。您可以轻松调整命名格式和其他内容。将此代码保存为脚本 (.ps1) 并在 Powershell 提示符 (powershell.exe) 中运行它

Given that your using Windows 7, I thought I'd suggest an alternative. Windows Powershell is a a very useful command tool that can be used for a ton of stuff. I think I solved your complete problem:

$folder = "C:\..."

$regex = "FROM: (.*) TO: (.*) DATE: (.*) at (.*) SUBJECT: (.*)"

$files = Get-ChildItem $folder *.txt 

ForEach ($file in $files) {  
        $line =  (Get-Content $file.FullName -TotalCount 1)
        $match =  ([regex]$regex).matches($line)[0]
        $date = [DateTime]($match.Groups[3]).Value + [TimeSpan]($match.Groups[4]).Value
        $from = ($match.Groups[1])
        $to = ($match.Groups[2])
        $subject = ($match.Groups[5])

        # You can change the naming format in the brackets below
        Rename-Item $file.FullName -NewName ( $date.ToString("yyyy-MM-dd_HH-mm-ss") + " Email From " + $from + " to " + $to + " RE " + $subject)
}

It makes a few assumptions (like a match will always be found). You can easily adjust naming format and other things. Save this code as a script (.ps1) and run it in the Powershell prompt (powershell.exe)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文