Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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

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,00005so to use this script, i would paste that into a txt file, change what was needed, and save as .bat to run?
- Joey

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, dFileContentsWscript.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, FSon 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

![]() |
![]() |
![]() |

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