Reputation: 89
Everyday a file is being saved with the current date in format testDDMMYYY.csv
for an example test24112017.csv
.
I need to open the current day's file with Powershell, however, I haven't found a way for it to work with variables.
#Date (24112017)
[String]$a=(Get-Date).ToShortDateString() | Foreach-Object {$_ -replace "\.", ""}
#Open the file called test24112017
[int] $S1=Get-Content "D:\test$($a).csv" -Tail 1 | Foreach-Object {$_ -replace "`"", ""} | ForEach-Object{$_.Split(",")[1]} | write-host
How can I get that variable working inside that path?
Upvotes: 2
Views: 5963
Reputation: 626
Hmm aren't you over-complicating things a bit?
First of all,
(Get-Date).ToShortDateString()
doesn't give me 24112017
but 24-11-2017
.
So i'm guessing we have different regional settings, what part of the world are you from?
Have you printed out $a
? What does it give you?
I would go for a different approach. Since your filename is literally always newer than the previous one (because it holds a date). I would just sort on the Name and select the first one (aka the newest file).
$newestfile = Get-ChildItem c:\temp | Sort-Object Name -Desc | select * -First 1
Now go do whatever you want with your latest file.
get-content $newestfile.fullname
Upvotes: 0
Reputation: 1742
Do not use (Get-Date).ToShortDateString() | Foreach-Object {$_ -replace "\.", ""}
, just use Get-Date -Format 'ddMMyyyy'
to format the date the way you want :
Get-Content "D:\test$(Get-Date -Format 'ddMMyyyy').csv" -Tail 1
Upvotes: 4