ondrovic
ondrovic

Reputation: 1175

PowerShell Search look for files in a text file

Need to loop through a list of file stored in a .txt file to find the path of each file

 files.txt
 example of the file in files.txt > 3030009948_3030009912_df1389947f0fb80d62832122.sasf

Using this

Get-ChildItem “file_location” -Recurse -Include “*.**” | foreach-object {$_.Fullname} | Out-File c:\files2.txt –width 1024
File Location is a network share Z:

Search for each file stored in files.txt export the path of the file in files2.txt Files will be found on Z: - network share

Upvotes: 0

Views: 162

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36342

You kind of have what you need already, so this is fairly simple, you just need to read the text file in and pipe it to your Get-ChildItem in a ForEach loop (I use Select -Expand FullName instead of looping each and selecting the property), then pipe that to Out-File.

GC files.txt | %{GCI Z:\$_ -Recurse|Select -Expand FullName} | Out-File Z:\Files2.txt

Edit: Ok, so you say that didn't work, but I don't know why it wouldn't. It worked fine for me on my machine when I tested it. It may have been slow anyway though since it was pulling a directory listing for a network drive once for each file, so we'll do this differently.

$Files = GC files.txt
$DirList = GCI Z:\ -Recurse
$Dirlist | ?{$Files -contains $_.Name} | Select -Expand FullName | Out-File Z:\Files.txt

That loads your list of files into an array, then pulls a full directory listing for the Z:\ drive, then filters the directory listing checking each file to see if it's name is in the list of desired files, and expands the FullName property, and saves it to the desired file.

Upvotes: 1

Related Questions