batch file to generat file names and content from .txt files?

  • Thread starter Thread starter gerryR
  • Start date Start date
G

gerryR

Hi All.

I'm not sure this is possible, I think it is but have been trying so many
thing I'm starting to think I'd be quicked doing it manually.

Basically I need to create 100s of .htm files (obviously its fine if there
..txt and I can rename) and would like the source of both the content of the
files and the name of the files to come from a few .txt files.

1st part I'm trying to rename a folder of .htm files using the contents of a
txt file I have.

So say I have 3 files in a folder (1.htm, 2.htm, 3.htm) and a txt file
called names.txt containing

full namea
full nameb
full namec

I'd like to rename the files so they become (full namea.htm, full nameb.htm,
full namec.htm)

I've tried

rename *.htm < names.txt

but just get syntax errors. Is this even possible? Can anyone help?

I'll get to the 2nd part if anyone can solve this, I apologise as the
message it way to big already!
thanks for any help anyone can provide (even a "no it can't be done" would
be better than nothing :)

gerryR
 
gerryR said:
Hi All.

I'm not sure this is possible, I think it is but have been trying so many
thing I'm starting to think I'd be quicked doing it manually.

Basically I need to create 100s of .htm files (obviously its fine if there
.txt and I can rename) and would like the source of both the content of
the files and the name of the files to come from a few .txt files.

1st part I'm trying to rename a folder of .htm files using the contents of
a txt file I have.

So say I have 3 files in a folder (1.htm, 2.htm, 3.htm) and a txt file
called names.txt containing

full namea
full nameb
full namec

I'd like to rename the files so they become (full namea.htm, full
nameb.htm, full namec.htm)

I've tried

rename *.htm < names.txt

but just get syntax errors. Is this even possible? Can anyone help?

I'll get to the 2nd part if anyone can solve this, I apologise as the
message it way to big already!
thanks for any help anyone can provide (even a "no it can't be done" would
be better than nothing :)

gerryR

for /f "delims=" %%i in (names.txt) do ren "%%i" "%%~ni.txt"

Assuming you don't have a file already named whatever.txt that is.

Try alt.msdos.batch.nt for more info.
 
Hi Billious

Thanks for the reply, for some reason I'm getting an "in was unexpected at
this time" error, I've tried changing it around a bit but to be honest I'm
not really sure what i'm changing. any idea?

thanks again for your time
gr
 
gerryR said:
Hi Billious

Thanks for the reply, for some reason I'm getting an "in was unexpected at
this time" error, I've tried changing it around a bit but to be honest I'm
not really sure what i'm changing. any idea?

thanks again for your time
gr


here's my batch job:

----- batch begins -------
[1]@echo off
[2]for /f "delims=" %%i in (fl1.txt) do echo ren "%%i" "%%~ni.txt"
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped
and should be rejoined. The [number] that starts the line should be
removed

Here's the content of fl1.txt:

----- file begins -------
a file.htm
another file.htm
----- file ends -------

(the lines beginning "-" do not form part of the file)

here's the output:

ren "a file.htm" "a file.txt"
ren "another file.htm" "another file.txt"

The ECHO keyword should be removed from [2] to execute the rename. It's
there to SHOW what the batch would do.

Without knowing PRECISELY what you've typed in - it's not that easy to look
over your shoulder and see - it's a bit difficult - but after a little bit
of playing, I found that if you've omitted the space between ["delims="] and
[%%i] then you'll get that error message. Batch tends to be very sensitive
to syntax.
 
Hi Billious

Thanks for the reply, for some reason I'm getting an "in was unexpected
at this time" error, I've tried changing it around a bit but to be honest
I'm not really sure what i'm changing. any idea?

thanks again for your time
gr


here's my batch job:

----- batch begins -------
[1]@echo off
[2]for /f "delims=" %%i in (fl1.txt) do echo ren "%%i" "%%~ni.txt"
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped
and should be rejoined. The [number] that starts the line should be
removed

Here's the content of fl1.txt:

----- file begins -------
a file.htm
another file.htm
----- file ends -------

(the lines beginning "-" do not form part of the file)

here's the output:

ren "a file.htm" "a file.txt"
ren "another file.htm" "another file.txt"

The ECHO keyword should be removed from [2] to execute the rename. It's
there to SHOW what the batch would do.

Without knowing PRECISELY what you've typed in - it's not that easy to
look over your shoulder and see - it's a bit difficult - but after a
little bit of playing, I found that if you've omitted the space between
["delims="] and [%%i] then you'll get that error message. Batch tends to
be very sensitive to syntax.

