PowerShell - how to capture filepath names, including long filepath names with this code + AlphaFS

孤街醉人 提交于 2019-12-12 05:01:19

问题


This question is an extension of PowerShell - searching for existing files generates empty output.

I'm having trouble capturing long file path names with AlphaFS. I've looked at related AlphaFS questions but I can't see how they address my issue: The code below is intended to capture files in a $source directory into a .csv, including those with paths that are longer than 260 characters. However, the output I get in TestFileLocations.csv consists of 1 line that reads

IsReadOnly|"IsFixedSize"|"IsSynchronized"|"Keys"|"Values"|"SyncRoot"|"Count"

followed by 125060 lines that read

False|"False"|"False"|"System.Collections.Hashtable+KeyCollection"|"System.Collections.Hashtable+ValueCollection"|"System.Object"|"1"

PS Code

[System.Reflection.Assembly]::LoadFrom('C:\AlphaFS\AlphaFS\lib\net35\AlphaFS.dll')
$searchFiles = Import-CSV 'C:\Data\SCRIPTS\PS1\TestFindFile.csv' -Header ("Name")
$source = 'C:\Data\Scripts'
$outputPath = 'c:\data\scripts\ps1\TestFileLocation.csv'

    $searchFiles |  ForEach-Object {
        $files = [Alphaleonis.Win32.Filesystem.Directory]::EnumerateFileSystemEntries($source,'*',[System.IO.SearchOption]::AllDirectories) 
        $files | ForEach-Object { [PSCustomObject] @{FileName = $_} }
    } | export-csv -notypeinformation -delimiter '|' -path $outputPath

My TestFindFile.csv contains only 4 lines pertaining to 3 existing files that should be found by the code. The last file has 262 characters:

Name
123.pdf
321.pdf
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt

I'm on Windows 7. Any pointers would be appreciated.


回答1:


I wrapped Alphafs into a PS module which you can find on the PowerShell Gallery here.

Since it is a module I cannot post the whole code but can include a link from where you can download it.

PSAlphaFS

Once you have the module you can do something like this :

 Get-LongChildItem -path yourpath | export-csv output.csv -notype


来源:https://stackoverflow.com/questions/37718372/powershell-how-to-capture-filepath-names-including-long-filepath-names-with-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!