首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBS MoveFile问题

VBS MoveFile问题
EN

Stack Overflow用户
提问于 2014-08-08 10:42:19
回答 1查看 1.7K关注 0票数 1

我确实写了第一条if语句,它在一开始运行良好,而我的文件大小只是在kbs中。

当我的文件大小达到mbs时,它开始给我带来一些问题,比如它没有从.CSV重新将文件重命名为.CSV.tmp,因此我决定在文件大小之前和之后添加文件大小(第二个IF语句),但它开始向我显示2个debug msgBox :Entry in:“然后再添加"fname”。在那之后,它失败了,没有给予和错误,但也不要做这项工作。

你觉得有什么问题吗?

代码语言:javascript
复制
Option Explicit
Dim FSO, FLD, FIL, GetFileSize1, GetFileSize2, f, fname
Dim strFolder,strFileName

strFolder = "\Sieb\Request"
Set FSO = CreateObject("Scripting.FileSystemObject")
set FLD = FSO.GetFolder(strFolder)

For Each Fil In FLD.Files
  strFileName=Lcase(Fil.Name)

  If instr (1,strFileName,"_a_request_")>0 then
    if (FSO.FileExists(".\Sieb_Process_Files\a\Request\"+Fil.Name)) then
    else
      FSO.MoveFile ".\Request\"+Fil.Name,".\Sieb_Process_Files\a\Request\" + Fil.Name +".tmp"
      FSO.MoveFile ".\Sieb_Process_Files\a\Request\" + Fil.Name +".tmp", ".\Sieb_Process_Files\a\Request\" + Fil.Name 
    end if
  End if

  If instr (1,strFileName,"_b_request_")>0 then
    if (FSO.FileExists(".\Sieb_Process_Files\b\Request\"+Fil.Name)) then
    else
      MsgBox "Entry in B Request with File : " + Fil.Name
      set fname = ".\Sieb_Process_Files\b\Request\" + Fil.Name
      MsgBox fname
      set f = FSO.GetFile(fname) 
      GetFileSize1 = f.size 
      MsgBox "Orignal File Size" + GetFileSize1
      FSO.MoveFile ".\Request\"+Fil.Name,".\Sieb_Process_Files\b\Request\" + Fil.Name +".tmp"
      set f = FSO.GetFile(".\Sieb_Process_Files\b\Request\" + Fil.Name +".tmp") 
      GetFileSize2 = f.size 
      MsgBox "Copied File Size" + GetFileSize2
      MsgBox "File Moved with tmp name"
      Do Until GetFileSize1=GetFileSize2
        FSO.MoveFile ".\Sieb_Process_Files\b\Request\" + Fil.Name +".tmp" ,".\Sieb_Process_Files\b\Request\" + Fil.Name 
        MsgBox "File renamed to orignal name exiting now"
        Exit Do
      Loop
    end if
  End if
Next
Set Fil = nothing
set FSO = nothing
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-08 14:50:03

显然,您的第一个If条件不可能成功地移动目标中不存在的文件(而且文件大小与其无关)。

代码语言:javascript
复制
if (FSO.FileExists(".\Sieb_Process_Files\a\Request\"+Fil.Name)) then
else
  FSO.MoveFile ".\Request\"+Fil.Name,".\Sieb_Process_Files\a\Request\" + Fil.Name +".tmp"
  FSO.MoveFile ".\Sieb_Process_Files\a\Request\" + Fil.Name +".tmp", ".\Sieb_Process_Files\a\Request\" + Fil.Name 
end if

一旦将文件移动到变量Fil引用的文件的不同位置/名称,那么第二个MoveFile (或者更确切地说,访问属性Fil.Name的尝试)将引发一个“Found”错误。如果您没有得到一个错误,您的代码中有一个没有显示的On Error Resume Next

另外,为什么要将文件移动到临时名称,然后返回到同一个目录中的原始名称?这只有在将文件移动到不同卷上的热文件夹时才有意义,这里的情况似乎并非如此。

这样的东西应该是你所需要的:

代码语言:javascript
复制
Option Explicit

Dim fso, sourceFolder, targetFolder, f, target

sourceFolder = "\Sieb\Request"
targetFolder = ".\Sieb_Process_Files\a\Request"

Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder(sourceFolder).Files
  target = fso.BuildPath(targetFolder, f.Name)

  If InStr(1, f.Name, "_a_request_", vbTextCompare) > 0 Then
    If Not fso.FileExists(target) Then
      f.Move target
    End If
  End If
Next

vbTextCompare使InStr()比较不区分大小写,因此不需要小写文件名。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25201660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档