Search a string in a text file. If it does not exist add it to the file.

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
kocchB
Posts: 9
Last visit: Mon Nov 05, 2012 3:18 pm

Search a string in a text file. If it does not exist add it to the file.

Post by kocchB »

I have a group of ini files that call procedures. The ini file name is based on the user logon ID.
For example: UserID1.ini UserID2.ini UserID3.ini .....
The path of the ini files is c:ini files
I also have a text file named usernames.txt populated with user logon ID(s). The content of usernames.txt looks like:
UserID1
UserID2
UserID3
UserID4
........


Need a Vbscript that will read (c:tempusernames.txt).
For each userID in usernames.txt, verify if there an ini file with the same name exist.
If true, search the existing file for the string "Net Use * ServerNameShareName"
If string exist do nothing
Else write the string "Net Use * ServerNameShareName" to the file.
If the ini file does not exist. Create one and write the string in it.

The script is below. When you run the script. it write several line of the string Net Use * ServerNameShareName on existing ini files that did not have it.

For example User1ID.ini content before is ( Net User Q ServerXshareX )
When you run the script, depending on how many username (5 for example) you have in usernames.txt

for the User1ID1.ini the content will be like this

Net User Q ServerXshareX
Net Use * ServerNameShareName
Net Use * ServerNameShareName
Net Use * ServerNameShareName
Net Use * ServerNameShareName
Net Use * ServerNameShareName


'*****************************************************************************************************************

Option Explicit

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Dim objFSO, objFile, objReadFile
Dim strInputFilePath, striniFolderPath, TextToAdd, arrFileLine(), i
Dim strLine


strInputFilePath = "C:tempUsernames.txt"
striniFolderPath = "C:ini files"
TextToAdd = "Net Use * ServerNameShareName"

Call Read(strInputFilePath)

Function ini(strLine, arrFileLines)
For Each strLine In arrFileLines
If objFSO.FileExists(striniFolderPath & "" & strLine & ".ini")Then
WScript.Echo "" & strLine & ".ini" & " exists"
Dim SearchingFor, strSearchString, mdftxt, strNextLine, arrNextLineList, j
SearchingFor = "Net Use * ServerNameShareName"
Set mdftxt = objFSO.OpenTextFile(striniFolderPath & "" & strLine & ".ini",1)
Do Until mdftxt.AtEndOfStream
strNextLine = mdftxt.ReadLine
arrNextLineList = Split(strNextLine)
For j = 0 To UBound (arrNextLineList)
If (InStr("" & arrNextLineList(j), SearchingFor) > 0) Then
WScript.Echo("" & arrNextLineList(j)) ' confirm the string is found. Do nothing
Else
Call Appendini()

End If
j = j + 1
Next
Loop
mdftxt.close
Else
Call Newini()
End If
Next
End Function

Function Appendini()
Dim objAppendini
Set objAppendini = objFSO.OpenTextFile(striniFolderPath & "" & strLine & ".ini",8, True)
objAppendini.WriteLine(TextToAdd)
objAppendini.Close
End Function
Function Newini()
Dim objNewFile
Set objNewFile = objFSO.CreateTextFile(strFolderPath & "" & strLine & ".ini", true)
bjNewFile.Write (TextToAdd)
objNewFile.Close
WScript.Echo "" & strLine & ".ini" & " was created."
End Function

Sub Read(strInputFilePath)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strInputFilePath)
If objFile.Size > 0 Then
Set objReadFile = objFSO.OpenTextFile(strInputFilePath, 1)
i = 0
Do Until objReadFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objReadFile.ReadLine
i = i + 1
Loop
objReadFile.Close
Call ini(strLine, arrFileLines)
Else
Call EmptyFolderMessage()
End If
End Sub

Sub EmptyFolderMessage()
MsgBox "The file is empty. Please populate with User ID(s) and try again.",64,"Modify ini Script."
End Sub
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Search a string in a text file. If it does not exist add it to the file.

Post by jvierra »

I am sorry but it is very difficult to understand what you are asking.

If you are gettng an error message then please post it.

You gave posted what looks like batch script an you are posting a vbscript also.

What is it the you are trying to do?

The code posted is hard to read and will not copy well. Can you please repost it as an attached file?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Search a string in a text file. If it does not exist add it to the file.

Post by jvierra »

I am guessing at what you are trying to do.

If my guess is correct then this is all you need to do.

See attached file:

Attached files /FileUpload/98/bb9a84b0d97a7bed874a5cf4cd701f.txt (1.1 KB)
User avatar
kocchB
Posts: 9
Last visit: Mon Nov 05, 2012 3:18 pm

Search a string in a text file. If it does not exist add it to the file.

Post by kocchB »

Yep your script does the job. It adds the string on existing ini files that did not have it prior.

I edited some of them and noticed a space between lines how to you remove the space to have the content looks like

net use Q: Server1Share1
net use R: Server2Share2
net use * ServerNameShareName

instead of

net use Q: Server1Share1
net use R: Server2Share2



net use * ServerNameShareName
This topic is 11 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked