Solved

Posted on 2010-11-17
1,617 Views
Hi,

Hi the error I get now is file already exist.

Set WshShell = WScript.CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

'##Get The Name Of The Local Machine
sComputerName = WScript.CreateObject("WScript.Network").ComputerName

'## Declare The Name Of The Remote Folder And File
sRemoteFolder = "\\ldnbm01\itd\Ahmed\"
sProgramName = "HP.docx"

'## Declare The Name Of The Local Folder
sLocalFolder = "\\ldndc02\DeptDesktops\itd"

'## Now We Copy The File

oFSO.CopyFile sRemoteFolder & sProgramName, sLocalFolder, OverwriteExisting

Set WshShell = Nothing
Set oFSO = Nothing
0
Question by:ahmedla1
• 4
• 2

LVL 11

Expert Comment

ID: 34155603
You could add this to your script above '## Now We Copy The File

' Check if file exists to prevent error
If oFSO.FileExists(sRemoteFolder & sProgramName) Then
oFSO.DeleteFile sRemoteFolder & sProgramName
End If

Someone else may offer a cleaner solution or a reason why your OverwriteExisting property isn't functioning correctly, but this should do the trick for you.
0

Author Comment

ID: 34156135
Hi

'on error resume next
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

'##Get The Name Of The Local Machine
sComputerName = WScript.CreateObject("WScript.Network").ComputerName

'## Declare The Name Of The Remote Folder And File
sRemoteFolder = "\\ldnbm01\itd\Ahmed\"
sProgramName = "HP.docx"

'## Declare The Name Of The Local Folder
sLocalFolder = "\\ldndc02\DeptDesktops\itd"

'## Check if file exists to prevent error
If oFSO.FileExists(sRemoteFolder & sProgramName) Then
oFSO.DeleteFile sRemoteFolder & sProgramName
End If

'## Now We Copy The File
msgbox sRemoteFolder
msgbox sProgramName
msgbox sLocalFolder

oFSO.CopyFile sRemoteFolder & sProgramName, sLocalFolder, OverwriteExisting

Set WshShell = Nothing
Set oFSO = Nothing
0

Author Comment

ID: 34156155
I am copy from one server to another. Will that make a difference?
0

LVL 11

Expert Comment

ID: 34156386
Add a backslash to the end of this line (mine's changed below).

sLocalFolder = "\\ldndc02\DeptDesktops\itd\"

Referencing this: http://technet.microsoft.com/en-us/library/ee198744.aspx
"When specifying the destination folder, it is important to include the trailing backslash (for example, D:\Archive\). If the backslash is there, CopyFile will copy the file into the Archive folder. If the backslash is not there, CopyFile will try to create a new file named D:\Archive. If the folder D:\Archive already exists, a "Permission denied error" will be generated, and the copy procedure will fail."

You may also need to remove/comment out the OverwriteExisting property since you're deleting the file in advance and doing a full replace.
0

Author Comment

ID: 34156934
This seems to work but the IF statement is ignored.

'on error resume next
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

'##Get The Name Of The Local Machine
sComputerName = WScript.CreateObject("WScript.Network").ComputerName

'## Declare The Name Of The Remote Folder And File
sRemoteFolder = "\\ldnbm01\itd\Ahmed\"
sProgramName = "test.doc"

'## Declare The Name Of The Local Folder
sLocalFolder = "\\ldndc02\DeptDesktops\itd\"

'## Check if file exists to prevent error
If oFSO.FileExists(sLocalFolder) Then
oFSO.DeleteFile sLocalFolder
End If

'## Now We Copy The File
msgbox sRemoteFolder
msgbox sProgramName
msgbox sLocalFolder

oFSO.CopyFile sRemoteFolder & sProgramName, sLocalFolder

Set WshShell = Nothing
0

LVL 12

Accepted Solution

Daz_1234 earned 500 total points
ID: 34162831
Hi,

SeanStrickland is correct about the backslash, but unfortunately his first post deleted the source file before copying it.

The main reason I think the copy was not working is because  you were specifying 'OverwriteExisting' without
setting it.

If you want to use the variable you also need somewhere earlier:
OverwriteExisting = True

... BUT - you do not need it at all because Overwrite = True by default.  You only need it if you want to force it to False.

Now the IF is not working because you are using FileExists to test for  the existence of a folder.  I imagine that you do not want to delete the whole folder (if you do then you'd need to create it again because the copy would not create it for you).  Therefore I think that you need to check for the source file, but not to delete it, just so that the copy does not fail if it is not there.

SO, here's my attempt :o)

Regards,
Daz.
Set WshShell = WScript.CreateObject("WScript.Shell")

Set oFSO = CreateObject("Scripting.FileSystemObject")

'## Get The Name Of The Local Machine

sComputerName = CreateObject("WScript.Network").ComputerName

'## Declare The Name Of The Remote Folder And File

sRemoteFolder = "\\ldnbm01\itd\Ahmed\"

sProgramName = "HP.docx"

'## Declare The Name Of The Local Folder

sLocalFolder = "\\ldndc02\DeptDesktops\itd\"

'## Check if source file exists, if so, copy it.

If oFSO.FileExists(sRemoteFolder & sProgramName) Then

'## Now We Copy The File

oFSO.CopyFile sRemoteFolder & sProgramName, sLocalFolder

Else

MsgBox "Source file: " & sRemoteFolder & sProgramName & " not found, copy not run", vbExclamation, "Copy Not Run"

End If

Set WshShell = Nothing

Set oFSO = Nothing

0

Author Comment

ID: 34164330
Hi

0

### Suggested Solutions

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
Synchronize a new Active Directory domain with an existing Office 365 tenant
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…