PC Review


Reply
Thread Tools Rate Thread

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

 
 
gerryR
Guest
Posts: n/a
 
      31st Aug 2006
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


 
Reply With Quote
 
 
 
 
billious
Guest
Posts: n/a
 
      31st Aug 2006

"gerryR" <(E-Mail Removed)> wrote in message
news:%23lnyO$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
gerryR
Guest
Posts: n/a
 
      31st Aug 2006
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


"billious" <(E-Mail Removed)> wrote in message
news:44f6ff86$0$15173$(E-Mail Removed)...
>
> "gerryR" <(E-Mail Removed)> wrote in message
> news:%23lnyO$(E-Mail Removed)...
>> 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.
>



 
Reply With Quote
 
billious
Guest
Posts: n/a
 
      31st Aug 2006

"gerryR" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "billious" <(E-Mail Removed)> wrote in message
> news:44f6ff86$0$15173$(E-Mail Removed)...
>>
>> "gerryR" <(E-Mail Removed)> wrote in message
>> news:%23lnyO$(E-Mail Removed)...
>>> 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.



 
Reply With Quote
 
gerryR
Guest
Posts: n/a
 
      31st Aug 2006

>>>> 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


 
Reply With Quote
 
billious
Guest
Posts: n/a
 
      31st Aug 2006

"gerryR" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
>>>>> 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)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DOS: How to create a batch file that will move file based on files =?Utf-8?B?QnJhbmRvbg==?= Windows XP General 2 25th Aug 2006 11:50 PM
Something deleted the content in Microsoft Word files and WordPerfectfiles, but left the file names. Edward Anti-Virus 7 16th Apr 2005 11:11 PM
file splitter (also creates a batch file to re-connect the split files) *ProteanThread* Freeware 0 11th Jan 2005 04:31 PM
Creating a batch file to append date to file names bladeraptor Microsoft Windows 2000 CMD Promt 13 5th Feb 2004 03:56 PM
hide batch file content daniel Microsoft Windows 2000 CMD Promt 9 31st Jul 2003 05:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:30 AM.