none
Powershell | Test-Path (to find missing files) in current running folder/subfolders RRS feed

  • Question

  • Hello,

    I'm trying to create a script that does the following (attached bellow is the code I have so far):
    -every time it runs from a different location (the script for example is manually copied in the root folder of a new location each time)
    -the new location as the root folder of the script that is now running has sub-folders, and in one of them a sequence of files exist
    -the script needs to check the last sequence number of the files and then count through all of them and verify that a number is not skipped from the sequence
    -if it finds a sequence number not existing, report it

    The structure of the folders are:  

        -root_folder
        --subfolder1
        ---capture_folder (including files that need to be verified as all present)
        --subfolder2
        ---capture_folder (including files that need to be verified as all present)

    The filename structure is:   *_f_001r.* (subfolder1) and *_f_001v.* (subfolder2)

    Here is were I am so far:

    Write-Host "Missing files: "
    $path = Get-ChildItem * -attributes D -recurse -include "capture"
    $Last = [INT](Get-ChildItem -Path .\* -rec -filter *_f_*)[-1].BaseName.Substring(14,3)
    $numbers = 1..$Last
    foreach ($number in $numbers) {
    if (Test-Path $path -Filter *$number*r*) { #Write-host "$number file exists"
    }
    else {Write-host "File $number r file does not exist" -ForegroundColor Red}
    if (Test-Path $path -Filter *$number*v*) { #Write-host "$number file exists"
    }
    else {Write-host "File $number v file does not exist" -ForegroundColor Red}
    }
    Write-Host " "
    Write-Host " "
    Read-Host -Prompt "Press Enter to exit"

    Any ideas? If I do something like $path = ".\*\*\*" it kind of works

    Thank you,

    D.


    • Edited by damGM Tuesday, December 18, 2018 9:44 PM refined appearance
    Tuesday, December 18, 2018 9:38 PM

Answers

  • Running the debugger from ISE got the same errors as posted above.

    Running code or a script in the debugger will not magically remove any errors or solve any problem. It's supposed to enable you to find the cause of the error. ;-)

    I added the following command on the top of the script ...

    That will just suppress any non terminating error that appears.

    Does the NULL error come from the script going through empty folders with no files were it would expect to find files?

    That sounds like a very likely cause. If it's really just this - it's easy to solve. Simply check before if the folder is empty. ;-)


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thursday, December 20, 2018 9:51 PM

