check multiple files in folder and subfolders

Clinton van Axel 126 Reputation points
2021-02-23T22:09:01.95+00:00

Hi all,

Is it possible to check for multiple files in folders and subfolders.
After the check do a copy.

I already made the copy part.

Get-Childitem "C:\Temp\*" -Include "test3.txt","test8.txt","test1.txt","test9.txt" -Recurse | foreach-object{Copy-Item -Path $_ -Destination "Q:\" -PassThru -recurse -Force}

I need help with the if part and how to use the test-path.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,433 questions
{count} votes

7 answers

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 32,321 Reputation points Microsoft Vendor
    2021-02-24T06:51:35.93+00:00

    Hi,

    If you want to know whether the file exists before copying it you can do it like this

    $files = "test3.txt","test8.txt","test1.txt","test9.txt"  
    $source = "C:\Temp\"  
    $destination = "Q:\"  
    foreach($file in $files){  
        $obj = Get-ChildItem -Path $source -Include $file -Recurse  
        if(-not $obj)  
        {  
            write-host "$file not found"  
        }  
        else{  
            Copy-Item -Path $obj.FullName -Destination $destination -Recurse -Force  
        }  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Siu Yip 1 Reputation point
    2022-07-14T15:15:27.907+00:00

    How can I make this more simple?

    Full path of the folder

    $folder1 = 'c:\users\backupnt04\downloads'

    If the folder exists, show message it exist.

    if (Test-Path -Path $folder1 -PathType Container) {
    try {Write-Host "Checked: The folder [$folder1] exist."
    } catch {throw $_.Exception.Message}
    }

    If the file does not exist, show a message it doesn't exist.

    else {Write-Host "Checked: The folder [$folder1] does not exist."
    }

    $folder2 = 'c:\users\backupnt04\desktop'

    if (Test-Path -Path $folder2 -PathType Container) {
    try {Write-Host "Checked: The folder [$folder2] exist."
    } catch {throw $_.Exception.Message}
    }

    else {Write-Host "Checked: The folder [$folder2] does not exist."
    }

    $folder3 = 'c:\users\shicorp\downloads'

    if (Test-Path -Path $folder3 -PathType Container) {
    try {Write-Host "Checked: The folder [$folder3] exist."
    } catch {throw $_.Exception.Message}
    }

    else {Write-Host "Checked: The folder [$folder3] does not exist."
    }

    $folder4 = 'c:\users\shicorp\desktop'

    if (Test-Path -Path $folder4 -PathType Container) {
    try {Write-Host "Checked: The folder [$folder3] exist."
    } catch {throw $_.Exception.Message}
    }

    else {Write-Host "Checked: The folder [$folder4] does not exist."
    }

    $folder5 = 'c:\users\cfahey\downloads'

    if (Test-Path -Path $folder5 -PathType Container) {
    try {Write-Host "Checked: The folder [$folder3] exist."
    } catch {throw $_.Exception.Message}
    }

    else {Write-Host "Checked: The folder [$folder5] does not exist."
    }

    $folder6 = 'c:\users\cfahey\desktop'

    if (Test-Path -Path $folder6 -PathType Container) {
    try {Write-Host "Checked: The folder [$folder3] exist."
    } catch {throw $_.Exception.Message}
    }

    else {Write-Host "Checked: The folder [$folder6] does not exist."
    }

    $folder7 = 'c:\users\jburke\downloads'

    if (Test-Path -Path $folder7 -PathType Container) {
    try {Write-Host "Checked: The folder [$folder3] exist."
    } catch {throw $_.Exception.Message}
    }

    else {Write-Host "Checked: The folder [$folder7] does not exist."
    }

    0 comments No comments

  3. Andreas Baumgarten 100.3K Reputation points MVP
    2022-07-14T16:39:09.757+00:00

    Hi @Siu Yip ,

    please take a look on the script if this fits your requirement:

    $folders = "./Junk/1","./Junk/2","./Junk/3","./Junk/4","./Junk/5","./Junk/6"  
    $folders | ForEach-Object {  
        Write-Host "Checking folder $_"  
        if (Test-Path -Path $_ -PathType Container) {  
            Write-Host "Folder $_ exists"}  
        else {Write-Host "Folder $_ does not exist"}  
    }  
    

    Result looks like this (on my Mac):

    220794-image.png

    Instead of relative paths (like .\Junk\1) you can use absolut paths as well (like c:\users\cfahey\desktop)

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  4. Siu Yip 1 Reputation point
    2022-07-14T17:12:42.897+00:00

    Thanks so very much AndreasBaumgarten.

    0 comments No comments

  5. Siu Yip 1 Reputation point
    2022-07-14T17:23:52.953+00:00

    One last question if possible. What's the powershell command to check for a filename test.txt on C:\users including all subdirectories?

    0 comments No comments