Solved

VBScript File already exist

Posted on 2010-11-17
7
1,692 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

This article outlines the process to identify and resolve account lockout in an Active Directory environment.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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