Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search for a file and replace it whiout prompting

Status
Not open for further replies.

wasserfa89

Technical User
Apr 15, 2012
24
CH
Hi all

I need a function in VB Script to search for a file on a local PC starting at the drive c:and if the file named "tnsnames.ora" is found it shoud be overwritten by a new version from an UNC Network Path

How can i do this?
 
recursively search a directory for the filename. If found, replace with another.

!! Untested !!

Code:
set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

function findFile(strDir, strFilename)
   'read the files
   for each objFile in objFSO.GetFolder(strDir).Files
      if (objFile.Name = strFilename) then
          findFile = objFile.Path
          exit function
      end if
   next

   'read directories
   for each objFolder in objFSO.GetFolder(strDir).SubFolders
      findFile = finFile(objFolder.Path, strFilename)
   next
end function

strSource = "\\server\share\tnsnames.ora"
strFile = findFile("C:\","tnsnames.ora")
if (objFSO.FileExists(strFile)) then
   objFSO.CopyFile strSource, strFile, true
end if

- Geates
[/code]

 
thanks but the following error appears:

set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

function findFile(strDir, strFilename)
'read the files
for each objFile in objFSO.GetFolder(strDir).Files
if (objFile.Name = strFilename) then
findFile = objFile.Path
exit function
end if
next

'read directories
for each objFolder in objFSO.GetFolder(strDir).SubFolders
findFile = finFile(objFolder.Path, strFilename)
next
end function

strSource = "\\server\datas\replacetns\tnsnames.ora"
strFile = findFile("C:\","tnsnames.ora")
if (objFSO.FileExists(strFile)) then
objFSO.CopyFile strSource, strFile, true
end if

--------------------------------------------------------------------------------------------------------------------------------------------------------------

Type mismatch "finFile" Error 800A00D




 
this was an error

i become the error Access denied on this line

for each objFile in objFSO.GetFolder(strDir).Files

800A0046


User is admin
 
Likely, there is a folder you don't have access to read. If you know the file will never be in a directory that you don't have access to AND you don't care about other error, you can put [tt]on error resume next[/tt] at the top of the function. Be aware that this command surpress ALL errors so you won't really know if it worked - but you'll know that it completed.

Code:
function findFile(strDir, strFilename)
   [red]on error resume next[/red]
   ... the reset of the code ...
end function

Alternatively, if you run it with administrative priviledges you likely won't get that error

- Geates


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top