Author Posts

July 19, 2017 at 12:06 pm

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
}

July 19, 2017 at 12:24 pm

Does '-isvalid' do what you think it does?

July 19, 2017 at 12:25 pm

This worked for me

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

July 19, 2017 at 12:26 pm

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.

July 19, 2017 at 12:34 pm

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"}

July 19, 2017 at 12:43 pm

Hi

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

Regards

Jarkko

July 19, 2017 at 12:53 pm

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"}

July 19, 2017 at 12:59 pm

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.

July 19, 2017 at 1:09 pm

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"
}

July 19, 2017 at 1:11 pm

That works. Great. Thank you so much.

July 19, 2017 at 1:13 pm

NP glad to help

July 19, 2017 at 5:01 pm

Hi Simon,

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

Thanks

July 20, 2017 at 7:59 am

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)"
}

July 20, 2017 at 11:17 am

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.

July 20, 2017 at 11:34 am

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
}

July 20, 2017 at 1:02 pm

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?

July 20, 2017 at 1:14 pm

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"
}

July 20, 2017 at 1:16 pm

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"
}

July 20, 2017 at 2:18 pm

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

July 20, 2017 at 2:35 pm

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

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