Solved

VBScript File already exist

Posted on 2010-11-17
7
1,682 Views
Last Modified: 2012-05-10
Hi,
I receive error of ' File Alread Exist' 800A003A. Please advise.

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
sComputerNameDist = CreateObject("ADSystemInfo").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
Comment
Question by:ahmedla1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 11

Expert Comment

by:SeanStrickland
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

by:ahmedla1
ID: 34156135
Hi

Thanks for your reply. Now I receive a different error (800A0035) 'File does not exist'

Please advise.

'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
sComputerNameDist = CreateObject("ADSystemInfo").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

by:ahmedla1
ID: 34156155
I am copy from one server to another. Will that make a difference?
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 11

Expert Comment

by:SeanStrickland
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

by:ahmedla1
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
sComputerNameDist = CreateObject("ADSystemInfo").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 13

Accepted Solution

by:
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
sComputerNameDist = CreateObject("ADSystemInfo").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

Open in new window

0
 

Author Comment

by:ahmedla1
ID: 34164330
Hi

Thanks for your version.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

756 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