Solved

Search a network folder for a file in VBS

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

690 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