Dee
Dee

Reputation: 1

powershell, find a date string

Apologies for the bad scripting but im a begginer in scripting. Got to start somewhere.

I wish to create a script that looks for a file that has yesterdays date in so is always Day-1 eg today is the 20180824 but I wish to check for 20180823

$file = "test{0}.txt" -f (Get-Date).AddDays(-1)ToString("yyyyMMdd") 
Test-Path C:\temp\$file -PathType Leaf'

Any ideas what is wrong with this script please

Upvotes: 0

Views: 127

Answers (2)

Bacon Bits
Bacon Bits

Reputation: 32155

As others have said, you've got a missing period:

$file = "test{0}.txt" -f (Get-Date).AddDays(-1).ToString("yyyyMMdd") 

You can also specify the format in the placeholder itself:

$file = "test{0:yyyyMMdd}.txt" -f (Get-Date).AddDays(-1)

Upvotes: 0

techGaurdian
techGaurdian

Reputation: 750

Changes Made added .ToString() and removed trailing '

$file = "test{0}.txt" -f (Get-Date).AddDays(-1).ToString("yyyyMMdd")

Test-Path C:\temp\$file -PathType Leaf

PowerShell Syntax Correction

Hope This Helps !!

Upvotes: 1

Related Questions