Computing.Net > Forums > Programming > find, replace, and save as

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

find, replace, and save as

Reply to Message Icon

Name: playinpearls
Date: August 26, 2008 at 06:33:16 Pacific
OS: xp
CPU/Ram: 3.2ghz, 992mb
Product: dell
Comment:

I have thousands of files i have to create for a configuration tool that i use at work.

i need a script that will open a main txt file (specifically an ini file), find and replace "xxxx" with "yyyy" and then save as "zzzz". I would like to be able to use a csv file to create multiple files at once because there are literally thousands i am going to be creating.

i have found a python script that looks like it might work, but i am an absolute noob at scripting and programming, and couldn't tell you how to make it work. any help would be greatly appreciated!

- Joey



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: August 26, 2008 at 21:31:39 Pacific
Reply:

Not clear yet. For a single file, use code below. But for multiple files, what's the naming rule for output?

::==================
@echo off > zzzz.ini
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (my.ini) do (
set str=%%a
set str=!str:xxxx=yyyy!
echo !str! >> zzzz.ini
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: playinpearls
Date: August 27, 2008 at 04:06:40 Pacific
Reply:

the files could be named the same as "yyyy" if it was easier

but i was actually going to name them as the serial number of the units i'm configuring

i was trying to use a csv file if rf031 was the file i was changing:

rf031,rf032,00002
rf031,rf033,00003
rf031,rf034,00004
rf031,rf035,00005

so to use this script, i would paste that into a txt file, change what was needed, and save as .bat to run?

- Joey


0

Response Number 3
Name: playinpearls
Date: August 28, 2008 at 06:54:59 Pacific
Reply:

ok, i've got this...

' ~~~Begin Script~~~

' Find and replace string

Dim FileName, Find, ReplaceWith, FileContents, dFileContents

Find = "RF047"
ReplaceWith = "RF048"
FileName = "RF048.ini"

'Read source text file
FileContents = GetFile(RF047)


'replace all string In the source file
dFileContents = replace(ReplaceWith, 1, 1, 1)

'Compare source And result
if dFileContents <> FileContents Then
'write result If different
WriteFile FileName, dFileContents

Wscript.Echo "Replace done."
If Len(ReplaceWith) <> Len(Find) Then 'Can we count n of replacements?
Wscript.Echo _
( (Len(dFileContents) - Len(FileContents)) /(Len(ReplaceWith)-Len(Find)) ) & _
" replacements."
End If
Else
Wscript.Echo "Searched string Not In the source file"
End If

' *************************
' Functions
' *************************

'Read text file
Function GetFile(RF047)
If FileName<>"" Then
Dim FS, FileStream
Set FS = CreateObject("Scripting.FileSystemObject")
on Error resume Next
Set FileStream = FS.OpenTextFile(Filename)
GetFile = FileStream.ReadAll
End If
End Function

'Write text file.
Function WriteFile(FileName, Contents)
Dim OutStream, FS

on Error Resume Next
Set FS = CreateObject("Scripting.FileSystemObject")
Set OutStream = FS.OpenTextFile(FileName, 2, True)
OutStream.Write Contents
End Function
=========================================

but instead of finding and replacing rf047 with rf048 and saving the file as rf048, it saves a file named rf048, with only rf048 written inside.
its not keeping anything else in the file, or maybe not reading it at all?

- Joey


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: find, replace, and save as

Batch file to find ip and host name, and save www.computing.net/answers/programming/batch-file-to-find-ip-and-host-name-and-save/19455.html

Find replace dialog acting unusual www.computing.net/answers/programming/find-replace-dialog-acting-unusual/7753.html

Using fread with a large file www.computing.net/answers/programming/using-fread-with-a-large-file-/10254.html