Solved

Need help with a VBScript

Posted on 2003-11-12
9
266 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
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.

 
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 55

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

856 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