서문으로 Access 2003에서 코드를 작성하고 있지만 Access 2013을 사용하는 사용자가 있으므로 둘 모두에 호환되어야합니다. Application.FileSearch를 사용하여 디렉터리의 여러 파일을 반복하는 루프가 있습니다. 이것이 새로운 버전의 Access에서는 더 이상 사용되지 않으므로 "For Each"를 사용하여 파일을 반복해야한다는 것을 알고 있습니다. 여기 파일을 반복하면서 FileSystemObject로 파일 이름 바꾸기
는 내가 변화하고있어 코드의 조각이다 :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
이 코드는 각각의 파일을 통해 루프는 다음 프로세스를 시작합니다() 함수는 파일을 변경합니다. 그래서 내가 작동하는 방식은 "SourceCode.txt"라는 이름의 활성 파일을 변경 한 다음 Process() 함수가 해당 이름의 파일로 작업하는 것을 알고 있습니다. 그런 다음 원래 파일 이름을 사용하여 파일을 "처리 된"하위 폴더로 이동합니다.
원본 코드에서 정상적으로 작동합니다. 새 코드는 Process()를 시작하기 전에 파일 이름을 "SourceCode.txt"로 바꿀 수있는 방법을 찾지 못하는 것을 제외하고 대부분 효과가있는 것 같습니다. 몇 가지 방법을 시도했지만 오류가 계속 발생합니다. 위의 코드에서 "f1.Name = strFileName"을 시도했습니다. 이렇게하면 "Permission Denied"오류가 발생합니다. 필자는 FileCopy를 사용하려고 시도한 다음 원래 파일에서 Kill 명령을 시도했지만 Kill 명령이 오류를 반환했습니다. 필자는 FileSystemObject가 파일을 잠그고 있기 때문에 파일을 옮기거나 죽일 수 없다고 생각합니다. 그러나 이전 버전의 코드는 문제없이 이동할 수있었습니다.
당신은 절대적으로 맞습니다. 이 함수에서 파일을 열 필요가 없었습니다. 나는 'For Each'루프를 모델링하기 위해 어딘가 샘플 코드를 얻었고 그 조각이 그 안에 있다고 생각한다. 이를 제거하면 문제가 해결되었습니다. 감사! –