Vbscript Vbs CopyFile到用户选择的文件夹工作不正常

Vbscript Vbs CopyFile到用户选择的文件夹工作不正常,vbscript,Vbscript,我的Vbscript用于提示选择文件夹,然后将文件复制到所选文件夹 我的代码是: Option Explicit Dim strPath, pth, fso strPath = SelectFolder( "" ) pth = """" & strPath & "\" & """" If strPath = vbNull Then WScript.Echo "Cancelled" Else Set fso = CreateObject("Scripting.F

我的Vbscript用于提示选择文件夹,然后将文件复制到所选文件夹 我的代码是:

Option Explicit
Dim strPath, pth, fso

strPath = SelectFolder( "" )
pth = """" & strPath & "\" & """"

If strPath = vbNull Then
  WScript.Echo "Cancelled"
Else
  Set fso = CreateObject("Scripting.FileSystemObject")

fso.CopyFile "H:\new\file.txt", strPath

' fso.CopyFile "H:\new\file.txt", pth

msgbox("Copy DONE")
End If


Function SelectFolder( myStartFolder )

Dim objFolder, objItem, objShell

On Error Resume Next
SelectFolder = vbNull

Set objShell  = CreateObject( "Shell.Application" )
Set objFolder = objShell.BrowseForFolder( 0, "Select Folder Please", 0, myStartFolder )

If IsObject( objfolder ) Then SelectFolder = objFolder.Self.Path

Set objFolder = Nothing
Set objshell  = Nothing
On Error Goto 0
End Function
当我使用fso.CopyFile“H:\new\file.txt”时,strPath仅在路径类似于c:\或d:\时复制到路径,而不将文件复制到类似于“d:\文件夹\测试\”的位置。如果选中,则显示错误-“PERMISSON DENIED”,但路径未设置任何属性

当我使用fso.CopyFile“H:\new\file.txt”pth时,它会显示错误,对于任何文件夹选择,“错误的文件名或编号”


我该怎么办?请帮助

我将第5行更改为以下内容:

pth = strPath & "\"

然后使用“fso.CopyFile”H:\new\file.txt“pth”行进行复制。这很有效。

我发现,如果用户选择d:\drive,那么它的长度是3 所以我编辑的代码是

dim ptlen,finalpatg
ptlen=len(strPath)
if ptlen = 3 then //for only drive selection the ptlen will be 3 so no need to add \
finalpath=strPath
else
 finalpath=strPath & "\" //add slash to path if ptlen not =3
end if

fso.CopyFile "H:\new\file.txt",finalpath

它成功地工作了

请有人帮帮我谢谢@Blueberr,但它能在选择C驱动器上工作吗。为此,在添加\符号后,pth将看起来像C:\\,它会工作吗?–它对我在Windows7上的测试有效。但是请记住,默认情况下,如果不以管理员身份运行,就无法复制到C:的根目录。如果您使用了加倍的反斜杠,则可以使用pth=strPath&“\”如果正确(pth,1)”\”然后使用pth=pth&“\”如果您始终希望路径以单个反斜杠结束,也可以使用
BuildPath()
函数。例如,
fso.BuildPath(strPath,“\”)
。谢谢大家。但我喜欢蓝耳环和它的工作。