Re: need help with a VBScript - copies files behind links to another location

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Ed (anonymous_at_discussions.com)
Date: 12/14/04


Date: Tue, 14 Dec 2004 10:00:55 -0700

Thank you. This script works great!

I'm completely new to VBScript so this is a good learning experience for me.
Now, I'd like to figure out how to drag a set of links or an entire folder
to the icon and have things work properly. I think I have an idea how to do
this, but it'll take a lot of trial and error for a beginner such as myself.
If you have suggestions, they are welcome! Here is the script again
below...

~~~~~~~~~~~~~~~~~~~~~
'The version below supports dragging of a link file to the script icon
'(with a hard coded target path), as well as two input parameters where
'the first one is a source folder and the second is the target folder.
'written by Torgeir Bakken (MVP)

Set oArgs = WScript.Arguments
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")

bFileOnly = False ' init value

If oArgs.Count = 1 Then

   sSource = oArgs(0)

   ' need to hard code target in this case
   sToFolder = "c:\test"

   If Not oFSO.FileExists(sSource) And Not oFSO.FolderExists(sToFolder) Then
     MsgBox "Error: First parameter is not a file and/or Second " _
          & "parameter not a folder!", _
          vbCritical + vbSystemModal, sTitle
     Wscript.Quit
   End If

   If LCase(oFSO.GetExtensionName(sSource)) <> "lnk" Then
     MsgBox "Error: First parameter is not a shortcut file!", _
          vbCritical + vbSystemModal, sTitle
     Wscript.Quit
   End If

   bFileOnly = True

Elseif oArgs.Count = 2 Then

   sSource = oArgs(0)
   sToFolder = oArgs(1)
   If Not oFSO.FolderExists(sSource) And Not oFSO.FolderExists(sToFolder)
Then
     MsgBox "Error: First and/or second parameter " _
          & "is not a folder!", _
          vbCritical + vbSystemModal, sTitle
     Wscript.Quit
   End If

Else
   MsgBox "Error: You need to supply a source file or path as input " _
        & "parameter (and optionally a target folder as a second)!", _
        vbCritical + vbSystemModal, sTitle
   Wscript.Quit
End If

' If the files exist in the folder from before (from a previous run),
' none of them must be RO or the script below will err. To avoid this,
' script runs the attrib command to remove any RO flags.
oShell.Run "attrib.exe /s -r " & sToFolder & "*", 0, True

If bFileOnly Then
   Set oShellLink = oShell.CreateShortcut(sSource)
   sLinkTargetPath = oShellLink.TargetPath

   If oFSO.FileExists(sLinkTargetPath) Then
     oFSO.CopyFile sLinkTargetPath, sToFolder & "\", True
   End If
Else
   Set oFiles = oFSO.GetFolder(sSource).Files

   For Each oFile In oFiles
     If LCase(oFSO.GetExtensionName(oFile)) = "lnk" Then
       Set oShellLink = oShell.CreateShortcut(oFile.Path)
       sLinkTargetPath = oShellLink.TargetPath
       If oFSO.FileExists(sLinkTargetPath) Then
         oFSO.CopyFile sLinkTargetPath, sToFolder & "\", True
       End If
     End If
   Next
End If

"Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com> wrote in message
news:%230C2Szc4EHA.2624@TK2MSFTNGP11.phx.gbl...
> Ed wrote:
>
>> I posted a message recently asking for help to copy files pointed to by
>> links to another destination and got a response directing me to the
>> VBScript listed below. It works, but has hardcoded source and
>> destination directories. Is it possible to modify this script so that it
>> accepts two command line arguments specifying the source and destination
>> directories? If so, how? Is it also possible to create a version of this
>> script that can be run without a command line, perhaps where I drag the
>> source LNK files to the script icon, causing it to run and copy the files
>> pointed to by the dragged LNK files to a destination directory hardcoded
>> in the script? Is there an even easier way to do this that I am missing?
> Hi
>
> The version below supports dragging of a link file to the script icon
> (with a hard coded target path), as well as two input parameters where
> the first one is a source folder and the second is the target folder.
>
> '--------------------8<----------------------
> Set oArgs = WScript.Arguments
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oShell = CreateObject("WScript.Shell")
>
> bFileOnly = False ' init value
>
> If oArgs.Count = 1 Then
>
> sSource = oArgs(0)
>
> ' need to hard code target in this case
> sToFolder = "c:\test"
>
> If Not oFSO.FileExists(sSource) And Not oFSO.FolderExists(sToFolder)
> Then
> MsgBox "Error: First parameter is not a file and/or Second " _
> & "parameter not a folder!", _
> vbCritical + vbSystemModal, sTitle
> Wscript.Quit
> End If
>
> If LCase(oFSO.GetExtensionName(sSource)) <> "lnk" Then
> MsgBox "Error: First parameter is not a shortcut file!", _
> vbCritical + vbSystemModal, sTitle
> Wscript.Quit
> End If
>
> bFileOnly = True
>
> Elseif oArgs.Count = 2 Then
>
> sSource = oArgs(0)
> sToFolder = oArgs(1)
> If Not oFSO.FolderExists(sSource) And Not oFSO.FolderExists(sToFolder)
> Then
> MsgBox "Error: First and/or second parameter " _
> & "is not a folder!", _
> vbCritical + vbSystemModal, sTitle
> Wscript.Quit
> End If
>
> Else
> MsgBox "Error: You need to supply a source file or path as input " _
> & "parameter (and optionally a target folder as a second)!", _
> vbCritical + vbSystemModal, sTitle
> Wscript.Quit
> End If
>
> ' If the files exist in the folder from before (from a previous run),
> ' none of them must be RO or the script below will err. To avoid this,
> ' script runs the attrib command to remove any RO flags.
> oShell.Run "attrib.exe /s -r " & sToFolder & "*", 0, True
>
> If bFileOnly Then
> Set oShellLink = oShell.CreateShortcut(sSource)
> sLinkTargetPath = oShellLink.TargetPath
> If oFSO.FileExists(sLinkTargetPath) Then
> oFSO.CopyFile sLinkTargetPath, sToFolder & "\", True
> End If
> Else
> Set oFiles = oFSO.GetFolder(sSource).Files
>
> For Each oFile In oFiles
> If LCase(oFSO.GetExtensionName(oFile)) = "lnk" Then
> Set oShellLink = oShell.CreateShortcut(oFile.Path)
> sLinkTargetPath = oShellLink.TargetPath
> If oFSO.FileExists(sLinkTargetPath) Then
> oFSO.CopyFile sLinkTargetPath, sToFolder & "\", True
> End If
> End If
> Next
> End If
>
> '--------------------8<----------------------
>
>
> --
> torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
> Administration scripting examples and an ONLINE version of
> the 1328 page Scripting Guide:
> http://www.microsoft.com/technet/scriptcenter/default.mspx



Relevant Pages

  • Re: need help with a VBScript - copies files behind links to another location
    ... I'd like to figure out how to drag a set of links or an entire folder ... 'The version below supports dragging of a link file to the script icon ... 'the first one is a source folder and the second is the target folder. ... For Each oFile In oFiles ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: need help with a VBScript - copies files behind links to another location
    ... I'd like to figure out how to drag a set of links or an entire folder ... 'The version below supports dragging of a link file to the script icon ... 'the first one is a source folder and the second is the target folder. ... For Each oFile In oFiles ...
    (microsoft.public.scripting.vbscript)
  • Re: readdir formulated badly? gives wrong count
    ... Sometimes there might be some numbered files already in the target ... I'll probably try to turn the perl script into something more general ... You are doing boolean tests on the contents of array elements when you ... sub usage { ...
    (perl.beginners)
  • Re: .old files being created by root
    ... The external program running on another server but connected to the overall system network apparently picks them up with an ftp program. ... the other end must be running a script (not a human logging in ... She should be in the target directory or some directory ... you never said if this is an SCO UNIX OS or Linux or some other UNIX. ...
    (comp.unix.sco.misc)
  • Re: kbuild make deb patch
    ... > target in any way. ... +# Simple script to generate a deb package for a Linux kernel. ... +# package install and removal. ... +cat <<EOF> debian/changelog ...
    (Linux-Kernel)