All replies

  • I've read your explanation twice but I'm still not sure that I've got what you try to do. Could you explain it more precise please?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, December 19, 2018 7:38 AM
  • I've read your explanation twice but I'm still not sure that I've got what you try to do. Could you explain it more precise please?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    You'll have to excuse my language barrier since this is not my native language (also my skills of explaining what I want to do might not be so good :) )

    • Every day I generate a set of files.
    • These files have a predefined name with a specific amount of characters.
    • In a specific part of the filename of each file a sequence number exists with 3 digits (this is the part of the code where I define the $Last as characters(14,3)  This sequence number goes from 001 to a different last number each time example 001-256, 001-443, or 001-0014. Representing the number of files.
    • These files are stored inside a sub-folder of the root (main project of each day) folder.
    • In each root folder (main project of the day) two of these folders exist with almost identical files and sequence numbers, and the only difference is that the 17th character of each filename changes on one folder to -r- and to the other to -v-
    • Concluding: Root folder-> two sub folders -> each one has a set of files with a sequence number that the only difference is one character after the sequence number

    What I want to do:

    • Run the script from the root folder so that it assumes it has to work from there and onward to any sub-folders it finds. Count the sequence number and find the last one (this already works)
    • And then run through the sequence number and report if any number is missing on any of the two folders with a command like Test-Path

    What I have seen that works and what not:

    • The part of the script that navigates to any sub-folder, looks for specific files and finds the sequence number and counts it so that it sets the $number from 001 to $last (which each time is different) WORKS!
    • The part of the script that navigates to any sub-folder and verifies the sequence number DOES NOT WORK!
    • IF I set the path of the running script to run directly from the sub-folder it self it can count the sequence number (for example the $path = ".\*\*" or something similar)
    • How can I tell the test-path command or any similar to look in -recurse on any sub-folder until it finds specific files and check them?

    Does any of the above make any sense? Or am I over complicating simple things?

    again Thank you!

    Wednesday, December 19, 2018 11:18 AM
  • Hmmm  ... I hope I got it now ... English isn't my native language either.  ;-)

    I set up a test environment for me and tried ... at least for me it's working this way:

    Get-ChildItem -Path 'Path\to\the\RootFolder\*\*' -Directory |
        ForEach-Object {
            $Folder = $_.FullName
            $LastFile = Get-ChildItem $Folder -File -Filter '*_f_*.xxx' | Sort-Object -Property BaseName | Select-Object -Last 1
            $LastNumber = [INT]$LastFile.BaseName.SubString(14,3)
            1..$LastNumber |
                ForEach-Object {
                    $FileToCheck = Join-Path -Path $Folder -ChildPath ("{2}_f_{0:000}{1}.xxx" -f $_,$LastFile.BaseName.Substring(17,1),$LastFile.BaseName.Substring(0,11))
                    If( -not ( Test-Path $FileToCheck )){
                        $FileToCheck
                    }
                }
        }

    This will list only missing files.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''




    • Edited by BOfH-666 Wednesday, December 19, 2018 11:03 PM
    Wednesday, December 19, 2018 2:58 PM
  • Thank you for your replies.

    The script actually now works on a local drive flawlessly. BUT When I try to run it to a path that is a network drive that is mapped to my computer it DOES report the missing file but also the continues to run and gives me the following errors:

    You cannot call a method on a null-valued expression.
    At line:9 char:9
    +         $LastNumber = [INT]$LastFile.BaseName.SubString(14,3)
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    You cannot call a method on a null-valued expression.
    At line:12 char:17
    + ...             $FileToCheck = Join-Path -Path $Folder -ChildPath ("{2}_f ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    Here is how I run the code:

    Write-Host "Missing file: "
    #Get-ChildItem -Path 'Path\to\the\RootFolder\*\*' -Directory |
    Get-ChildItem -Path 'Y:\201812\xxxxx_0099\*\*' -Directory |
        ForEach-Object {
            $Folder = $_.FullName
            $LastFile = Get-ChildItem $Folder -File -Filter '*_f_*' | Sort-Object -Property BaseName | Select-Object -Last 1
            $LastNumber = [INT]$LastFile.BaseName.SubString(14,3)
            1..$LastNumber |
                ForEach-Object {
                    $FileToCheck = Join-Path -Path $Folder -ChildPath ("{2}_f_{0:000}{1}.*" -f $_,$LastFile.BaseName.Substring(17,1),$LastFile.BaseName.Substring(0,11))
                    If( -not ( Test-Path $FileToCheck )){ Write-host "$FileToCheck" -ForegroundColor red
                    }
                }
        }
    Write-Host " "
    Write-Host " "
    Read-Host -Prompt "Press Enter to exit"
    Any ideas?



    • Edited by damGM Thursday, December 20, 2018 8:19 AM correction of error messages
    Thursday, December 20, 2018 8:03 AM
  • The script actually now works on a local drive flawlessly. BUT When I try to run it to a path that is a network drive that is mapped to my computer it DOES report the missing file but also the continues to run and gives me the following errors:....

    So you will have to debug the script. Run the code from the ISE or in VSCode line by line and inspect the output of the pipeline or the content of the variables you use. Did you try to run it on a UNC path instead of a mapped drive?

    Any ideas?

    If the script runs without errors on a local drive you could simply run it on the server (or client) where you need it. You could do that even remotely or unattended with a scheduled task.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thursday, December 20, 2018 9:55 AM
  • I tried the map drive and the UNC path, but same error.

    Running the debugger from ISE got the same errors as posted above. I added the following command on the top of the script and it is still working, but not reporting the errors:

    $ErrorActionPreference = "SilentlyContinue"

    I hope nothing will explode silently..

     Does the NULL error come from the script going through empty folders with no files were it would expect to find files?

    Thursday, December 20, 2018 5:30 PM
  • Running the debugger from ISE got the same errors as posted above.

    Running code or a script in the debugger will not magically remove any errors or solve any problem. It's supposed to enable you to find the cause of the error. ;-)

    I added the following command on the top of the script ...

    That will just suppress any non terminating error that appears.

    Does the NULL error come from the script going through empty folders with no files were it would expect to find files?

    That sounds like a very likely cause. If it's really just this - it's easy to solve. Simply check before if the folder is empty. ;-)


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thursday, December 20, 2018 9:51 PM

  • Does the NULL error come from the script going through empty folders with no files were it would expect to find files?

    That sounds like a very likely cause. If it's really just this - it's easy to solve. Simply check before if the folder is empty. ;-)


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Again THANK YOU!
    Saturday, December 22, 2018 8:00 PM