• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

replacing text on multiple text files

C

chufa72

#1
Hello. I am looking for a way to automatically replace text on multiple text
files. I am writing my first script for work, but I've hit a brick wall. The
ways I've tried and failed only work on a single text file at a time. Thanks,

David
 

My Computer

T

Todd Vargo

#2
chufa72 wrote:

> Hello. I am looking for a way to automatically replace text on multiple
text

> files. I am writing my first script for work, but I've hit a brick wall.
The

> ways I've tried and failed only work on a single text file at a time.
Thanks,

This may be a dumb question, but what ways did you try for multiple files?
We can not tell why you hit a brick wall if we can not see the code you
used.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
 

My Computer

A

Al Dunbar

#3
"chufa72" <[email protected]> wrote in message
news:[email protected]

> Hello. I am looking for a way to automatically replace text on multiple
> text
> files. I am writing my first script for work, but I've hit a brick wall.
> The
> ways I've tried and failed only work on a single text file at a time.
> Thanks,
If you have code that works for a single file, all you need to do is to put
that into a loop where you supply a different filename each time. If your
code looks like this:

file2change = "C:\test\file1.txt"
rem - your code to modify the file

change it to this:

for each file2change in
array("C:\test\file1.txt","C:\test\file2.txt","C:\test\file3.txt")
rem - your code to modify the file
next

This still only does one file at a time, as it does not start processing the
second file until the first has been done. But at least it will process
multiple files in one run of the script.

Depending on the particulars of your situation there could be better ways to
organize this, the most obvious being to pass as a parameter to your script
the name of a file that contains a list of files to be processed. Without
knowing precisely the situation or the code you have working, though, it is
hard to be more specific.

/Al
 

My Computer

C

chufa72

#4
I have tried this code, which is a chimera from several ones in the
depository (pardon my ignorance, I am completely newbish).

Const OverwriteExisting = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder("C:\scripts\temp")
objFSO.CopyFile "C:\scripts\*.txt" , "C:\scripts\temp" , OverwriteExisting
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\scripts\temp'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In FileList
If objFile.Extension = "txt" Then
strNewName = objFile.Drive & objFile.Path & _
objFile.FileName & "." & "pcr"
errResult = objFile.Rename(strNewName)
End If
Next
Const FOR_READING = 1
Const FOR_WRITING = 2
strFileName = "C:\scripts\temp\*.txt" (comment: this right here is where it
fails)

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)

"Todd Vargo" wrote:

> chufa72 wrote:

> > Hello. I am looking for a way to automatically replace text on multiple
> text

> > files. I am writing my first script for work, but I've hit a brick wall.
> The

> > ways I've tried and failed only work on a single text file at a time.
> Thanks,
>
> This may be a dumb question, but what ways did you try for multiple files?
> We can not tell why you hit a brick wall if we can not see the code you
> used.
>
> --
> Todd Vargo
> (Post questions to group only. Remove "z" to email personal messages)
>
>
 

My Computer

T

Todd Vargo

#5
"chufa72" <[email protected]> wrote in message
news:[email protected]

> I have tried this code, which is a chimera from several ones in the
> depository (pardon my ignorance, I am completely newbish).
>
> Const OverwriteExisting = TRUE
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFolder = objFSO.CreateFolder("C:\scripts\temp")
> objFSO.CopyFile "C:\scripts\*.txt" , "C:\scripts\temp" , OverwriteExisting
> strComputer = "."
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
>
> Set FileList = objWMIService.ExecQuery _
> ("ASSOCIATORS OF {Win32_Directory.Name='C:\scripts\temp'} Where " _
> & "ResultClass = CIM_DataFile")
>
> For Each objFile In FileList
> If objFile.Extension = "txt" Then
> strNewName = objFile.Drive & objFile.Path & _
> objFile.FileName & "." & "pcr"
> errResult = objFile.Rename(strNewName)
> End If
> Next
> Const FOR_READING = 1
> Const FOR_WRITING = 2
> strFileName = "C:\scripts\temp\*.txt" (comment: this right here is where
it

> fails)
>
> Set objFS = CreateObject("Scripting.FileSystemObject")
> Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)
Unfortunately, not enough information was given. Assuming from the code
above, it appears you want to create a temp folder, copy all .txt files to
it, rename the .txt files in temp folder to .pcr extension, then open
for_writing to the oldname in the TEMP folder. The following code does that.

Const FOR_READING = 1
Const FOR_WRITING = 2
Const OverwriteExisting = TRUE
TEMP = "C:\scripts\temp"

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(TEMP) Then objFSO.DeleteFolder(TEMP)
Set objFolder = objFSO.CreateFolder(TEMP)
objFSO.CopyFile "C:\scripts\*.txt", TEMP, OverwriteExisting

