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
Solved

VBScript File already exist

Posted on 2010-11-17
7
1,660 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
  • 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

839 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