我确实写了第一条if语句,它在一开始运行良好,而我的文件大小只是在kbs中。
当我的文件大小达到mbs时,它开始给我带来一些问题,比如它没有从.CSV重新将文件重命名为.CSV.tmp,因此我决定在文件大小之前和之后添加文件大小(第二个IF语句),但它开始向我显示2个debug msgBox :Entry in:“然后再添加"fname”。在那之后,它失败了,没有给予和错误,但也不要做这项工作。
你觉得有什么问题吗?
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发布于 2014-08-08 14:50:03
显然,您的第一个If条件不可能成功地移动目标中不存在的文件(而且文件大小与其无关)。
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。
另外,为什么要将文件移动到临时名称,然后返回到同一个目录中的原始名称?这只有在将文件移动到不同卷上的热文件夹时才有意义,这里的情况似乎并非如此。
这样的东西应该是你所需要的:
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
NextvbTextCompare使InStr()比较不区分大小写,因此不需要小写文件名。
https://stackoverflow.com/questions/25201660
复制相似问题