2012-04-06 17 views
11

Ciao Sto cercando di elencare tutti i file in una sottodirectory in cui risiede la cartella di lavoro di Excel. Per qualche motivo, il codice non può essere eseguito oltre la funzione Dir. Qualcuno può consigliare? Grazie!Funzione Dir() non funzionante in Mac Excel 2011 VBA

Sub ListFiles() 

    ActiveSheet.Name = "temp" 

    Dim MyDir As String 
    'Declare the variables 
    Dim strPath As String 
    Dim strFile As String 
    Dim r As Long 

    MyDir = ActiveWorkbook.Path 'current path where workbook is 
    strPath = MyDir & ":Current:" 'files within "Current" folder subdir, I am using Mac Excel 2011 

    'Insert the headers in Columns A, B, and C 
    Cells(1, "A").Value = "FileName" 
    Cells(1, "B").Value = "Size" 
    Cells(1, "C").Value = "Date/Time" 

    'Find the next available row 
    r = Cells(Rows.Count, "A").End(xlUp).Row + 1 

    'Get the first file from the folder 
      'Note: macro stops working here 
    strFile = Dir(strPath & "*.csv", vbNormal) 

    'Loop through each file in the folder 
    Do While Len(strFile) > 0 

     'List the name, size, and date/time of the current file 
     Cells(r, 1).Value = strFile 
     Cells(r, 2).Value = FileLen(strPath & strFile) 
     Cells(r, 3).Value = FileDateTime(strPath & strFile) 

     'Determine the next row 
     r = r + 1 

     'Get the next file from the folder 
     strFile = Dir 

    Loop 

    'Change the width of the columns to achieve the best fit 
    Columns.AutoFit 

End Sub 
+0

Che cosa significa "non può eseguire "significa esattamente? C'è un messaggio di errore? Qual è esattamente il contenuto di 'strPath' quando l'esecuzione termina? –

risposta

18

Gianna, non è possibile utilizzare DIR come quello in VBA-EXCEL 2011. Voglio dire i caratteri jolly non sono supportati. Devi usare MACID per questo scopo.

Vedi questo esempio di codice (collaudato)

Sub Sample() 
    MyDir = ActiveWorkbook.Path 
    strPath = MyDir & ":" 

    strFile = Dir(strPath, MacID("TEXT")) 

    'Loop through each file in the folder 
    Do While Len(strFile) > 0 
     If Right(strFile, 3) = "csv" Then 
      Debug.Print strFile 
     End If 

     strFile = Dir  
    Loop 
End Sub 

Vedere questo link per maggiori informazioni su MACID

Topic: MacID Funzione

link: http://office.microsoft.com/en-us/access-help/macid-function-HA001228879.aspx

EDIT:

Nel caso in cui quel collegamento muoia di cui dubito, ecco un estratto.

MacID Funzione

Usato su Macintosh per convertire una costante a un valore che può essere utilizzato da Dir, Kill, Shell e AppActivate 4 caratteri.

Sintassi

MacID (costante)

L'argomento costante richiesta è composto da 4 caratteri utilizzati per specificare un tipo di risorsa, tipo di file, firma dell'applicazione, o Apple Event, ad esempio testo, OBIN , "XLS5" per i file Excel ("XLS8" per Excel 97), Microsoft Word utilizza "W6BN" ("W8BN" per Word 97) e così via.

Osservazioni

MacID viene utilizzato con Dir e uccidere per specificare un tipo di file Macintosh. Dal momento che il Macintosh non supporta * e? come caratteri jolly, puoi invece utilizzare una costante di quattro caratteri per identificare gruppi di file. Ad esempio, la seguente istruzione restituisce file di tipo TESTO dalla cartella corrente:

Dir ("SomePath", MacID ("TEXT"))

MacID viene utilizzato con Shell e AppActivate specificare un'applicazione utilizzando dell'applicazione firma unica.

HTH

+1

+1 semplicemente superbo –

+1

Un problema da tenere presente è che i percorsi dei file restituiti vengono abbreviati per adattarsi a un limite di 31 caratteri (il limite del nome file dal file system HFS pre-OS X). La funzione di accorciamento mantiene l'estensione del file ma altera i caratteri prima di esso, in modo simile ai nomi dei percorsi brevi per i file su Windows. –

0
If Dir(outputFileName) <> "" Then 
Dim ans 
ans = MsgBox("File already exists.Do you wish to continue(the previous file will be deleted)?", vbYesNo) 
If ans = vbNo Then 
Exit Sub 
Else 
Kill outputFileName 
End If 
End If 

For listitem = 0 To List6.ListCount() - 1 
0

Per la risposta di cui sopra, ha funzionato per me, quando ho tirato fuori il "testo" in MacID:

Sub LoopThruFiles() 

    Dim mydir As String 
    Dim foldercount As Integer 
    Dim Subjectnum As String 
    Dim strpath As String 
    Dim strfile As String 

    ChDir "HD:Main Folder:" 
    mydir = "HD:Main Folder:" 
    SecondaryFolder = "Folder 01:" 
    strpath = mydir & SecondaryFolder 

    strfile = Dir(strpath) 

    'Loop through each file in the folder 
    Do While Len(strfile) > 0 
    If Right(strfile, 3) = "cef" Then 
     MsgBox (strfile) 
     End If 
     strfile = Dir 
    Loop 
End Sub