1

I wish to search for specific files listed in searchFiles and pipe their locations to TestFileLocation.CSV. However, my current script only generates an empty CSV. What am I missing?

My TestFindFile.csv is of the form:

Name
123.pdf
321.pdf
aaa.pdf

SNIPPET

$searchFiles = Import-CSV 'C:\Data\SCRIPTS\PS1\TestFindFile.csv' -Header ("Name")
$source = 'C:\Data'

ForEach($File in $searchFiles) 
{
    Get-ChildItem $source -Filter $File -rec | where {!$_.PSIsContainer} | select-object FullName | export-csv -notypeinformation -delimiter '|' -path c:\data\scripts\ps1\TestFileLocation.csv
}
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
val
  • 1,629
  • 1
  • 30
  • 56

2 Answers2

1

You were overwriting the CSV for each iteration of the loop.

$searchFiles = Import-CSV 'C:\Data\SCRIPTS\PS1\TestFindFile.csv' -Header ("Name")
$source = 'C:\Data'
$outputPath = 'c:\data\scripts\ps1\TestFileLocation.csv'

$searchFiles |  ForEach-Object {
    # Silently continue to try to ignore error like
    # not being able to read path's which are too long
    Get-ChildItem $source -Filter $_ -rec -ErrorAction SilentlyContinue | where {!$_.PSIsContainer} | select-object FullName 
} | export-csv -notypeinformation -delimiter '|' -path $outputPath

Example using AlphaFS

A comment asked for an example using AlphaFS because it claims to overcome the long path issue. I'm not going into all the details, but here is how I got it to work.

# download and unzip to c:\alpahfs
# dir C:\AlphaFS\* -Recurse -File | Unblock-File 
[System.Reflection.Assembly]::LoadFrom('C:\AlphaFS\lib\net451\AlphaFS.dll')
$searchFiles = Import-CSV 'C:\Data\SCRIPTS\PS1\TestFindFile.csv' -Header ("Name")
$source = 'C:\Data'
$outputPath = 'c:\data\scripts\ps1\TestFileLocation.csv'

    $searchFiles |  ForEach-Object {
        $files = [Alphaleonis.Win32.Filesystem.Directory]::EnumerateFiles($source,'*',[System.IO.SearchOption]::AllDirectories) 
        $files | ForEach-Object { [PSCustomObject] @{FileName = $_} }
    } | export-csv -notypeinformation -delimiter '|' -path $outputPath
# type $outputPath
TravisEz13
  • 2,263
  • 1
  • 20
  • 28
  • @val What file path, the one in the code or the one in the CSV? Update: In case you mean the path to the CSV, I made it a variable. – TravisEz13 Jun 09 '16 at 00:29
  • Your solution worked on my test case. However, when I attempt to run a search on my server I get "Get-ChildItem: The specified path, file name, or both are too long." I suspect the file path to the files on the server may be getting too long. However, I don't understand if this hinders the search or only when it tries to write to the output csv. – val Jun 09 '16 at 00:32
  • 1
    This is a known issue in PowerShell. See [this PowerShell UserVoice issue](https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/11685975-long-path-support). Meaning if the path is too long, PowerShell cannot read it. I'll update the script as best I can to ignore the errors and continue, but I've heard reports that this doesn't work 100% of the time. – TravisEz13 Jun 09 '16 at 00:35
  • 1
    OK- i see that if i have a file that is longer than 260 characters it will not appear in my CSV list. This seems difficult to generate... – val Jun 09 '16 at 01:43
  • Yes, If you need this feature please vote up the [PowerShell UserVoice issue](https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/11685975-long-path-support). – TravisEz13 Jun 09 '16 at 01:45
  • Any idea how I would incorporate AlphaFS command into my snippet? I followed your links to this statement: C:\> [Alphaleonis.Win32.Filesystem.Directory]::EnumerateDirectories($Env:WinDir, 'a*') – val Jun 09 '16 at 01:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114170/discussion-between-travis-plunk-and-val). – TravisEz13 Jun 09 '16 at 02:23
1

If your .csv file contains the header "Name", there is no need to again declare it when running Import-Csv.

The reason the output is empty is that you are searching for an Object which contains the property Name (imported from the TestFindFile.csv). Search for $File.Name. Also pull commands outside the loop that don't need to be there:

$searchFiles | Select -ExpandProperty Name | % {
   Get-ChildItem $source -Filter $_ -Recurse | where {!$_.PSIsContainer}
} | select-object FullName | export-csv -notypeinformation -delimiter '|' -path c:\data\scripts\ps1\TestFileLocation.csv
xXhRQ8sD2L7Z
  • 1,686
  • 1
  • 14
  • 16