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?
Thanks for your help!
Ed Self
~~~~~~~~~~~~~~~~~~~~~~
sToFolder = "c:\test\backup\"
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
' 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
' Using the desktop folder as an example
'sFolderWithLinks = oShell.SpecialFolders("Desktop")
' For a hard coded path, uncomment the following line and edit the path
sFolderWithLinks = "c:\test"
Set oFiles = oFSO.GetFolder(sFolderWithLinks).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
You can use this code to get started.
---
Dim objArgs, nArgs
Dim strToFolder, strFromFolder
Set objArgs = WScript.Arguments
If (objArgs.Count > 1) Then
strFromFolder = objArgs(0)
strToFolder = objArgs(1)
Wscript.Echo "From: " & strFromFolder
Wscript.Echo "To: " & strToFolder
Else
WScript.Echo "You have to specify two arguments"
End If
---
If you drag a file to the script that file will be used as the first (and
only) argument to the script, so you shouldn't have a problem with doing
that.
Patrick Ogenstad
--
Simplifying Network Documentation
http://sydi.sourceforge.net/
"Ed" <anon...@discussions.com> wrote in message
news:uRdEZ0a4...@TK2MSFTNGP11.phx.gbl...
> 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
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)
sSource = oArgs(0)
bFileOnly = True
"Torgeir Bakken (MVP)" <Torgeir.B...@hydro.com> wrote in message
news:%230C2Szc...@TK2MSFTNGP11.phx.gbl...
> 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!
Hi
New version listed below. Under the script are also some links
to some scripting resources.
'--------------------8<----------------------
'
' Script that locates target files in link (shortcut) files
' and copies them to a target folder.
'
' It supports the following operations:
'
' 1)
' Dragging of one or more link file(s) to the script icon
' (script will then use a hard coded target path)
'
' 2)
' Dragging of *one* folder to the script icon
' or add the folder to the command line.
' All link files in this folder will then be enumerated
' (script will then use a hard coded target path)
'
' 3)
' On the command line, add two paths, the first one is source folder
' and the second is the target folder.
' Note that it is a pre-requisite that the target folder exists
' for all methods above!
'
' Author: Torgeir Bakken
' Date: 2004-12-14
'
Set oArgs = WScript.Arguments
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
bFilesOnly = True ' init value
' hard coded target folder for the the cases where
' second parameter is not a folder
sToFolder = "c:\test"
If oArgs.Count = 0 Then
MsgBox "Error: You need to supply shortcut file(s) or path as input " _
& "parameter (and optionally, if first parameter is a folder, " _
& "a target folder as a second parameter)!", _
vbCritical + vbSystemModal, sTitle
Wscript.Quit
Elseif oArgs.Count = 1 And oFSO.FolderExists(oArgs(0)) Then
sSource = oArgs(0)
bFilesOnly = False
Elseif oArgs.Count = 2 Then
' test if both input parameters are folders
If oFSO.FolderExists(oArgs(0)) And oFSO.FolderExists(oArgs(1)) Then
sSource = oArgs(0)
sToFolder = oArgs(1)
bFilesOnly = False
End If
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 -r " & sToFolder & "\*", 0, True
If bFilesOnly Then
For i = 0 to oArgs.Count - 1
sSource = oArgs(i)
If LCase(oFSO.GetExtensionName(sSource)) = "lnk" Then
Set oShellLink = oShell.CreateShortcut(sSource)
sLinkTargetPath = oShellLink.TargetPath
If oFSO.FileExists(sLinkTargetPath) Then
oFSO.CopyFile sLinkTargetPath, sToFolder & "\", True
End If
End If
Next
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<----------------------
WSH 5.6 documentation (local help file) can be downloaded from here
if you haven't got it already:
http://msdn.microsoft.com/downloads/list/webdev.asp
For a list of some scripting resources and links to some Windows Script
Host (WSH) Web introductions, take a look here:
http://groups.google.com/groups?selm=3FFC3C56...@hydro.com
"Torgeir Bakken (MVP)" <Torgeir.B...@hydro.com> wrote in message
news:uwAoFgg4...@tk2msftngp13.phx.gbl...
> Thank you VERY MUCH for this help! I've never known that such
> scripts were possible. It makes me want to dive in and learn
> more. I can imagine so many possibilities!
Hi
You are very welcome, and yes, a new world is opening up if you
learn some scripting :-)