Vbs CopyFile到User Selected Folder无法正常工作

时间:2014-05-28 11:12:43

标签: vbscript

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

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:\ folder \ test \ '如果选中,则显示错误 - " PERMISSON DENIED ",但路径未设置任何属性

当我使用 fso.CopyFile" H:\ new \ file.txt",pth 时,它显示错误 - " 错误的文件名或号码"任何文件夹选择

我该怎么办?请帮忙

2 个答案:

答案 0 :(得分:0)

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

pth = strPath & "\"

然后使用'fso.CopyFile“H:\ new \ file.txt”,第p行进行复制。这很有用。

答案 1 :(得分:0)

我发现如果用户选择d:\ drive,那么它的lenth是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

它成功运作