2010-05-18 13 views
9

मुझे एक फ़ाइल खोलने की आवश्यकता है जिसका पूरा फ़ाइल नाम मुझे नहीं पता।यदि मैं केवल फ़ाइल नाम का हिस्सा जानता हूं तो मैं एक फ़ाइल कैसे खोलूं?

मुझे पता है कि फ़ाइल का नाम कुछ ऐसा है।

filename*esy 

मुझे पता है कि दी गई निर्देशिका में इस फ़ाइल की केवल एक घटना है।

उत्तर

13

filename*esy पहले से ही एक "खोल तैयार" वाइल्डकार्ड & अगर thats alway मामले तो आप बस कर सकते हैं है,

const SOME_PATH as string = "c:\rootdir\" 
... 
Dim file As String 
file = Dir$(SOME_PATH & "filename*esy" & ".*") 

If (Len(file) > 0) Then 
    MsgBox "found " & file 
End If 

बस कॉल (या खाली जब तक पाश) file = Dir$() अगले मैच के लिए।

1
If InStr(sFilename, "filename") > 0 and InStr(sFilename, "esy") > 0 Then 
'do somthing 
end if 

या आप रेगुलर एक्सप्रेशन से

उपयोग कर सकते हैं
Dim RE As Object, REMatches As Object 

    Set RE = CreateObject("vbscript.regexp") 
    With RE 
     .MultiLine = False 
     .Global = False 
     .IgnoreCase = True 
     .Pattern = "filename(.*)esy" 
    End With 

    Set REMatches = RE.Execute(sFilename) 
    REMatches(0) 'find match 
+0

किसी कारण से मैं मान रहा था कि फ़ाइल खुली थी। उफ़ – Glennular

2

वहाँ एक Application.FileSearch आप उपयोग कर सकते हैं (देखें नीचे) है। आप अपने पैटर्न से मेल खाने वाली फ़ाइलों की खोज के लिए इसका उपयोग कर सकते हैं। यह जानकारी here से ली गई है।

Sub App_FileSearch_Example() 

    With Application.FileSearch 
     .NewSearch 
     .LookIn = "c:\some_folder\" 
     .FileName = "filename*esy" 
     If .Execute(SortBy:=msoSortByLastModified, SortOrder:=msoSortOrderDescending) > 0 Then  
      For i1 = 1 To .FoundFiles.Count 
       ' do something with matched file(s) 
      Next i1 

     End If 

    End With  
End Sub 
0

मैं इस प्रश्न को एक समारोह के रूप में आजमाने की कोशिश कर रहा था। यह वह समाधान है जो मेरे लिए काम कर रहा है।

Function fileName(path As String, sName As String, ext As String) As Variant 

'path is Full path from root. Can also use path = ActiveWorkbook.path & "\" 
'sName is the string to search. ? and * are wildcards. ? is for single char 
'example sName = "book?" or sName ="March_*_2014*" 
'ext is file extention ie .pdf .xlsm .xls? .j* 

Dim file As Variant 'Store the next result of Dir 
Dim fname() As String 'Dynamic Array for result set 
ReDim fname(0 To 0) 
Dim i As Integer ' Counter 
i = 0 

' Use dir to search and store first result 
fname(i) = path & Dir(path & "\" & sName & ext) 
i = i + 1 

'Load next result 
file = Dir 

While file <> "" 'While a file is found store that file in the array 
    ReDim Preserve fname(0 To i) As String 
    fname(i) = path & file 
    file = Dir 
Wend 

fileName = Application.Transpose(fname) 'Print out array 

End Function 

यह मेरे लिए एकल या सरणी कार्य के रूप में काम करता है।

0

क्या आप जानते हैं कि कोई अन्य फ़ाइल "फ़ाइल नाम" और इसी क्रम में "ESY" शामिल हैं तो आप बस

Workbooks.Open Filename:= "Filepath\filename*esy.*" 

उपयोग कर सकते हैं या यदि आप तो पात्रों लापता की संख्या पता (4 अक्षर संभालने अज्ञात)

Workbooks.Open Filename:= "Filepath\filename????esy.*" 

मैं फ़ाइलों को जो तारीख हैं टाइमस्टैम्प हिस्सा अनदेखी करने के लिए & टाइमस्टैंप पर कोड को चलाने के लिए इस विधि का उपयोग।

संबंधित मुद्दे