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?
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
March 3, 2020 at 3:12 pm
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 🙂
March 3, 2020 at 3:14 pm
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
March 3, 2020 at 3:18 pm
Thanks anyway, you guys. @=)
March 3, 2020 at 9:01 pm
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
March 3, 2020 at 11:24 pm
I'm curious... are you trying to find this file so you can import it into SQL Server?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 4, 2020 at 9:40 am
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.
March 4, 2020 at 9:45 am
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'.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply