Finding specific file with dynamic date

  • I need to find a file with a specific date in it. The problem is I never know what the date will be except "today". The date is formatted as yyyyMMddhhmm. Since it was created earlier in the day by an automated process, I need to be able to find it by ignoring the hhmm part of the date. And I can't remove the hhmm because we need to be able to run this process multiple times during the day without it erroring out. To be clear, we rarely re-run the process, but we need to have the ability for it. Hence we need to keep hhmm so the process can write new files without problems.

    Here's the code I have working:

    Get-ChildItem -Path "FileSystem::\\my\nas\Share\Path\" -Name  "MyFiles_*.zip"

    When I try to add variables for the file name, I get nothing back.

    $FileDateCheck = Get-Date -Format "yyyyMMDD"

    $FileName = "MyFiles_" + $FileDateCheck + "*.zip"

    $ZipDestinationPath = "\\my\nas\Share\Path\"

    Get-ChildItem -Path "FileSystem::\\my\nas\Share\Path\" -Name $FileName

    It won't even let me use a variable for the NAS path.

    There are many files with similar names, just the date being different, in this directory. I just need to target the most recent one (for the moment) but I don't want to use last created property because I want to be able to adjust for other processes in which I target the file name by date and it might be older.

    Any thoughts on what I'm missing here?

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Well, I feel kind of silly. I have spent 2 weeks trying to figure this out before I finally posted here. Then after posting, went back to searching and finally found the final bits of my solution. DUH. Also a typo in the date I was trying to check because apparently case matters in PowerShell (which I knew but wasn't paying attention to when I typed my code).

    $FileDateCheck = Get-Date -Format "yyyyMMdd"

    $FileName = "MyFiles_" + $FileDateCheck + "*.zip"

    $ZipDestinationPath = "FileSystem::\\my\nas\Share\Path\"

    Get-ChildItem -File -Path $ZipDestinationPath -Filter $FileName -Name

     

     

     

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • I think the format is case sensitive.  I tried a similar example to yours against my file server and found the format needs to be:

    $FileDateCheck = Get-Date -format "yyyyMMdd"

    --edit: okay looks like you found the problem while I was typing the answer 🙂

     

  • Everyone needs a rubber ducky! Glad we could be of service!

    -------------------------------------------------------------------------------------------------------------------------------------
    Please follow Best Practices For Posting On Forums to receive quicker and higher quality responses

  • Thanks anyway, you guys. @=)

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • If you are actually looking for the latest file that matches - it can be done this way:

    $searchDate = (Get-Date).AddDays(-1);
    $ZipDestinationPath = "\\my\nas\Share\Path\";
    $searchString = "MyFiles_" + $searchDate.ToString("yyyyMMdd") + "*.zip";
    Get-ChildItem -File -Name -Path $ZipDestinationPath -Filter $searchString | Sort LastWriteTime | Select -Last 1;

    If you have more than 1 file that matches the date string - I assume you want only the latest file that is available.  You also don't need to specify 'FileSystem' for the path.

    I specified (Get-Date).AddDays(-1) to show how to retrieve the latest file that matches for yesterday's date.  For current date you can use Get-Date by itself or (Get-Date).AddDays(0).

    Now all you need to do is expand the script to use parameters for the search date, zip destination and file prefix and you can search for the latest file in any destination with any leading characters for the specified date.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • I'm curious... are you trying to find this file so you can import it into SQL Server?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Some files we're importing into SQL. Some files are data ported out of SQL that we need to add SFTP log files to. And some files we're doing other things with that I can't remember off the top of my head. It's a mixed bag of stuff. Essentially, we have to get rid of our current compression protocols per corporate mandate and I'm looking for a free way to do what we need to do. PowerShell seems to cover most of my needs if I can wrap my head around the cmdlets and learn the language.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Jeffrey Williams wrote:

    If you have more than 1 file that matches the date string - I assume you want only the latest file that is available.  You also don't need to specify 'FileSystem' for the path.

    As I said in my initial post, I need more than the latest file. This has to be interchangeable with other needs later down the line, which means I will be looking for older files, not necessarily the oldest file. And there are files with multiple different dates in the folder I'm querying. Possibly multiples with the same date. Not usually.

    Also, yes, apparently I do need to specify 'FileSystem' because I can't get the path to work properly. It kept erroring out with "path does not exist" every time I didn't use 'FileSystem'.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply