VB源文件夹问题

时间:2010-08-25 18:42:55

标签: vb.net

如何使用今天的日期挑选文件?

我的文件夹中有日期和时间,08-25-2010-123803654.xml,08-25-2010-123804441.xml,08-24-2010-123851240.xml等。

我想拔出今天的托管,当我输入我的代码时,它给了我一个错误。

我的代码是:

  

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

结束模块

2 个答案:

答案 0 :(得分:0)

您发布的代码实际上并未将今天的日期附加到基本路径,而是创建一个静态字符串。你不是说:

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

答案 1 :(得分: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