vba 在循环文件时使用 FileSystemObject 重命名文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18130149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rename a file with FileSystemObject while looping through files
提问by Michael T
As a preface, I'm writing code in Access 2003, but will have users using Access 2013, and so I need it to be compatible for both. I have a loop that uses the Application.FileSearch to loop through a number of files in a directory. It is my understanding that this is deprecated in newer version of Access, and so I have to use "For Each" to loop through files.
作为前言,我正在 Access 2003 中编写代码,但会有用户使用 Access 2013,因此我需要它与两者兼容。我有一个循环,它使用 Application.FileSearch 遍历目录中的多个文件。据我了解,这在较新版本的 Access 中已被弃用,因此我必须使用“For Each”来循环访问文件。
Here's the piece of code I'm changing:
这是我正在更改的一段代码:
strPath = CurrentProject.Path & "\Files\"
strFileName = "SourceCode.txt"
With Application.FileSearch
.FileName = "*.txt"
.LookIn = strPath
.Execute
intFileCount = .foundfiles.Count
For x = 1 To intFileCount
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(.foundfiles(x))
strNewFileName = Right((.foundfiles(x)), Len((.foundfiles(x))) - (InStr((.foundfiles(x)), "Files\") + 5))
fs.MoveFile f, strPath & strFileName
'Run the Process() function
Process
FileCopy strPath & strFileName, strPath & "Processed\" & strNewFileName
Kill strPath & strFileName
Next x
End With
And here's the code I'm replacing it with:
这是我将其替换为的代码:
strPath = CurrentProject.Path & "\Files\"
strFileName = "SourceCode.txt"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(strPath)
Set fc = f.Files
For Each f1 In fc
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strPath & f1.Name, 1)
strNewFileName = f1.Name
f1.Name = strFileName
'Run the Process() function
Process
FileCopy strPath & strFileName, strPath & "Processed\" & strNewFileName
Kill strPath & strFileName
Next
This code loops through each file, then launches a Process() function that makes changes to the file. So the way I have it working is it changes the active file to be named "SourceCode.txt", then the Process() function knows to work with the file of that name. Then it moves the file to the "Processed" subfolder, with its original file name.
此代码循环遍历每个文件,然后启动一个 Process() 函数来更改文件。所以我的工作方式是将活动文件更改为“SourceCode.txt”,然后 Process() 函数知道使用该名称的文件。然后它将文件移动到“已处理”子文件夹,并使用其原始文件名。
This worked fine in the original code. The new code seems to mostly work, except I can't find a way to rename the file to "SourceCode.txt" prior to launching Process(). I tried several ways but I keep getting errors. In the code above, I tried "f1.Name = strFileName". This gives me a "Permission Denied" error. I tried alternatively to use FileCopy, and then the Kill command on the original file, but the Kill command returned an error. I suspect the FileSystemObject is locking the file so it can't be moved or killed. But the old version of the code was able to move it with no problem.
这在原始代码中运行良好。新代码似乎主要工作,除了我找不到在启动 Process() 之前将文件重命名为“SourceCode.txt”的方法。我尝试了几种方法,但我不断收到错误消息。在上面的代码中,我尝试了“f1.Name = strFileName”。这给了我一个“权限被拒绝”错误。我尝试交替使用 FileCopy,然后在原始文件上使用 Kill 命令,但 Kill 命令返回错误。我怀疑 FileSystemObject 正在锁定文件,因此无法移动或杀死它。但是旧版本的代码能够毫无问题地移动它。
回答by mr.Reband
You get "Permission Denied" because you are attempting to rename a file when it is in use, namely by objFSO.OpenTextFile
. This is different than fs.GetFile
and not in your original code - do you need it? It returns a TextStream
that you are not subsequently using.
您收到“权限被拒绝”是因为您试图在文件使用时重命名文件,即通过objFSO.OpenTextFile
. 这fs.GetFile
与您的原始代码不同而不是不同- 您需要它吗?它返回TextStream
您随后未使用的 。
In any event, move the f1.Name = strFileName
to before opening the file, or after you are done processing, call objFile.Close
and then rename.
在任何情况下,f1.Name = strFileName
在打开文件之前或在完成处理后,调用objFile.Close
并重命名。