2010-08-25 80 views
0

如何使用今天的日期挑出文件?VB源文件夾問題

我有文件夾中有日期和時間的文件,08-25-2010-123803654.xml,08-25-2010-123804441.xml,08-24-2010-123851240.xml等。

我想拉出今天entrys,當我把我的代碼,它給了我一個錯誤。

我的代碼是:

SourceFolder = 「C:\ TEST(DateTime.Now.tostring( 'MM-DD-YYYY-*')」

林不知道林做錯了

感謝

我的代碼: 模塊模塊1

Private Property fs As Object 
Private Property BaseName As Object 
Private Property FullTargetPath As Object 

Sub Main() 


    Dim xlApp, xlWkb, SourceFolder, TargetFolder, file 
    xlApp = CreateObject("excel.application") 
    fs = CreateObject("Scripting.FileSystemObject") 
    Const xlNormal = 1 
    SourceFolder = "C:\TEST\" & DateTime.Now.ToString("MM-dd-yyyy") & "*" 
    TargetFolder = "C:\TEST\Excel" 

    'Hide Excel 
    xlApp.Visible = False 

    'Process each file in SourceFolder 
    For Each file In fs.GetFolder(SourceFolder).files 
     'Open file in SourceFolder 
     xlWkb = xlApp.Workbooks.Open(file) 
     'Get Filename 
     BaseName = fs.getbasename(file) 
     'Concatenate full path. Extension will be automatically added by Excel 
     FullTargetPath = TargetFolder & "\" & BaseName 
     'Save as XLS file into TargetFolder 
     xlWkb.SaveAs(FullTargetPath, xlNormal) 
     'Close the file after its done 
     xlWkb.close() 
    Next 

    xlWkb = Nothing 
    xlApp = Nothing 
    fs = Nothing 

    MsgBox("Finished. Bout time!") 


End Sub 

前端模塊

回答

0

我要你的代碼只是這部分轉化爲有希望把你在正確的軌道上:

Dim targetFolder As String = "C:\TEST\Excel" 

Dim sourceFolder As String = "C:\TEST" 

' Here is where we specify a pattern by which we will find files in ' 
' sourceFolder. ' 
Dim searchPattern As String = String.Format("{0:MM-dd-yyyy}*.xml", Date.Today) 

' This will give us an array with only those files matching the pattern above; ' 
' that is, only files from today. ' 
Dim todaysFiles() As String = Directory.GetFiles(sourceFolder, searchPattern) 

For Each file As String In todaysFiles 
    ' Excel stuff... ' 

    Dim fileName As String = Path.GetFileNameWithoutExtension(file) 

    ' Concatenate full path. Extension will be automatically added by Excel. ' 
    Dim fullTargetPath = Path.Combine(targetFolder, fileName) 

    ' More Excel stuff... ' 
Next 
+0

所以後來sourcefolder應該閱讀, > SourceFolder = directory > TargetFolder =「C:\ TEST \ Excel」 searchpattern如何發揮作用? – GabrielVa 2010-08-25 19:35:13

+0

@gabrielVa:看看我的更新是否能幫助你多一點。 – 2010-08-25 19:57:23

+0

是的,這似乎有幫助很多,我現在明白了,雖然我得到一個新的錯誤,我創建了一個私人函數的目錄(),當我現在運行控制檯應用程序它說以下內容:System.NotImplementedException未處理 消息=該方法或操作未實現。 Source = ConsoleApplication3 – GabrielVa 2010-08-26 11:26:05

0

您發佈不今天的日期實際上追加到基本路徑的代碼,它創建了一個靜態字符串。難道你的意思是:

SourceFolder = "C:\TEST\" & DateTime.Now.ToString("MM-dd-yyyy") & "*" 
+0

嘿大衛, 我想,但我保持在以下收到錯誤: 每個文件在fs.GetFolder(sourceFolder).files 錯誤是:從HRESULT異常:0x800A004C(CTL_E_PATHNOTFOUND) – GabrielVa 2010-08-25 19:29:17