Solved

Need help with a VBScript

Posted on 2003-11-12
9
263 Views
Last Modified: 2010-05-01
Hello all, what I need is a VBScript or just a function for that matter but it has to work in a .vbs file.

What I need it to do is take an argument that is a path to a specific directory on my server. Within that directory there are subfolders and files. I need to look for specific files and when they are found I need to overwrite them with the updated version of these files. The directory structure is different on each server but the path is always the same. What I'm am doing is updating icon files for office programs. Our GPO's in the domain control what the user has for icons on their desktop so there are stored on the server. We are about to upgrade to Office 2003 so the icons need to be updated as well. So the script has to be able to read through the directory where we keep them and when it finds them replace them with the newer version. A function would work better because I have to do it for Word, Excel, PowerPoint, ect... and it would be easier if I could pass the function the argument of which one I'm looking for.

Something Like:

Public Sub changefile(path, file)
     getfolder(path)
     searchfolder(path) for (file)
     If (file) exsist then overwrite with (newfile)
     else exit sub
     Do until there are no more files to change
exit sub

Obviously this isnt proper code it was just to help show what I need it tp do.

Points go to the first code that works.

Thanks.
0
Comment
Question by:geekoid2
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
first approach (without parameters and path aand filenames hard-coded):

Call CopyIfNewer()




sub CopyIfNewer()
Dim fso
Set fso = CreateObject("scripting.filesystemobject")

Dim fTarget, fSource
Set fTarget = fso.GetFile("c:\Fmc\Register\FmcRegCorp.mde")
Set fSource = fso.GetFile("\\lnxsui\comun\Software\FmcRegister\Updates\FmcRegCorp.mde")
If fSource.DateCreated > fTarget.DateCreated Then
    fTarget.Delete True
    fSource.Copy "c:\Fmc\Register\", True
End If
End Sub
0
 

Author Comment

by:geekoid2
Comment Utility
This is close but it assumes that the target file is known. What I asked is that it takes a tageted path and then searches through this path to determine if the files exist. If the file does exist then copy it with the new version. I don't want it to test the DateCreated property I only need to verify that the file is there and if so then copy.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
I understood what you need, i posted a "FIRST approach" as i said.
AFAIK, it is not possible to pass command line args to vbscript file. Sorry. I will keep searching anyway.
0
 
LVL 26

Expert Comment

by:EDDYKT
Comment Utility
>>it is not possible to pass command line args to vbscript file

Dim oArgs, ArgNum, ArgServerList

Set oArgs = WScript.Arguments
ArgNum = 0
While ArgNum < oArgs.Count

      Select Case LCase(oArgs(ArgNum))
            Case "--help","-?":
                  Call DisplayUsage
            Case Else:
                  Call DisplayUsage
      End Select      

      ArgNum = ArgNum + 1
Wend

Sub DisplayUsage
      WScript.Echo "Usage: ..."
      WScript.Quit (1)
End Sub
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:geekoid2
Comment Utility
You most certainly can pass command line arguments in a script file. If you don't know how you just need to say so. I wont hold it against you, lol.

This is going to be what I use to copy the file once it's found.

Public Sub copynewicon(pathto)

      Dim pathfrom

      pathfrom = "D:\Test2\New.txt"
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set f = fso.GetFile(pathfrom)
      f.Copy (pathto)
End Sub

copynewicon "D:\Test\New.txt"

What I need is the routine that takes a path i.e... D:\path and then searches through all of the files within the path and each time it comes across the file replace it with the new one.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
just take a close look to my comment before go further, i said:
"....
AFAIK, it is not possible to pass command ...."

As
Far
As
I
Know
0
 
LVL 26

Expert Comment

by:EDDYKT
Comment Utility
AFAIK

it is really a short hand that I've never seen before


8->
0
 

Author Comment

by:geekoid2
Comment Utility
Oh, I though he was sneezing or something verbally.
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 125 total points
Comment Utility
As I mentioned in my post to the previous posting copying short cut files (if that is what you are doing) rather than creating them on the machine is not such a good idea. But if you want to do it that way then you need a method to parse a directory sub-tree. The easiest way to do this (arguably not the most efficient) is to use a recursive search. The following MSDN article will tell you how to do this

HOW TO: Recursively Search Directories by Using FileSystemObject
PSS ID Number: 185601

Basically, you take a path and a filename as parameters, use the path as a starting point and recursively search for the file. If you find it you replace it

Sub SomeFunction(path, file)
   filepath = Find(path, file)
   if filepath <> "" then
        Copy(SourceFile, filepath)
   end if
end sub

The Find function you can get from the article.

In your post you don't say where the function would get the value for newfile. If you post that information I can post more comprehensive code
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now