I have 250,000 text files (Stories of various names) in alphabetized folders.
What I would like to do is drop a batch file in to each of the folders, run it and have it rename all the text files in that folder. I have experimented with a few test batch files to no avail.
I would like to rename each text file based on the 4th line (Story Title) and the 6th line (Story's Author) of their respected text.....so the outputted file name would look like this:
Story Title - Story's Author.txt
Here is the line formatting for all of the files:
Line 1 (empty)
Line 2 (empty)
Line 3 (empty)
Line 4 < Story Title >
Line 5 empty
Line 6 < by Author's Name >
Line 7 (empty)
Line 8 (empty)
Line 9 (empty)
Line 10 Category: Various text Here
Line 11 Published: YYYY-MM-DD
Line 12 Updated: YYYY-MM-DD
Line 13 Packaged: YYYY-MM-DD HH:MM:SS
Line 14 Chapters: Numbers Here
Line 15 Publisher: website.com
Line 16 Story URL: https://www.website.com/s/xxxxxxxx
Line 17 Author URL: (empty)
Line 18 https://www.website.com/stories/memberp ... xxxxxxxxxx
Line 19 Summary: text here that sometimes extends down farther
From Line 18 down each file is different, but the story beginnings always start on Line 31. My thoughts are to maybe just delete lines 10 through 30 after the batch file reads Line 4 and 6 so it can rename them....or would it be better to use a second Batch file to delete lines 10 - 30 after the rename?
If I attempt to do this manually I think I will be doing this for the next 20 years.....HELP!
Many thanks for any help.
Rename Text Files Based On Content ("Line 4" - "Line 6".txt)
Moderator: DosItHelp
-
- Posts: 2
- Joined: 21 Jul 2017 23:26
Rename Text Files Based On Content ("Line 4" - "Line 6".txt)
Last edited by CarnalProdigy on 22 Jul 2017 14:04, edited 2 times in total.
Re: Rename Text Files Based On Content ("Line 4" - "Line 6".txt)
Usually you'll have exceptions you didn't think of. E.g. it isn't line 4 and 6 but something else, or title/author are empty, etc.. If so please tell us.
Also there are characters \/:*?"<>| that are not allowed in file names. The code below will already replace most of them. But if you have asterisks in the names you're out of luck.
Currently the code only lists the commands. If it looks okay for you then remove ECHO and PAUSE.
Steffen
Also there are characters \/:*?"<>| that are not allowed in file names. The code below will already replace most of them. But if you have asterisks in the names you're out of luck.
Code: Select all
@echo off &setlocal
for /f "delims=" %%i in ('dir /a-d /b *.txt') do (
set "oldname=%%~nxi"
setlocal EnableDelayedExpansion
<"!oldname!" (
for /l %%j in (1 1 3) do set /p "="
set /p "ttl="
set /p "="
set /p "aut="
)
if /i "!aut:~,3!"=="by " set "aut=!aut:~3!"
set "ttl=!ttl:"=!"
set "aut=!aut:"=!"
for %%j in ("\" "/" "<" ">" "|" ":") do (
set "ttl=!ttl:%%~j=_!"
set "aut=!aut:%%~j=_!"
)
ECHO ren "!oldname!" "!ttl:?=! - !aut:?=!.txt"
endlocal
)
PAUSE
Currently the code only lists the commands. If it looks okay for you then remove ECHO and PAUSE.
Steffen
Re: Rename Text Files Based On Content ("Line 4" - "Line 6".txt)
Now that you edited your initial post twice I wonder if you already tried the code.
Why not leaving the content as it is? Removing lines always means that you have to write the file new.
Steffen
CarnalProdigy wrote:My thoughts are to maybe just delete lines 10 through 30 after the batch file reads Line 4 and 6 so it can rename them
Why not leaving the content as it is? Removing lines always means that you have to write the file new.
Steffen
-
- Posts: 2
- Joined: 21 Jul 2017 23:26
Re: Rename Text Files Based On Content ("Line 4" - "Line 6".txt)
aGerman wrote:Now that you edited your initial post twice I wonder if you already tried the code.CarnalProdigy wrote:My thoughts are to maybe just delete lines 10 through 30 after the batch file reads Line 4 and 6 so it can rename them
Why not leaving the content as it is? Removing lines always means that you have to write the file new.
Steffen
I wanted to thank you for your code....and the amazingly fast response you gave me! I did have a chance earlier today to test it. It worked fantastic!
Unfortunately, I had to go after a few quick tests. I did edit my original post, the first because I was trying to attach one of the text files and discovered that you are not allowed to. The second, was me trying to provide more info to answer your original questions.....and then I was thinking about the delete option.
The delete idea would be to clean up the files. Possible to to an all in one Batch file or do it after the renaming with a second Batch file?
Again, THANK YOU for the code above! Just that is a life saver!
Re: Rename Text Files Based On Content ("Line 4" - "Line 6".txt)
You're wellcome!
Well as I tried to explain above, you can't simply remove a few lines from somewhere in a file. In that case you would have to read the files line by line and write them in a new file while leaving out those that you don't want anymore. Computing thousands of files this way would take quite a while (depending on their size). If these lines are not bothering you too much you should rather keep them in the file.
Steffen
CarnalProdigy wrote:The delete idea would be to clean up the files. Possible to to an all in one Batch file or do it after the renaming with a second Batch file?
Well as I tried to explain above, you can't simply remove a few lines from somewhere in a file. In that case you would have to read the files line by line and write them in a new file while leaving out those that you don't want anymore. Computing thousands of files this way would take quite a while (depending on their size). If these lines are not bothering you too much you should rather keep them in the file.
Steffen
Re: Rename Text Files Based On Content ("Line 4" - "Line 6".txt)
And it's because of the fast, efficient solutions to problems like this that batch RULES!CarnalProdigy wrote:I have 250,000 text files (Stories of various names) in alphabetized folders.
I would like to rename each text file based on the 4th line (Story Title) and the 6th line (Story's Author) of their respected text...
If I attempt to do this manually I think I will be doing this for the next 20 years...
If it took you 3 minutes to rename each file, it would have taken you 250000*3=750000 minutes, or 12,500 man hours of work. Working 8hrs a day it would have taken you almost 5 years to do the job.
So maybe not 20 years, but still a lot of years for something a batch file would do in under a day!