Test-Path - Multiple Files

This topic contains 19 replies, has 5 voices, and was last updated by  js 3 months ago.

  • Author
    Posts
  • #75226

    Claire
    Participant

    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
    }

  • #75227

    js
    Participant

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

    • #75232

      Claire
      Participant

      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.

  • #75230

    Simon B
    Participant

    This worked for me

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

  • #75241

    Claire
    Participant

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

    • #75242

      Hi

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

      Regards

      Jarkko

  • #75245

    Simon B
    Participant

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

    • #75248

      Claire
      Participant

      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.

  • #75251

    Simon B
    Participant

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

    • #75254

      Claire
      Participant

      That works. Great. Thank you so much.

  • #75257

    Simon B
    Participant

    NP glad to help

  • #75268

    Brad
    Participant

    Hi Simon,

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

    Thanks

  • #75304

    Simon B
    Participant

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

  • #75311

    Claire
    Participant

    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.

  • #75314

    Simon B
    Participant

    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
    }

  • #75323

    Claire
    Participant

    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?

  • #75328

    Simon B
    Participant

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

  • #75329

    Simon B
    Participant

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

  • #75334

    Simon B
    Participant

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

    • #75337

      js
      Participant

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

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

You must be logged in to reply to this topic.