question

McNaughtonJohn-2514 avatar image
0 Votes"
McNaughtonJohn-2514 asked MotoX80 answered

Importing a list of Folder names to search a network drive using Powershell

So, I am still new to writing my own scripts and am trying to search a network drive against a list of folder names for the folders that are on the list but NOT in the network drive. Because the list of names is long, I am thinking that importing a .csv is my best way forward and importing the list to the code.
This is the code I have made so far, now; it is not working (hence asking this question) but I am stumped as to if I am just completely off kilter on my script or if I am close, but it is a syntax issue of some kind. Any help to point me in the right direction is welcome.

     $Folders = Import-Csv "path for csv"
        
     $FolderReport =            
     foreach ($Folder in $Folders) {
         $name = $Folder.Name
          
         Get-Childitem -Path "Network Drive" -recurse -include {Name -eq $name}
     }
        
     $FolderReport |
     export-csv 'Where it's written to' -NoTypeInformation -Force
        
     Start 'opening the file'
windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

There's not enough information to assess the validity of your script.

Are you interested only matching the file name? Or must the location of the file in the directory hierarchy be the same too?
Assuming the directory hierarchies are the same, does the CSV have the full path names of only the files or does it also contain the directory paths?
Are the drive letters on which the top level folder of the directory exists on the local system and the remote system the same?

I wouldn't recommend using Get-ChildItem simply to verify the presence of a file. Use Test-Path for that.

When you say that the list of file names is long, about how may files are there in the list? It may be faster to build a hash of the file paths and use the path as the hash key to check for the existence of the 'other' file.

0 Votes 0 ·

1 Answer

MotoX80 avatar image
0 Votes"
MotoX80 answered

for the folders that are on the list but NOT in the network drive

How about something like this?

 $Folders = Import-Csv "c:\temp\files.csv"
 $Found = @()                                  # Array of names that we found
 $NotFound = @()                               # Array of names that we did not find 
                
 foreach ($Folder in $Folders) {
     "Searching for {0}" -f $Folder.Name
              
      $Results=  Get-Childitem -Path "c:\temp" -recurse -include $Folder.Name
      "I found {0} objects" -f $Results.count
      # Add entry into appropriate array 
      if ($Results.count -eq 0) {
         $NotFound += [PSCustomObject]@{
             Name = $Folder.Name
         } 
      } else {
         $Found += [PSCustomObject]@{
             Name  = $Folder.Name
             Count = $Results.count
         } 
      }
 }
          
 $Found | Export-Csv c:\temp\Found.csv -NoTypeInformation            
 $NotFound | Export-Csv c:\temp\NotFound.csv -NoTypeInformation            
    
    
 "Here are the names that I found."      
 Get-Content c:\temp\Found.csv
 "Here are the names that I did not find."
 Get-Content c:\temp\NotFound.csv      

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.