FileSystemObject FileExists는 한 컴퓨터에서 작동하지만 다른 컴퓨터에서 작동합니다

StackOverflow https://stackoverflow.com//questions/25026553

문제

Excel 2010 VBA에서는 파일이 있는지 확인하기 위해 FileSystemObject의 FileExists 속성을 사용하고 있습니다. 내 컴퓨터에서 그것은 잘 작동합니다. 그러나 또 다른 Excel 2010 컴퓨터에서는 파일이있는 Windows 탐색기에서 파일이있는 경우 파일이 없을 때 파일이 있지 않다고보고합니다. 두 경우 모두 검사중인 파일은 로컬 하드 드라이브에 있습니다.

셸을 사용하여 파일을 압축 해제하면 파일을 확인하고 있습니다. 압축을 푼 후 DoEvents를 사용합니다. 파일이 ZIP 파일이있는 것과 동일한 폴더로 추출됩니다.

여기 코드가 있습니다.

Dim oShell As Object, oZippedFile As Object, oUnzipTargetFolder As Object
Dim FSO As Object, oFile As Object, oFolder As Object
Dim strZipfileFullpath As Variant, strTargetFolderpath As Variant, strTargetFullpath As Variant 'NOT AS STRINGS!! (Shell error if strings)
Dim bFileCheckFsoFileExist As Boolean

Set oShell = CreateObject("shell.application")
Set FSO = CreateObject("scripting.filesystemobject")

strZipfileFullpath = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)

Set oFile = FSO.GetFile(strZipfileFullpath)
Set oFolder = oFile.ParentFolder
strTargetFolderpath = oFolder.Path & Application.PathSeparator
strTargetFullpath = strTargetFolderpath & oShell.Namespace(strZipfileFullpath).Items.Item(0).Name

oShell.Namespace(strTargetFolderpath).CopyHere oShell.Namespace(strZipfileFullpath).Items.Item(0) 'this zip has only one file.
DoEvents

bFileCheckFsoFileExist = FSO.FileExists(strTargetFullpath)
.

하나의 컴퓨터 에서이 작업이 잘 작동하지만 다른 컴퓨터에서 다른 컴퓨터에서 파일이 명확하게 알 수 있음을보고 있습니다.

업데이트 :

나는 다른 사람들이 동일한 걸림돌로 들어가는 것에 도움이 될 수 있으면이 메모를 추가 할 것이라고 생각했습니다.

기본 기본 문제는 셸을 사용하여 압축 파일을 추출 할 때 Zipped File 객체의 이름 속성에 Explorer가 확장을 숨기고 파일에 등록 된 확장자가있는 경우 확장자가 포함되어 있지 않습니다. 예 :

Dim strGofnZipfileFullpath As Variant
Dim oShell As Object, oShellZipfile As Object, oShellZippedFileInZipfile As Object
Dim strShellZippedFileInZipfileFilename As Variant 'NOT AS STRINGS!! (Shell error if strings)

strGofnZipfileFullpath = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)

Set oShell = CreateObject("shell.application")
Set oShellZipfile = oShell.Namespace(strGofnZipfileFullpath)
Set oShellZippedFileInZipfile = oShellZipfile.Items.Item(0) 'assumes only 1 file in zip. 
        strShellZippedFileInZipfileFilename = oShellZippedFileInZipfile.Name
.

Windows 탐색기가 확장을 숨기도록 설정되고 압축 파일이 등록 된 확장자가있는 경우 StrShellzippeDFileInzipFileFileFileName에는 확장자가 포함되어 있지 않습니다.

그러나 압축 파일 객체 (OshellzippedFileInzipFile)에는 확장자가 포함 된 경로 속성이 있습니다. 따라서 다음과 같은 확장명을 포함하여 파일 이름을 가져올 수 있습니다.

strShellZippedFileInZipfileFilename = Right(oShellZippedFileInZipfile.Path, InStr(StrReverse(oShellZippedFileInZipfile.Path), Application.PathSeparator) - 1)
.

도움이 되었습니까?

해결책

염두에 두어야 할 것은 Windows 탐색기의 폴더 옵션입니다.이 경우이 경우에 도움이되지는 않지만 Windows 탐색기에 파일이 표시되는 방법에 대해 거대한 문제가있는 많은 FSO 상호 작용이있었습니다.시스템이 파일 확장명을 표시하는 것처럼 간단한 것으로, 다른 사람이 파일을 찾는 데 때때로 범인이 될 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top