Solved

Search a network folder for a file in VBS

Posted on 2011-03-03
2
412 Views
Last Modified: 2012-05-11
I am looking for some help in a script.  We have folder on our server for our users home directory.  
There is a file in each one called config.xml.  I need to mass edit some lines.  I found this and it works but I can't figure out how to search for every instance of the file in all sub folders in the share.
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\config.xml", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "<PromptOnShutDown>1</PromptOnShutDown>", "<PromptOnShutDown>0</PromptOnShutDown>")

Set objFile = objFSO.OpenTextFile("C:\config.xml", ForWriting)
objFile.WriteLine strNewText
objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\config.xml", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "<PurgeOnShutDown>1</PurgeOnShutDown>", "<PurgeOnShutDown>0</PurgeOnShutDown>")

Set objFile = objFSO.OpenTextFile("C:\config.xml", ForWriting)
objFile.WriteLine strNewText
objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\config.xml", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "<CompactOnShutDown>1</CompactOnShutDown>", "<CompactOnShutDown>0</CompactOnShutDown>")

Set objFile = objFSO.OpenTextFile("C:\config.xml", ForWriting)
objFile.WriteLine strNewText
objFile.Close

Open in new window


This will only search a specific location.  I would need it to search the folder, find the file, edit these line, and then move to the next one and repeat.

Thanks for any help!
0
Comment
Question by:BEI_INC
2 Comments
 
LVL 11

Accepted Solution

by:
Tasmant earned 500 total points
ID: 35028724
try this

Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
strPath = "\\server\share"  'you can use any path you want, UNC or local
Recurse(strPath)
'End


Function Recurse(strPath)
	Set oFolder = objFSO.GetFolder(strPath)
	Set oSubFolders = oFolder.SubFolders
	Set oFiles = oFolder.Files
	
	For Each oFile In oFiles
		If oFile.Name = "config.xml" Then
			UpdateConfig(oFile.Path)
		End If
	Next
	
	For Each oSubFolder In oSubFolders
		Recurse(oSubfolder.Path)
	Next
End Function


Function UpdateConfig(strFile)
	Set objFile = objFSO.OpenTextFile(strfile, ForWriting)
	strText = objFile.ReadAll
	strNewText = Replace(strText, "<PromptOnShutDown>1</PromptOnShutDown>", "<PromptOnShutDown>0</PromptOnShutDown>")
	objFile.Write strNewText
	objFile.Close
End function

Open in new window

0
 

Author Closing Comment

by:BEI_INC
ID: 35056418
Thanks!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question