I need a batch file or text file generation, please.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

I need a batch file or text file generation, please.

#1 Post by Kerr Avon » 17 Apr 2016 06:47

I'm using Windows 7, and I have a folder with 1683 folders inside it, each containing an issue of the Magnet (a pre-war children's (and adults, the appeal was universal) weekly story paper - http://friardale.co.uk/Magnet/Magnet.htm).

Each of the folders is merely a number;

0001
0002
0003
0004
0005


etc, and I want to edit these so that they also contain the name of each individual story, i.e.;


0001 The Making of Harry Wharton
0002 The Taming of Harry
0003 The Mystery of Greyfriars
0004 Chums of the Remove
0005 Kidnapped

etc. But rather than edit each folder name manually, I'd rather use a batch file, something like;


Rename 0001 "0001 The Making of Harry Wharton"
Rename 0002 "0002 The Taming of Harry"
Rename 0003 "0003 The Mystery of Greyfriars"
Rename 0004 "0004 Chums of the Remove"
Rename 0005 "0005 Kidnapped"

but for all 1683 files of course (and there would need to be quotation marks, of course, in case the name included spaces). And I was wondering if there was a way to generate a text file that would scan the folder, and so find that there was 1683 subfolders and for each subfolder add a line to the text file of;

rename [folder name] "[folder name] "

(the above ends up with a space before the second quotation maeks), so that I end up with;

Rename 0001 "0001 "
Rename 0002 "0002 "
Rename 0003 "0003 "
Rename 0004 "0004 "
Rename 0005 "0005 "

(up to 1683, of course), so that all I have to do then is to manually enter the name of each story, rename the .txt to .bat, and run it.

If this isn't possible, then can someone use Microsoft Word or whatever to auto-generate a text file that contains what I want, please, and post it here. But a program or batch file that could be used to automatically generate the list would be preferable, as then I could use it for other things in the future.

Thanks for any answers.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: I need a batch file or text file generation, please.

#2 Post by foxidrive » 17 Apr 2016 11:39

The .doc versions of the file downloads include the title - do you have these or the .pdf versions, or the .mobi versions?

Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

Re: I need a batch file or text file generation, please.

#3 Post by Kerr Avon » 17 Apr 2016 14:41

foxidrive wrote:The .doc versions of the file downloads include the title - do you have these or the .pdf versions, or the .mobi versions?


I have the PDF versions, but I converted them all to .jpg files so I could use (the fantastic picture viewer) Irfanview, as I don't like .pdf files.

Unfortunately, only some of the Magnet issues are available in .doc format, but all are in .pdf format.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: I need a batch file or text file generation, please.

#4 Post by ShadowThief » 17 Apr 2016 16:25

Kerr Avon wrote:
foxidrive wrote:The .doc versions of the file downloads include the title - do you have these or the .pdf versions, or the .mobi versions?


I have the PDF versions, but I converted them all to .jpg files so I could use (the fantastic picture viewer) Irfanview, as I don't like .pdf files.

Unfortunately, only some of the Magnet issues are available in .doc format, but all are in .pdf format.

Then where are we supposed to get the list of names from? I am not typing that all out by hand.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: I need a batch file or text file generation, please.

#5 Post by penpen » 18 Apr 2016 02:48

If i'm not wrong, then the OP only wanted to have something like this "createList.bat" (untested):

Code: Select all

@echo off
> "the.txt" (
   for /F "tokens=*" %%a in ('dir /a:d /b') do (
      echo rename "%%~a" "%%~a "
   )
)

But i recommend the use of OCR software like https://github.com/tesseract-ocr/tesseract/blob/master/README.md.


penpen

Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

Re: I need a batch file or text file generation, please.

#6 Post by Kerr Avon » 18 Apr 2016 05:20

ShadowThief wrote:Then where are we supposed to get the list of names from? I am not typing that all out by hand.


No, I just need a batch file that contains;

Rename 0001 "0001 "
Rename 0002 "0002 "
Rename 0003 "0003 "
Rename 0004 "0004 "
Rename 0005 "0005 "

(up to 1683, of course), and then I'll enter the text myself.




penpen wrote:If i'm not wrong, then the OP only wanted to have something like this "createList.bat" (untested):

Code: Select all

@echo off
> "the.txt" (
   for /F "tokens=*" %%a in ('dir /a:d /b') do (
      echo rename "%%~a" "%%~a "
   )
)

But i recommend the use of OCR software like https://github.com/tesseract-ocr/tesseract/blob/master/README.md.


penpen


That batch file text does exactly what I want - thanks very much! I've just ran it, and it created a 4KB text file containing the rename commands how I wanted them, i.e. (copy and pasted from the produced text file);

rename "0001" "0001 "
rename "0002" "0002 "
rename "0003" "0003 "
rename "0004" "0004 "
rename "0005" "0005 "

(up to 1683), so now all I have to do is enter the story titles, rename the .txt to .bat, run the batch file, and the renaming will be done.

Thanks again to both of you.

Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

Re: I need a batch file or text file generation, please.

#7 Post by Kerr Avon » 18 Apr 2016 06:07

Just to push my luck (sorry!), is this possible?

I have a saved .txt file listing the titles, one per line, of the storie titles, i.e.;

The Making of Harry Wharton
The Taming of Harry
The Mystery of Greyfriars
Chums of the Remove
Kidnapped

through to the final name. Is it possible to have a batch file or program that will merge the text file that penpen's batch file made, with this text file, i.e. so that the text file containing;

rename "0001" "0001 "
rename "0002" "0002 "
rename "0003" "0003 "
rename "0004" "0004 "
rename "0005" "0005 "


and the text file with the names;


The Making of Harry Wharton
The Taming of Harry
The Mystery of Greyfriars
Chums of the Remove
Kidnapped



will produce a third text file containing;


rename "0001" "0001 The Making of Harry Wharton"
rename "0002" "0002 The Taming of Harry"
rename "0003" "0003 The Mystery of Greyfriars"
rename "0004" "0004 Chums of the Remove"
rename "0005" "0005 Kidnapped"

(up until 1683, of course). If this isn't possible, then fair enough, I'm just looking to save myself a lot of typing (well, copy and pasting, really). Thanks for any answers.

Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

Re: I need a batch file or text file generation, please.

#8 Post by Kerr Avon » 18 Apr 2016 16:16

A friend reminded me that this was easily done with Excel, and I've done it, and now all of the folders have the desired file names. Thanks to everyone for reading this, and for penpen for writing the batch file that creates the initial batch file.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: I need a batch file or text file generation, please.

#9 Post by foxidrive » 19 Apr 2016 05:53

Kerr Avon wrote:No, I just need a batch file that contains;

Rename 0001 "0001 "
Rename 0002 "0002 "
Rename 0003 "0003 "
Rename 0004 "0004 "
Rename 0005 "0005 "

(up to 1683, of course), and then I'll enter the text myself.

I was very amused when it became clear that you just wanted the text above.

Protip here Kerr - don't include a link and talk about a magazine and website and the pdf files, when none of those things are what you wanted. It's great to have good info, but here it threw me and others entirely off the scent. :)

No harm done, this time. ;)

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: I need a batch file or text file generation, please.

#10 Post by penpen » 19 Apr 2016 07:11

@Kerr Avon:
If the name of the directories are numbers only, and if the episode names are in the line with the same number of the file "Titles.txt", you could do something like this:

Code: Select all

@echo off
>"result.txt" (
   for /F "tokens=1* delims=:" %%a in ('findstr /n "^" "Titles.txt"') do (
      set /A "line=10000+%%~a"
      call echo rename "%%line:~1%%" "%%line:~1%% %%~b"
   )
)
(Don't use forbidden characters for file names.)


penpen

Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

Re: I need a batch file or text file generation, please.

#11 Post by Kerr Avon » 19 Apr 2016 11:04

foxidrive wrote:I was very amused when it became clear that you just wanted the text above.

Protip here Kerr - don't include a link and talk about a magazine and website and the pdf files, when none of those things are what you wanted. It's great to have good info, but here it threw me and others entirely off the scent. :)

No harm done, this time. ;)


Understood, sorry mate.





penpen wrote:@Kerr Avon:
If the name of the directories are numbers only, and if the episode names are in the line with the same number of the file "Titles.txt", you could do something like this:

Code: Select all

@echo off
>"result.txt" (
   for /F "tokens=1* delims=:" %%a in ('findstr /n "^" "Titles.txt"') do (
      set /A "line=10000+%%~a"
      call echo rename "%%line:~1%%" "%%line:~1%% %%~b"
   )
)
(Don't use forbidden characters for file names.)


penpen



Thanks for that, I'll try it the next time I need something similar.

BTW, regarding bad filenames, I'd stupidly put some bad characters into the issue name text file, such as

The Smedly/Vernon Smith series

where of course the forward slash can't be used in a folder name. I should have realised straight away, as opposed to having to look through the text file AFTER some renames had failed, but human error will always be with us (if I'm involved, anyway!).

Post Reply