Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBScript File already exist

Posted on 2010-11-17
7
Medium Priority
?
1,890 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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
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.
Suggested Courses

971 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