Solved

Search a network folder for a file in VBS

Posted on 2011-03-03
2
413 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending Attachment via CDO 3 70
Attention Please 10 49
need the count of duplicate records in a column 12 43
Excel formula to report date modified 14 29
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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