2009-09-20 5 views
0

Dir 함수를 사용하는 매크로가 있습니다.Dir()에서 파일 이름 추출

MyFile = Dir(CurDir() & Sep & "*.xls") 
Do While MyFile <> "" 
    ... 
    MyFile = Dir() 
Loop 

어떻게하면 MyFile (확장명 제외)에서 파일 이름 만 추출 할 수 있습니까?

답변

1

편리한 내장 함수가 같은 일부 문자열 조작해야 할 것이다, 그래서 그것은, 보이지 않는 다음 을 MyFile 변수가 확장자를 가진 파일 이름을 보유하고

' Get just the file name and extension 
lastPathIndex = InStrRev(MyFile, Application.PathSeparator) 
If lastPathIndex >= 1 Then 
    MyFile = Right(MyFile, Len(MyFile) - lastPathIndex) 
End If 

' Now get the file name without the extension 
lastDotIndex = InStrRev(MyFile, ".") 
If lastDotIndex >= 1 Then 
    MyFile = Left(MyFile, lastDotIndex - 1) 
End If 

' MyFile now contains just the filename 
0

및 경로없이.

는 귀하의 의견을 바탕으로, 당신은 .XLS 될 것입니다 발견 된 파일의 확장자를 알고, 그래서 당신은 한 줄에 코드를 업데이트 사용할 수 있습니다

MyFile = Dir(CurDir() & Sep & "*.xls") 
Do While MyFile <> "" 
    MyFile = Left(MyFile, Len(MyFile) - 4) 
    ... 
    MyFile = Dir() 
Loop 
0

또 다른 방법은

fileName = Split(MyFile, Sep)(UBound(Split(MyFile, Sep))) 
입니다