Reputation: 588
sry guys m new to programming. I was trying to make a vbscript to cope a vbs file from current location to system startup folder. But m getting the error bad file name or number. but when i give path manually it works like a charm. The path my code is self picking is also correct. Cant understand what is the problem. Please help me. Here is my code.
Set objShell = Wscript.CreateObject("Wscript.Shell")
strMyPath = objShell.SpecialFolders("Startup")
wscript.echo strPath
wscript.echo strMyPath
'Const strMyPath = "C:\Users\Bilal\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
Const SourceFile = "abc.vbs"
Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file already exists in the destination folder
If fso.FileExists(strMyPath) Then
'Check to see if the file is read-only
If Not fso.GetFile(strMyPath).Attributes And 1 Then
'The file exists and is not read-only. Safe to replace the file.
fso.CopyFile SourceFile, strMyPath, True
Else
'The file exists and is read-only.
'Remove the read-only attribute
fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes - 1
'Replace the file
fso.CopyFile SourceFile, strMyPath, True
'Reapply the read-only attribute
fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes + 1
End If
Else
'The file does not exist in the destination folder. Safe to copy file to this folder.
fso.CopyFile SourceFile, myStrPath, True
End If
Set fso = Nothing
Upvotes: 2
Views: 8774
Reputation: 4170
Ok. So it should look like this:
Set objShell = Wscript.CreateObject("Wscript.Shell")
strPath = objShell.SpecialFolders("Startup")
strMyPath = strPath&"\"
Const SourceFile = "abc.vbs"
strMyPath = strMyPath & SourceFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file already exists in the destination folder
If fso.FileExists(strMyPath) Then
'Check to see if the file is read-only
If Not fso.GetFile(strMyPath).Attributes And 1 Then
'The file exists and is not read-only. Safe to replace the file.
fso.CopyFile SourceFile, strMyPath, True
Else
'The file exists and is read-only.
'Remove the read-only attribute
fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes - 1
'Replace the file
fso.CopyFile SourceFile, strMyPath, True
'Reapply the read-only attribute
fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes + 1
End If
Else
'The file does not exist in the destination folder. Safe to copy file to this folder.
fso.CopyFile SourceFile, strMyPath, True
End If
Set fso = Nothing
Upvotes: 1
Reputation: 38745
To make
If fso.FileExists(strMyPath) Then
'work', strMyPath must contain a valid file specification. As far as I can see, in your code it contains the path to the (destination?) folder.
Use properly named variable (names)s that make clear whether they hold folder or file specs.
Upvotes: 1