[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Search a network folder for a file in VBS

Posted on 2011-03-03
2
Medium Priority
?
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
Tasmant earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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