Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Missing Flash Drive Aborts VBScript Copying

Status
Not open for further replies.

stevemarks59

Technical User
Apr 8, 2010
22
US
My OS: XP-PRO SP3

I use the VBScript below to copy files and folders to different drives. This script allows my folders to retain their custom icons when copied. One of the destination folders "N:\APPS" is on a USB flash drive . I have discovered if my flash drive is not connected the script aborts the copying process when it can't find the drive. I would like to know if the script can be modified to ignore non existing destinations and continue to copy to the other valid destinations. I realize I could write the script so the copying to the flash drive would be the last operation but I am interested in finding out if this script could be modified as I explained above.


On Error Resume Next
Const OverwriteExisting = True
set args = wscript.Arguments
set objShell = CreateObject("Shell.Application" )

Set objFolder = objShell.NameSpace("D:\Program Files" )
if not objFolder is nothing then
for each item in args
objFolder.CopyHere item, 16
next

Set objFolder = objShell.NameSpace("N:\APPS" )
if not objFolder is nothing then
for each item in args
objFolder.CopyHere item, 16
next

Set objFolder = objShell.NameSpace("W:\APPS" )
if not objFolder is nothing then
for each item in args
objFolder.CopyHere item, 16
next

Set objFolder = objShell.NameSpace("X:\APPS" )
if not objFolder is nothing then
for each item in args
objFolder.CopyHere item, 16
next

End If
End If
End If
End If












 
youve got your 'end if's in the wrong place. if you indented you code you would find this easier to spot. in addition, your script has no comments, no echo's to the screen, if it did it would have shown you what was going wrong

Set objFolder = objShell.NameSpace("D:\Program Files" )
if not objFolder is nothing then
for each item in args
objFolder.CopyHere item, 16
next
End If
Set objFolder = Nothing
'
Set objFolder = objShell.NameSpace("N:\APPS" )
if not objFolder is nothing then
for each item in args
objFolder.CopyHere item, 16
next
End If
Set objFolder = Nothing
'
Set objFolder = objShell.NameSpace("W:\APPS" )
if not objFolder is nothing then
for each item in args
objFolder.CopyHere item, 16
next
End If
Set objFolder = Nothing
'
Set objFolder = objShell.NameSpace("X:\APPS" )
if not objFolder is nothing then
for each item in args
objFolder.CopyHere item, 16
next
End If
Set objFolder = Nothing


I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Thank you very much mrmovie. Your modification made the script do exactly what I wanted. I only had to include the third and fourth lines from the original script. The script I will use now is:

set args = wscript.Arguments
set objShell = CreateObject("Shell.Application" )

Set objFolder = objShell.NameSpace("D:\Program Files" )
if not objFolder is nothing then
for each item in args
objFolder.CopyHere item, 16
next
End If
Set objFolder = Nothing
'
Set objFolder = objShell.NameSpace("N:\APPS" )
if not objFolder is nothing then
for each item in args
objFolder.CopyHere item, 16
next
End If
Set objFolder = Nothing
'
Set objFolder = objShell.NameSpace("W:\APPS" )
if not objFolder is nothing then
for each item in args
objFolder.CopyHere item, 16
next
End If
Set objFolder = Nothing
'
Set objFolder = objShell.NameSpace("X:\APPS" )
if not objFolder is nothing then
for each item in args
objFolder.CopyHere item, 16
next
End If
Set objFolder = Nothing


 
I am really amazed and appreciate the code you gave me. While using it today I discovered it also did something I really wanted the script to do but was told on several forums it was not possible. Here is what I asked:

Do you know how this script could be modified so it would ignore the error if the source and destination were the same. This would allow me to have the same script in my folders on various drives. The original copy script you gave me a while back would ignore the error and continue. I tried inserting these two lines from the original script

------------------------------------
On Error Resume Next
Const OverwriteExisting = True
-------------------------------

to the to the top of the new script but it did not make a difference. I realize it may not be possible with this code but I figured if anyone would know how chances are it would be you. Regardless, I am very happy with this script and this is only a very minor detail I would like to change. Thanks.



The reply I was given:

Posted 25 October 2010 - 06:30 AM
The error you get is an Explorer error and not a script error. This because the method uses the explorer shell to copy the items.
So using the On Error Resume Next wont help in this one.. The only work around would be (If exists) to search for an other VB method which also copies the attributes. :)




 
Is it possible to eliminate the prompt?

"Error Copying File or Folder
Cannot copy******* .The source and Destination are the same
OK "
I would like the script to automatically answer "OK".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top