2014-12-04 3 views
-1

Directory.GetFiles 및 "abc"및 "xyz"라는 파일 제외 방법은 무엇입니까?Directory.GetFiles (strFolderPath) 제외 특정 파일 이름 VB.NET

기본적으로 모든 파일이 있고 특정 파일 그룹을 하나의 부서로 보내야하고 "abc"및 "xyz"파일을 다른 곳으로 보내야하는 디렉터리가 있습니까? 순간

나는이 작업을 수행 :

'Standard Documents 
Dim strAttachments As List(Of String) = New List(Of String) 
strAttachments.AddRange(Directory.GetFiles(GlobalVariables.strFolderPath)) 

나는 동일한 기능을하지만, 파일을 제외이 필요하고 다른 주소로 파일을 포함 위와 비슷한 명령을한다.

건배 ,,

제임스

UPDATE

Dim exclude = {"ATS_Declaration"} 
Dim myFiles = From fn In Directory.EnumerateFiles(GlobalVariables.strFolderPath) 
       Where Not exclude.Contains(Path.GetFileNameWithoutExtension(fn), StringComparer.InvariantCultureIgnoreCase) 
Dim strAttachments As List(Of String) = New List(Of String)(myFiles) 
+1

가능한 복제본 [여러 기준에 따라 Directory.EnumerateFiles에서 파일 제외] (http://stackoverflow.com/questions/27285113/exclude-files-from-directory-enumeratefiles-based-on-multiple-criteria) – Plutonix

+0

왜 strAttachments를 반복하고 나중에 두 개의 목록을 만들지 않습니까? – WeSt

답변

2
당신은 LINQ와 System.IO.Path.GetFileNameWithoutExtension 사용할 수 있습니다

: 그건하지 않기 때문에 내가 EnumerateFiles을 사용했다

Dim exclude = { "abc", "xyz" } 
Dim myFiles = From fn in Directory.EnumerateFiles(GlobalVariables.strFolderPath) 
       Where Not exclude.Contains(Path.GetFileNameWithoutExtension(fn), StringComparer.InvariantCultureIgnorecase) 
Dim strAttachments As List(Of String) = New List(Of String)(myFiles) 

주 필요 없다 o 먼저 모든 것을 메모리에로드하십시오.

+0

안녕하세요, 코드를 사용했지만 여전히 내 파일 "ATS_Declaration"을 포함하고 있습니다. 파일명이나 파일 이름 만 제외 시키려면 전체 경로를 정규화해야합니까? (확장자 포함) – Lynchie

+1

@Lynchie :'string()'을 제외하고'ATS_Declaration'을 추가 했습니까? 위의 코드를 실제로 사용 했습니까? 특정 확장을 제외하고 싶다고 생각한 첫 번째 버전에서 'Path.GetExtension'을 사용했습니다. 하지만 지금은'GetFileNameWithoutExtension'을 사용하고 있습니다. –

+0

안녕하세요, 예 - 지금 주 질문에 업데이트 된 코드를 배치하겠습니다. 여전히 strAttachments 목록에 파일이 포함되어 있습니다. – Lynchie

관련 문제