Test-Path - Multiple Files

Hello,

I am working on a script where i need to test that two files exist in a location.

I know you can use Test-path to specify multiple files to check.

Can someone give me a hand with this script?

if( !( ( Test-path -path $Filea -IsValid ) -and ( Test-path -path $Fileb -IsValid ) ) ){
“one of the files not found!”; break
}

Does ‘-isvalid’ do what you think it does?

This worked for me

If ((test-path -path “C:\path\file1.txt”) -and (test-path -Path “C:\path\file2.txt”)) {Write-host “found both”}

It does if i don’t have multiple files

so if i was to specify

Test-path $Filea -IsValid

This comes back either True or False.

But i have two files in one location i need to make sure is there before doing anything.

Well that works but doesn’t with my script.

My script so far is the following:
$logs = Get-ChildItem -Path C:\Test -Recurse -file | where { $.CreationTime.Date -gt (Get-Date).AddDays(-3)}
$Filea = $Logs | where-object {$
.Name -eq “SuccessfulDataTransfers.txt”}
$Fileb = $Logs | where-object {$_.Name -eq “VerifiedDataTransfers.txt”}
If ((test-path -path “$Filea”) -and (test-path -Path “$Fileb”)) {“Write-host found both”}

Hi

On If sentence use $Filea.FullName and $Fileb.FullName if you are not in that path.

Regards

Jarkko

I managed to get this to work

$path = "c:\path"
$logs = Get-ChildItem -Path $path -Recurse -file | where { $.CreationTime.Date -gt (Get-Date).AddDays(-3)}
$Filea = $Logs | where-object {$
.Name -eq “SuccessfulDataTransfers.txt”}
$Fileb = $Logs | where-object {$_.Name -eq “VerifiedDataTransfers.txt”}
$filea = $path + $filea.name
$fileb = $path + $fileb.name
If ((test-path -path “$Filea”) -and (test-path -Path “$Fileb”)) {“Write-host found both”}

Thanks, One downside in my script if fileb file doesn’t exit then this fails before it does the test-path.

$Filea = $Logs | where-object {$.Name -eq “SuccessfulDataTransfers.txt”}
$Fileb = $Logs | where-object {$
.Name -eq “VerifiedDataTransfers.txt”}

Is what i have so far.

ok try this

$path = "c:\path"
$logs = Get-ChildItem -Path $path -Recurse -file | where { $.CreationTime.Date -gt (Get-Date).AddDays(-3)}
$Filea = $Logs | where-object {$
.Name -eq “SuccessfulDataTransfers.txt”}
$Fileb = $Logs | where-object {$_.Name -eq “VerifiedDataTransfers.txt”}
Try{
If ((test-path -path $Filea.fullname) -and (test-path -Path $Fileb.fullname)) {Write-host “found both”}
}
Catch{
Write-host “not found”
}

That works. Great. Thank you so much.

NP glad to help

Hi Simon,

what if file was moved or renamed, how to find what file not exist in folder

Thanks

Brad, you could probably use FileSystemWatcher See https://dereknewton.com/2011/05/monitoring-file-system-changes-with-powershell/ for more help with this as I have not used it much. Below is the code from this site. In the action sections you can write whatever code you want when an event is triggered i.e. send an email etc

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = “C:\Path”
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$changed = Register-ObjectEvent $watcher “Changed” -Action {
write-host “Changed: $($eventArgs.FullPath)”
}
$created = Register-ObjectEvent $watcher “Created” -Action {
write-host “Created: $($eventArgs.FullPath)”
}
$deleted = Register-ObjectEvent $watcher “Deleted” -Action {
write-host “Deleted: $($eventArgs.FullPath)”
}
$renamed = Register-ObjectEvent $watcher “Renamed” -Action {
write-host “Renamed: $($eventArgs.FullPath)”
}

ok next problem i am facing.

Each night i get a folder created in the location. For example last night the folder was called 1972017
The night before was called 1872017

This script will run early in the morning. My script so far selects
$logs = Get-ChildItem -Path $latest -Recurse -file | where { $_.CreationTime.Date -gt (Get-Date).AddDays(-1)}
Which shows me the files created but what i want to do is select the folder
$latest = Get-ChildItem -Path $Path | Sort-Object LastAccessTime -Descending | Select-Object -First 1 and then drill down to get the two files selected.

This is my script so far.

$path = “C:\TEST”
$latest = Get-ChildItem -Path $Path | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$logs = Get-ChildItem -Path $latest -Recurse -file | where { $.CreationTime.Date -gt (Get-Date).AddDays(-1)}
$Filea = $Logs | where-object {$
.Name -eq “SuccessfulDataTransfers.txt”}
$Fileb = $Logs | where-object {$_.Name -eq “VerifiedDataTransfers.txt”}
Try{
If ((test-path -path $Filea.fullname) -and (test-path -Path $Fileb.fullname)) {Write-host “found both”}
}
Catch{
Write-host “not found”
}

Thank you all.

If you had something like this running eveytime a new folder is it would overwrite the contents of newfolder.txt you could always append if more than one folder is going to be created
so in your original script you could run

$path = get-content c:\test\newfolder.txt

The below script has to be running when the folder is created

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = “C:\Path”
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$created = Register-ObjectEvent $watcher “Created” -Action {
write-host “Created: $($eventArgs.FullPath)”
$folder = “$($eventArgs.fullPath)”
$folder | out-file c:\test\newfolder.txt
Write-host $folder
}

Thanks,
The Files contained within 1972017 will be two files
VerifiedDataTransfers.txt and SuccessfulDataTransfers.txt

so once i have selected the folder, i want to confirm these two files are here.

Then if they are both here then i will be doing something with these using sql.
Currently its showing as write-host “Found both” or “not found” but these will be doing more.

so i need to confirm the most recent folder which will be created at 23:30 every night has the two files.
I don’t want think i need to write this to a text file do i?
i need to get the contents of a folder?

Try that

$path = Get-ChildItem c:\test | ?{ $.PSIsContainer } | Sort CreationTime -Descending | Select -First 1
$logs = Get-ChildItem -Path $path -Recurse -file | where { $
.CreationTime.Date -gt (Get-Date).AddDays(-3)}
$Filea = $Logs | where-object {$.Name -eq “SuccessfulDataTransfers.txt”}
$Fileb = $Logs | where-object {$
.Name -eq “VerifiedDataTransfers.txt”}
Try{
If ((test-path -path $Filea.fullname) -and (test-path -Path $Fileb.fullname)) {Write-host “found both”}
}
Catch{
Write-host “not found”
}

Sorry should be

$path = Get-ChildItem c:\test | ?{ $.PSIsContainer } | Sort CreationTime -Descending | Select -First 1
$logs = Get-ChildItem -Path $pathfullname -Recurse -file | where { $
.CreationTime.Date -gt (Get-Date).AddDays(-3)}
$Filea = $Logs | where-object {$.Name -eq “SuccessfulDataTransfers.txt”}
$Fileb = $Logs | where-object {$
.Name -eq “VerifiedDataTransfers.txt”}
Try{
If ((test-path -path $Filea.fullname) -and (test-path -Path $Fileb.fullname)) {Write-host “found both”}
}
Catch{
Write-host “not found”
}

Missing a . in $pathfullname line should read
$logs = Get-ChildItem -Path $path.fullname -Recurse -file | where { $_.CreationTime.Date -gt (Get-Date).AddDays(-3)}

Another way to do that where part (must have the period after 3):

where { (get-date) - $_.CreationTime -lt 3. }