1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to? Renaming files using information inside the file?

Discussion in 'TCC/LE Support' started by Geoffrey Kidd, Aug 6, 2017.

  1. Geoffrey Kidd

    Joined:
    Aug 6, 2017
    Messages:
    1
    Likes Received:
    0
    I've got a folder full of TXT story files with the actual story title and author are on the second and third lines of the file.

    For example, the file Carl-APsalter.txt begins with:
    ======================
    The Avalon Psalter
    by Lillian Stewart Carl

    What I want to do is to either:
    A: get a list of file names and headers in a format that I can then edit into rename commands using a textpad macro

    Carl-APsalter.txt
    ======================
    The Avalon Psalter
    by Lillian Stewart Carl

    Note: I can get a list of the file names OR a list of the header information,
    but not both at the same time. A "FOR ... echo | head )doesn't work.

    Even better, though, would be some way to build and execute the following command
    for each file:

    rename Carl-APsalter.txt "Lillian Stewart Carl - The Avalon Psalter.txt"

    Any help appreciated. I only have to do this once but getting it to work is driving me nuts.
    Thanks in advance.
     
  2. K_Meinhard

    Joined:
    May 20, 2008
    Messages:
    310
    Likes Received:
    0
    Have you considered using the @line function?

    And for many purpoises it is easier to use a DO loop instead of FOR
     
  3. Charles G

    Joined:
    Apr 2, 2011
    Messages:
    1,012
    Likes Received:
    0
    Code:
    md Processed
    for %fn in (*.txt) (
      set line2=%@line[%fn,1]
      set line3=%@line[%fn,2]
      set cAuthor=%@right[-4,%line3]
      copy /q "%fn" "Processed\%[cAuthor] - @line2.txt"
    )
    echo Newly named files are in Processed folder....
    
     
  4. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,188
    Likes Received:
    11
    As Klaus suggested, use DO instead of FOR. It'll make debugging substantially easier.
    Code:
    do f in *.txt (ren %f "%@right[-3,%@line[%f,2]] - %@line[%f,1].*")
    
     

Share This Page