tags:

views:

35

answers:

2

How can I pick out files using todays date?

I have files that have dates and times in a folder, 08-25-2010-123803654.xml, 08-25-2010-123804441.xml, 08-24-2010-123851240.xml, etc.

I want to pull out just todays entrys and when i put in my code it gives me an error.

My code is:

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

Im not sure what Im doing wrong here.

Thanks

MY Code: Module Module1

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

End Module

A: 

The code you posted doesn't actually append todays date to the base path, it creates a static string. Don't you mean:

SourceFolder = "C:\TEST\" & DateTime.Now.ToString("MM-dd-yyyy") & "*"
David Rutten
Hey David, I tried that but I keep getting an error under the following: For Each file In fs.GetFolder(SourceFolder).filesError is: Exception from HRESULT: 0x800A004C (CTL_E_PATHNOTFOUND)
gabrielVa
A: 

I'm going to translate just this portion of your code to hopefully put you on the right track:

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
Dan Tao
So then sourcefolder should read, >SourceFolder = directory>TargetFolder = "C:\TEST\Excel"How would searchpattern come into play?
gabrielVa
@gabrielVa: See if my update helps you out a bit more.
Dan Tao
Yes, that seems to help quite a bit and I understand it now, although I get a new error, I created a private function for directory() and when I run the console app now it says the following: System.NotImplementedException was unhandled Message=The method or operation is not implemented. Source=ConsoleApplication3
gabrielVa
@gabrielVa: Well, then... can you change the name of your function so it doesn't overlap with a member of `System.IO`? Alternately you could simply resolve the ambiguity by typing out `System.IO.Directory.GetFiles`.
Dan Tao
Thanks Dan, looks like Ive go one final hurdle, i get Illegal characters in path under todaysfiles()Dim targetfolder As String = "C:\TEST\Excel" Dim sourcefolder As String = "C:\TEST" Dim Searchpattern As String = String.Format("{0:MM-dd-yyyy}-*.xml", Date.Today) Dim todaysfiles() As String = System.IO.Directory.GetFiles(Searchpattern)TXT Visualizer shows the following though: 08-26-2010-*.xml
gabrielVa
@gabirelVa: Wow, I apologize -- I posted flat-out wrong code. Sorry! Use `Directory.GetFiles(sourceFolder, searchPattern)` instead (updated answer).
Dan Tao
Yes!! That worked Dan! The file now pulls form the data source folder as well as the search pattern. Thank you so VERY much Dan!!
gabrielVa