在遍历文件时使用FileSystemObject重命名文件 [英] Rename a file with FileSystemObject while looping through files

查看:477
本文介绍了在遍历文件时使用FileSystemObject重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为序言,我正在使用Access 2003编写代码,但将使用户使用Access 2013,因此,我需要它与两者兼容。我有一个循环,该循环使用Application.FileSearch遍历目录中的许多文件。我的理解是,在较新版本的Access中已弃用了此功能,因此我必须使用 For Each来遍历文件。

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.

这是我的一段代码正在更改:

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

这是我要替换的代码

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

此代码循环遍历每个文件,然后启动一个Process()函数,该函数对文件进行更改。因此,我的工作方式是将活动文件更改为 SourceCode.txt,然后Process()函数知道可以使用该名称的文件。然后,将文件移至带有原始文件名的已处理子文件夹。

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之外。我尝试了几种方法,但始终出错。在上面的代码中,我尝试了 f1.Name = strFileName。这给我一个权限被拒绝错误。我尝试使用FileCopy,然后使用原始文件上的Kill命令,但Kill命令返回错误。我怀疑FileSystemObject正在锁定文件,因此无法移动或杀死该文件。但是旧版本的代码可以毫无问题地移动它。

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.

推荐答案

您会因为以下原因而获得权限被拒绝尝试在使用文件时重命名文件,即通过 objFSO.OpenTextFile 重命名。这与 fs.GetFile 不同,并且不在您的原始代码中-您需要吗?它返回您随后不再使用的 TextStream

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.

无论如何,在打开文件之前或完成操作后,将 f1.Name = strFileName 移至处理中,调用 objFile.Close ,然后重命名。

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.

这篇关于在遍历文件时使用FileSystemObject重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