For Each objFile In objFSO.GetFolder("E:\scripts\temp").Files
'Condition below is not really needed because
' the TEMP folder only contains .txt files.
'If objFSO.GetExtensionName(objFile.Name) = "txt" Then
strOldName = objFile.Path
strNewName = Left(strOldName, InstrRev(strOldName, ".")) & "pcr"
objFSO.MoveFile strOldName, strNewName
Set objTS = objFSO.OpenTextFile(strOldName, FOR_WRITING, True)
objTS.Write "What do you want to write here?"
'End If
Next

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
 

My Computer

C

chufa72

#6
Thank you very much for your reply. Basically what I wanted to do is for
every text file in the working folder, create a new one with the same file
name, but extension .pcr instead of .txt. The contents of this .pcr file
would be its file name written four times, in four different lines. Sounds
complicated, I know.

The way I was going to go about it was, like you said, to create a temp
folder, copy all .txt files to it, rename the .txt files in temp folder to
..pcr extension, then (and this is where I got stuck) replacing the text
inside.

Thanks,

David

"Todd Vargo" wrote:

>
> "chufa72" <[email protected]> wrote in message
> news:[email protected]

> > I have tried this code, which is a chimera from several ones in the
> > depository (pardon my ignorance, I am completely newbish).
> >
> > Const OverwriteExisting = TRUE
> >
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Set objFolder = objFSO.CreateFolder("C:\scripts\temp")
> > objFSO.CopyFile "C:\scripts\*.txt" , "C:\scripts\temp" , OverwriteExisting
> > strComputer = "."
> > Set objWMIService = GetObject("winmgmts:" _
> > & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
> >
> > Set FileList = objWMIService.ExecQuery _
> > ("ASSOCIATORS OF {Win32_Directory.Name='C:\scripts\temp'} Where " _
> > & "ResultClass = CIM_DataFile")
> >
> > For Each objFile In FileList
> > If objFile.Extension = "txt" Then
> > strNewName = objFile.Drive & objFile.Path & _
> > objFile.FileName & "." & "pcr"
> > errResult = objFile.Rename(strNewName)
> > End If
> > Next
> > Const FOR_READING = 1
> > Const FOR_WRITING = 2
> > strFileName = "C:\scripts\temp\*.txt" (comment: this right here is where
> it

> > fails)
> >
> > Set objFS = CreateObject("Scripting.FileSystemObject")
> > Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)
>
> Unfortunately, not enough information was given. Assuming from the code
> above, it appears you want to create a temp folder, copy all .txt files to
> it, rename the .txt files in temp folder to .pcr extension, then open
> for_writing to the oldname in the TEMP folder. The following code does that.
>
> Const FOR_READING = 1
> Const FOR_WRITING = 2
> Const OverwriteExisting = TRUE
> TEMP = "C:\scripts\temp"
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> If objFSO.FolderExists(TEMP) Then objFSO.DeleteFolder(TEMP)
> Set objFolder = objFSO.CreateFolder(TEMP)
> objFSO.CopyFile "C:\scripts\*.txt", TEMP, OverwriteExisting
>
> For Each objFile In objFSO.GetFolder("E:\scripts\temp").Files
> 'Condition below is not really needed because
> ' the TEMP folder only contains .txt files.
> 'If objFSO.GetExtensionName(objFile.Name) = "txt" Then
> strOldName = objFile.Path
> strNewName = Left(strOldName, InstrRev(strOldName, ".")) & "pcr"
> objFSO.MoveFile strOldName, strNewName
> Set objTS = objFSO.OpenTextFile(strOldName, FOR_WRITING, True)
> objTS.Write "What do you want to write here?"
> 'End If
> Next
>
> --
> Todd Vargo
> (Post questions to group only. Remove "z" to email personal messages)
>
>
 

My Computer

C

chufa72

#7
By the way, I forgot to mention in my previous reply that the code you posted
works, thanks!

I do have a follow-up question, though: is it possible to make it work at a
variable current folder it is in rather than a fixed location (c:\scripts)? I
realize now that in practice, this script would be executed at many different
folders and not just c:\scripts.

"Todd Vargo" wrote:

>
> "chufa72" <[email protected]> wrote in message
> news:[email protected]

