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
答案 0 :(得分:1)
制作
If fso.FileExists(strMyPath) Then
'work',strMyPath必须包含有效的文件规范。据我所知,在你的代码中它包含了(目标?)文件夹的路径。
使用正确命名的变量(名称)来明确它们是否包含文件夹或文件规范。
答案 1 :(得分:1)
确定。所以看起来应该是这样的:
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