Solved

Need help with a VBScript

Posted on 2003-11-12
9
265 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
ID: 9731297
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
ID: 9731952
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
ID: 9732093
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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 26

Expert Comment

by:EDDYKT
ID: 9733277
>>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
 

Author Comment

by:geekoid2
ID: 9733466
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
ID: 9733585
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
ID: 9734069
AFAIK

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


8->
0
 

Author Comment

by:geekoid2
ID: 9735820
Oh, I though he was sneezing or something verbally.
0
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 125 total points
ID: 9737885
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

809 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