# .bat or dos command to rename .txt files based on first line



## RedHanded (May 19, 2012)

Hi Guys,
I'm trying (going by memory from when I last used msdos, win 3.11 era  ) to rename a bunch of .txt files based on the content in the first line of each file, using cmd prompt in win7..
Its usually a string of 10 to 50 characters, always has spaces between words (document title), and some of them might have single or double quotes (guessing that these can make a problem)..
I tried installing ~7-8 free renaming utilities but none offered the option I needed.

if I cannot just rename them to "first line document title.txt" I could live with "first_line_document_title.txt" or a dash instead of underscore, and later use CKrename in another pass to fix those..

another thing, some of these text files show line breaks only when opened in wordpad (notepad ignores and its all unwrapped), and some work in notepad too. (mentioning in case there is a different way of describing the first line for each of these two text filetypes, although their extension is .txt in both cases)

Hope there is a solution, thanks for help in advance


----------



## Squashman (Apr 4, 2003)

How long is the first line of some of these files?

And yes if there is quotes in the first line of the file the script would have to use string substitution to remove them and any other special characters that might be in them.


----------



## Squashman (Apr 4, 2003)

Could you post some examples of the current filenames and what is the first line of text of those files and what you want the new filename to look like. Find some with quotes and single quotes and any special characters in the first line.


----------



## RedHanded (May 19, 2012)

Thanks squashman for offering help  I got this sorted last night (a bunch of extra steps and not how its supposed to be done but - done).. First I noticed the program FlashRenamer has an option to rename files based on their HTMLtitle.. so I used BKreplacem (replacetext2.2 now) to add "" to the beginning of each file, and replaced line break with +line break.. Then renamed with FlashRenamer, then removed and again using BKreplacem 
I am sure there is a smoother way to work this out, but I wasnt able to figure it out on time using dos commands and gave up.. (if you have an idea you can still post it, google likes this forum it seems  someone will use your advice


----------



## Squashman (Apr 4, 2003)

Well I know the basic code to do what you want to do but I don't post code until I have absolutely have all the facts digested. I can't tell you how many times I have written code for someone and then they came back and told me they meant something different and it either made the code become 10 times longer or something that could not be done with batch. I posted my concerns to your problem within an hour of you posting your question and since you didn't get back to me I wasn't going to post any code and at this point there is no need to since you have resolved your problem.
Time is the most precious commodity in my life. I tend to use it pretty wisely these days.


----------

