2015-12-30 52 views
0

我必須在文件夾中獲取文件。我的流程可以在批次中運行。如何在批處理過程中獲取文件夾中的文件?

我的代碼是這樣的:

Io     file; 
FileIoPermission perm; 
int handle; 
Filename fileName; 

[handle, filename] = WINAPI::findFirstFile(myFilePatch + "\\*.txt"); 

fileOpen = strFmt (myFilePatch + "\\" + filename); 

if (filename) 
{ 
    perm = new FileIoPermission(filename, 'r'); 
    perm.assert(); 
    file = new TextIo(filename, 'r', 65001); 
} 

//etc... other code 
// I go on to find another file 

filename = WinAPI::findNextFile(handle); 
fileOpen = strFmt (myFilePatch + "\\" + filename); 

if (filename) 
{ 
    // open file.... 
} 

我的問題是WinAPI.findFirstFileWinAPI::findNextFile我有一個錯誤。 如何在批處理文件夾中搜索文件?

Thansk全部,

享受!

+0

你檢查[嘗試使用WINAPI ::上用FindFirstFile服務器上運行(http://stackoverflow.com/questions/8300111/try-to-use-winapifindfirstfile-running-on-Server)?這聽起來非常相似。請始終在您的問題中包含實際的錯誤信息。 –

回答

1

使用System.IO.DirectoryInfo並使用for-loop循環遍歷文件。只需將下面的文件夾路徑替換爲文件夾的位置,它將生成文件夾內所有文件的列表。

static void loopDirectory(Args _args) 
{ 
    System.IO.DirectoryInfo  directory; 
    System.IO.FileInfo[]  files; 
    System.IO.FileInfo   file; 
    InteropPermission   permission; 
    Filename     tmpFilePath; 
    Filename     tmpFileNameShort; 
    Filename     tmpFileExt; 

    str   fileNameTemp; 
    counter  filesCount; 
    counter  loop; 

    permission = new InteropPermission(InteropKind::ClrInterop); 
    permission.assert(); 

    directory = new System.IO.DirectoryInfo(@"C:\Users..."); 
    files  = directory.GetFiles(); 
    filesCount = files.get_Length(); 

    for (loop = 0; loop < filesCount; loop++) 
    { 
     file          = files.GetValue(loop); 
     fileNameTemp        = file.get_FullName(); 
     [tmpFilePath, tmpFileNameShort, tmpFileExt] = fileNameSplit(fileNameTemp); 

     info(tmpFileNameShort); 
    } 
    CodeAccessPermission::revertAssert(); 
} 
+0

很好@TinavanderVyver,以及很好的解決方案來獲取名稱tmpFileNameShort。工作得很好!謝謝。新年快樂! – ulisses

相關問題