I have a list of files that I want to check a directory in powershell to see if they exist. I'm not too familiar with powershell but this is what I have so far that doesn't work.
$filePath = "C:\Desktop\test\"
$currentDate = Get-Date -format yyyyMMdd
$listOfFiles =
"{0}{1}testFile.txt",
"{0}{1}Base.txt",
"{0}{1}ErrorFile.txt",
"{0}{1}UploadError.txt"`
-f $filePath, $currentDate
foreach ( $item in $listOfFiles )
{
[System.IO.File]::Exists($item)
}
Is this possible?
You could use the Test-Path cmdlet.
$filePath = "C:\Desktop\test\"
$currentDate = Get-Date -format yyyyMMdd
#I'm using 1..4 to create an array to loop over rather than manually creating each entry
#Also used String Interpolation rather than -f to inject the values
1..4 | ForEach-Object {Test-Path "${filePath}${currentDate}file$_.txt"}
Edit: For the updated file names, here is how you could put them in an array to be looped through.
"testFile","Base","ErrorFile","UploadError" | ForEach-Object {
Test-Path "${filePath}${currentDate}$_.txt"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With