> > I have tried this code, which is a chimera from several ones in the
> > depository (pardon my ignorance, I am completely newbish).
> >
> > Const OverwriteExisting = TRUE
> >
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Set objFolder = objFSO.CreateFolder("C:\scripts\temp")
> > objFSO.CopyFile "C:\scripts\*.txt" , "C:\scripts\temp" , OverwriteExisting
> > strComputer = "."
> > Set objWMIService = GetObject("winmgmts:" _
> > & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
> >
> > Set FileList = objWMIService.ExecQuery _
> > ("ASSOCIATORS OF {Win32_Directory.Name='C:\scripts\temp'} Where " _
> > & "ResultClass = CIM_DataFile")
> >
> > For Each objFile In FileList
> > If objFile.Extension = "txt" Then
> > strNewName = objFile.Drive & objFile.Path & _
> > objFile.FileName & "." & "pcr"
> > errResult = objFile.Rename(strNewName)
> > End If
> > Next
> > Const FOR_READING = 1
> > Const FOR_WRITING = 2
> > strFileName = "C:\scripts\temp\*.txt" (comment: this right here is where
> it

> > fails)
> >
> > Set objFS = CreateObject("Scripting.FileSystemObject")
> > Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)
>
> Unfortunately, not enough information was given. Assuming from the code
> above, it appears you want to create a temp folder, copy all .txt files to
> it, rename the .txt files in temp folder to .pcr extension, then open
> for_writing to the oldname in the TEMP folder. The following code does that.
>
> Const FOR_READING = 1
> Const FOR_WRITING = 2
> Const OverwriteExisting = TRUE
> TEMP = "C:\scripts\temp"
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> If objFSO.FolderExists(TEMP) Then objFSO.DeleteFolder(TEMP)
> Set objFolder = objFSO.CreateFolder(TEMP)
> objFSO.CopyFile "C:\scripts\*.txt", TEMP, OverwriteExisting
>
> For Each objFile In objFSO.GetFolder("E:\scripts\temp").Files
> 'Condition below is not really needed because
> ' the TEMP folder only contains .txt files.
> 'If objFSO.GetExtensionName(objFile.Name) = "txt" Then
> strOldName = objFile.Path
> strNewName = Left(strOldName, InstrRev(strOldName, ".")) & "pcr"
> objFSO.MoveFile strOldName, strNewName
> Set objTS = objFSO.OpenTextFile(strOldName, FOR_WRITING, True)
> objTS.Write "What do you want to write here?"
> 'End If
> Next
>
> --
> Todd Vargo
> (Post questions to group only. Remove "z" to email personal messages)
>
>
 

My Computer

T

Todd Vargo

#8
chufa72 wrote:

> By the way, I forgot to mention in my previous reply that the code you
posted

> works, thanks!
>
> I do have a follow-up question, though: is it possible to make it work at
a

> variable current folder it is in rather than a fixed location
(c:\scripts)? I

> realize now that in practice, this script would be executed at many
different

> folders and not just c:\scripts.
The following script will operate on files in whatever folder is current and
eliminates creation of the TEMP folder.

Const FOR_READING = 1
Const FOR_WRITING = 2
Const OverwriteExisting = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each objFile In objFSO.GetFolder(".").Files
If objFSO.GetExtensionName(objFile.Name) = "txt" Then
strNewName = objFSO.GetBaseName(objFile.Name) & ".pcr"
Set objTS = objFSO.OpenTextFile(strNewName, FOR_WRITING,
OverwriteExisting)
For i = 1 to 4
objTS.WriteLine objFSO.GetBaseName(objFile.Name)
Next
End If
Next

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
 

My Computer

C

chufa72

#9
Thank you very much! Works like a charm.

"Todd Vargo" wrote:

>
> chufa72 wrote:

> > By the way, I forgot to mention in my previous reply that the code you
> posted

> > works, thanks!
> >
> > I do have a follow-up question, though: is it possible to make it work at
> a

> > variable current folder it is in rather than a fixed location
> (c:\scripts)? I

> > realize now that in practice, this script would be executed at many
> different

> > folders and not just c:\scripts.
>
> The following script will operate on files in whatever folder is current and
> eliminates creation of the TEMP folder.
>
> Const FOR_READING = 1
> Const FOR_WRITING = 2
> Const OverwriteExisting = TRUE
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
>
> For Each objFile In objFSO.GetFolder(".").Files
> If objFSO.GetExtensionName(objFile.Name) = "txt" Then
> strNewName = objFSO.GetBaseName(objFile.Name) & ".pcr"
> Set objTS = objFSO.OpenTextFile(strNewName, FOR_WRITING,
> OverwriteExisting)
> For i = 1 to 4
> objTS.WriteLine objFSO.GetBaseName(objFile.Name)
> Next
> End If
> Next
>
> --
> Todd Vargo
> (Post questions to group only. Remove "z" to email personal messages)
>
>
 

My Computer