-2

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!

Ray Woodcock
  • 209
  • 3
  • 13
  • I rarely post questions -- could someone please explain the downvotes? – Ray Woodcock Aug 07 '17 at 01:25
  • 1
    I venture to guess that it's because this is a "give me the codes"-style question: you are asking for someone else to do all your work for you, with no indication that you have made any effort to do it on your own. This is not a "code-for-hire" (or for free!) site; such requests are frowned upon. You can check the [help], and the [help/on-topic] on-topic page in particular, for some extra information. – Hellion Oct 04 '17 at 17:58
  • Thanks for the guess. That would be strange reasoning. I thought I said I was planning to do the parts I knew how to do. For the rest of it, why wouldn't someone try just pointing me in the right direction, offer suggestions, or otherwise do something helpful? I mean, users *are* invited to ask questions. – Ray Woodcock Dec 18 '17 at 04:36

1 Answers1

3

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)

  • I have been thinking for about a year that I should learn PowerShell. I've seen several instances where it was plainly more powerful than the old DOS batch language. It would probably take only a day or two to learn how to use it and to make it work . The thing is, I just can't – Ray Woodcock Apr 18 '11 at 18:58
  • I'd be happy to provide more assistance with the powershell script - Can you post what went wrong. Other than that, I think your best bet is to find a command line tool that can read the first line of a file, then pipe it to a text file. – Anthony Truskinger Apr 19 '11 at 00:47
  • Just noticed I didn't complete the thought. I think I wrote (and must have deleted) something like, "I just can't spare the time now." My belated thanks for the offer to help. – Ray Woodcock Sep 27 '17 at 18:36