Hi Billious

I am getting that output now but nothings happening my files. I see in your
output the files are just being renamed from .htm to .txt. The problem is
my file names and the list of names in my txt files are not the same. I
need to rename my file names (which are all random names) from
"1strandomefilename.htm" to "1st line of txt file.htm" then
"2ndrandomfilename.htm" to "2nd line of txt file.htm"

sorry I think I was unclear in my 1st post. Thanks very much for your help
so far, if the above makes things clearer and you can think of a solution
I'd really appreciate it.

best regards
gerryR
 
gerryR said:
Hi All.

I'm not sure this is possible, I think it is but have been trying so
many thing I'm starting to think I'd be quicked doing it manually.

Basically I need to create 100s of .htm files (obviously its fine if
there .txt and I can rename) and would like the source of both the
content of the files and the name of the files to come from a few .txt
files.

1st part I'm trying to rename a folder of .htm files using the
contents of a txt file I have.

So say I have 3 files in a folder (1.htm, 2.htm, 3.htm) and a txt file
called names.txt containing

full namea
full nameb
full namec

I'd like to rename the files so they become (full namea.htm, full
nameb.htm, full namec.htm)

I've tried

rename *.htm < names.txt

but just get syntax errors. Is this even possible? Can anyone help?

I'll get to the 2nd part if anyone can solve this, I apologise as the
message it way to big already!
thanks for any help anyone can provide (even a "no it can't be done"
would be better than nothing :)

gerryR

for /f "delims=" %%i in (names.txt) do ren "%%i" "%%~ni.txt"

Assuming you don't have a file already named whatever.txt that is.

Try alt.msdos.batch.nt for more info.

Hi Billious

Thanks for the reply, for some reason I'm getting an "in was unexpected
at this time" error, I've tried changing it around a bit but to be
honest I'm not really sure what i'm changing. any idea?

thanks again for your time
gr


here's my batch job:

----- batch begins -------
[1]@echo off
[2]for /f "delims=" %%i in (fl1.txt) do echo ren "%%i" "%%~ni.txt"
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped
and should be rejoined. The [number] that starts the line should be
removed

Here's the content of fl1.txt:

----- file begins -------
a file.htm
another file.htm
----- file ends -------

(the lines beginning "-" do not form part of the file)

here's the output:

ren "a file.htm" "a file.txt"
ren "another file.htm" "another file.txt"

The ECHO keyword should be removed from [2] to execute the rename. It's
there to SHOW what the batch would do.

Without knowing PRECISELY what you've typed in - it's not that easy to
look over your shoulder and see - it's a bit difficult - but after a
little bit of playing, I found that if you've omitted the space between
["delims="] and [%%i] then you'll get that error message. Batch tends to
be very sensitive to syntax.

Hi Billious

I am getting that output now but nothings happening my files. I see in
your output the files are just being renamed from .htm to .txt. The
problem is my file names and the list of names in my txt files are not the
same. I need to rename my file names (which are all random names) from
"1strandomefilename.htm" to "1st line of txt file.htm" then
"2ndrandomfilename.htm" to "2nd line of txt file.htm"

sorry I think I was unclear in my 1st post. Thanks very much for your
help so far, if the above makes things clearer and you can think of a
solution I'd really appreciate it.

best regards
gerryR

----- batch begins -------
[1]@echo off
[2]:: create a list of filenames in a tempfile
[3]del "%temp%\tempfn.txt" 2>nul
[4]for %%i in (*.htm) do echo %%~dpnxi >>"%temp%\tempfn.txt"
[5]:: rename the files
[6]for /f "usebackq delims=" %%i in ("%temp%\tempfn.txt") do for /f
"delims=" %%j in (newnames.txt) do if exist "%%i" if not exist
"%%~dpi\%%j.htm" ECHO ren "%%i" "%%j.htm"
[7]del "%temp%\tempfn.txt" 2>nul
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped
and should be rejoined. The [number] that starts the line should be
removed

where newnames.txt contains the new names for the files, one to a line.

Notes:

[3] deletes the tempfile if it exists, suppressing any error message (if the
file doesn't exist, for example)
[4] could take a path before the filemask if required
[6] the ECHO keyword needs to be removed to perform the rename. As it
stands, the batch will show that it will ATTEMPT to rename ALL of the files
to EACH of the new names. Once the ECHO is removed, the first name that
doesn't already exist will be used as the target name. Once a file has been
renamed, the original name will no longer exist, so subsequent renames of
that file won't be attempted.

Test it on a small directory first (that should be obvious)
 
Back
Top