3

I'm trying to build a comma separated list of file names in a specific folder. These files tend to have long names (they're test case results and the name is very descriptive).

I've found that Get-ChildItem ignores files whose name (including path) is too long.

# Folder contains the following file (240 chars, actual name is 'only' 210 chars):
# SomeServiceWithAnAlreadyLongNameUnderTest_NameOfTheParticularTestSuiteThatProducedThisResult-NameOfTheActualMethodUnderTest-Given_A_Certain_Precondition_Is_Met_WhenCalling_With_Someparameter_As_SomeValue_Then_Result_Is_Something-Failed.txt
Get-ChildItem *.txt -name
# I would have loved for this to work:
(Get-ChildItem *.txt -name) -join ','

The result does not include the file, since the path+filename are too long. There's no error or warning either.

As a work around I'm using a cmd script file with for:

for %%a in (%XMLPath%\*.xml) do ( ... build comma separated list with %%a ... )

How can I the full list including (too?) long names in Powershell?

Onots
  • 2,118
  • 21
  • 28
  • 1
    Try using [System.IO.Directory]::GetFiles() to obtain you paths collection and work with that. – Martin Maat Dec 25 '15 at 10:21
  • Also try prefixing your path with \\?\ – JamesQMurphy Dec 25 '15 at 18:29
  • @MartinMaat, GetFiles() has the same limitation. – majkinetor Dec 25 '15 at 19:42
  • 1
    PowerShell is build on .NET which does not support long file paths. Check this out. http://stackoverflow.com/questions/12204186/error-file-path-is-too-long – Martin Maat Dec 25 '15 at 20:50
  • 1
    If you want to build your own .NET assembly (to Add-Type into your PowerShell script) that works around this limitation by addressing Win32 system calls, this may help: http://www.codeproject.com/Articles/19688/In-C-Use-Win-API-to-Enumerate-File-and-Directory – Martin Maat Dec 25 '15 at 21:02
  • 1
    FYI UNC paths are now supported by default in PowerShell v6.0.0-beta.3: https://stackoverflow.com/a/47046785/1599699 – Andrew Nov 01 '17 at 01:10

3 Answers3

3

This seems to be a really popular question because it has been asked multiple times on this site.Anyway here are your alternatives:

Use Pinvoke

Boe Prox - Technet

Use Robocopy

Boe Prox - Technet

Use AlphaFS with Powershell V5 and V4

if you can find a computer with PowerShell v5 (windows 10 comes with PS v5) you can install a module called PSAlphaFS .

Install-Module PSAlphaFS

by default the modules from powershellgallery get installed to

C:\Program Files\WindowsPowerShell\Modules

Note:The PSAlphaFS module can also be run on a target system with PowerShell V4 but not on powershell V3.

Here is how you can find out the version of Powershell that is currently running.

$PSVersionTable.PSVersion

Once the module is in place you can then run the following command which is similar to get-childitem but without the 260 char limitation.

Get-LongChildItem  -Filter *.txt -Name
Kiran Reddy
  • 2,836
  • 2
  • 16
  • 20
1

It appears there is no straightforward way to handle this situation using PowerShell. To me that means PowerShell is not the right tool for this job and I'm going to use my "work-around" cmd script instead.

Onots
  • 2,118
  • 21
  • 28
  • It's a limitation in the .net frameworks, and not pr say in Powershell. But still, Powershell is build apon .net so in some sens, Powershell inherit this limitation. But I would disagree, that it's not the right tool. There is planty og great tools you can use to help Powershell, and not as a workaround. – Daniel Lindegaard Nov 25 '16 at 20:47
1

PowerShell v6.0.0-beta.3 and up now supports UNC paths by default; see my answer here for more information: https://stackoverflow.com/a/47046785/1599699

Andrew
  • 5,839
  • 1
  • 51
  • 72