【发布时间】:2016-03-22 14:18:01
【问题描述】:
我有一段 VBS 代码,其中应将文件 (C:\test.txt) 复制到新创建的临时文件夹中。但它无法复制文件。奇怪的是,当我在源参数 (C:\ *est.txt) 中有通配符时,相同的函数可以正常工作。任何建议都会很有帮助。
set fso = createobject("Scripting.FileSystemObject")
if fso.FileExists(src_temp) then
'src_temp contains the file path.
'Path of the temporary folder
Set tmp_fld = fso.GetSpecialFolder(TemporaryFolder)
tmp_fld = tmp_fld & "\OldFiles_tmp"
'Create the temporary folder if does not exist
If Not fso.FolderExists(tmp_fld) Then
fso.CreateFolder(tmp_fld)
End If
'Copy the files to temporary path
On Error Resume Next
fso.CopyFile src_temp, tmp_fld, True 'last parameter is set as true for overwriting the existing
On Error Goto 0
End If
我已验证是否创建了目标临时文件夹以及路径和其他内容。路径中的通配符如何使 CopyFile 起作用,而对于完整的文件名则不起作用。还有怎么解决这个问题?
【问题讨论】:
-
尝试将文件名也添加到目的地
-
这可能与 windows 无法正确检测 tmp_fld 应该是文件还是文件夹有关。在这种情况下,如果将 tmp_fld = tmp_fld & "\OldFiles_tmp" 替换为 tmp_fld = tmp_fld & "\OldFiles_tmp\" (尾随退格键)会有所帮助。不过,这只是一个疯狂的猜测。
-
src_temp contains the file path... 是否包含文件名? -
是的。添加尾随退格有效。谢谢。
标签: vbscript filesystemobject file-copying