none
How to check if multiple files exist in a UNC path? RRS feed

  • Question

  • Below is my script which collects the 1) File Name 2) Server Name 3) LastWriteTime property of the file.

    My issue is that it throws error if the file specified in $files does not exist.

    $results = @()
    $servers = Get-Content "C:\ServerNames.txt"
    $uncs = Get-Content "C:\UNCPath.txt"
    $files = Get-Content "C:\FileNames.txt"
    
    foreach ($server in $servers)
    {
    	foreach ($unc in $uncs)
    	{
        	sl "\\$server\$unc"
            $filecol = ls $files
    		foreach ($file in $filecol)
    		{
    		   	$results += New-Object psObject -Property @{
    				'FileName'=$file.Name;
    				'Server'=$server;
    				'LastWriteTime'=$file.LastWriteTime
    			}
            }
        }
    }

    I tried Test-Path cmdlet but doesnt seem to work properly with multiple file names.

    Any help appreciated!

    Thank you!

    Tuesday, February 12, 2013 11:28 PM

Answers

  • Try this...

    $results = @() $servers = Get-Content "C:\ServerNames.txt" $uncs = Get-Content "C:\UNCPath.txt" $files = Get-Content "C:\FileNames.txt" foreach ($server in $servers) { foreach ($unc in $uncs) { foreach($file in $files){ if(Test-Path "\\$server\$unc\$file"){

    $fileitem = Get-Item "\\$server\$unc\$file"

    $results += New-Object psObject -Property @{ 'FileName'=$fileitem.Name; 'Server'=$server; 'LastWriteTime'=$fileitem.LastWriteTime } } } }



    Inspired by Carlsberg.

    • Marked as answer by TechNetKing Wednesday, February 13, 2013 5:36 PM
    Tuesday, February 12, 2013 11:47 PM

All replies

  • Try this...

    $results = @() $servers = Get-Content "C:\ServerNames.txt" $uncs = Get-Content "C:\UNCPath.txt" $files = Get-Content "C:\FileNames.txt" foreach ($server in $servers) { foreach ($unc in $uncs) { foreach($file in $files){ if(Test-Path "\\$server\$unc\$file"){

    $fileitem = Get-Item "\\$server\$unc\$file"

    $results += New-Object psObject -Property @{ 'FileName'=$fileitem.Name; 'Server'=$server; 'LastWriteTime'=$fileitem.LastWriteTime } } } }



    Inspired by Carlsberg.

    • Marked as answer by TechNetKing Wednesday, February 13, 2013 5:36 PM
    Tuesday, February 12, 2013 11:47 PM
  • Thanks bud! that did it!

    Still learning here :)

    Wednesday, February 13, 2013 5:36 PM
  • We're all still learning here ;-)

    Inspired by Carlsberg.

    Wednesday, February 13, 2013 6:24 PM