question

Ed7 avatar image
0 Votes"
Ed7 asked MotoX80 commented

Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

HI,

I am having issues with my script that cannot get the path from certain files/folders.
Others files/folders it does the job but I would like to know how to handle this and avoiding to give me this erro please.

Here s my current script@


 #Message to user
 Write-Output "`nThis script will collect the FilePathName, Name, RelativePath and the Hash of the files/folders you have chosen."
              "`nAt the end, it will tell you how long it took to run the script"   
 Read-Host -Prompt "`nPress ENTER to continue or CTRL+C to quit" 
    
 #Counting the time from the beginning
 $starttime = (Get-Date)
    
 #Variables
 #Can only be added 3 paths maximum. If added more than 3 it may change the relatrive path
 $root = "C:\mypath"
    
    
 #Variable for creating the CSV File
 $report = "mycsvfile.csv"
    
    
 #Process for generating the HASH
 $hasher = [System.Security.Cryptography.SHA256]::Create()
 $AllFiles = @() 
    
 "`n"#line space
    
 Write-Host "Generating the Hash from $root" 
    
    
    
 #Getting information from directories
 foreach ($file in get-childitem $root -recurse | Select-Object FullName, Directory, Name, PSIsContainer, Length)
 {
     $acl = get-acl $file.fullname | select-object owner,accesstostring,group
     $obj = new-object psObject
    
    
 #Generating HASH File
     if(!$file.PsIsContainer)
         {
         $relativePath = $file.FullName.Substring($root.Length)
         Write-Host "Debug $relativePath" -ForegroundColor Green
    
         $inputStream = New-Object IO.StreamReader $file.fullname
         $hashBytes = $hasher.ComputeHash($inputStream.BaseStream)
         $inputStream.Close()
    
         $builder = New-Object System.Text.StringBuilder
         $hashBytes | Foreach-Object { [void] $builder.Append($_.ToString("X2")) }
    
 #Add info into CSV FILE
         $obj | Add-Member -membertype noteproperty -name FilePathandName -Value $file.FullName
         $obj | Add-Member -membertype noteproperty -name Name -Value $file.Name
         $obj | Add-Member -MemberType noteproperty -Name RelativePath -Value $relativePath #-force
         $obj | Add-Member -MemberType noteproperty -Name Hash -Value $builder.ToString()
         #$obj | Add-Member -membertype noteproperty -name CreationTime -Value $file.CreationTime
         #$obj | Add-Member -MemberType noteproperty -Name LastAccessTime -Value $file.LastAccessTime
         #$obj | Add-Member -MemberType noteproperty -Name LastWriteTime -Value $file.LastWriteTime
    
            
     #Variable to send info to CSV
     $AllFiles += $obj
     Clear-Variable relativePath
     }
 Remove-Variable obj
 }
    
 #$AllFiles += $obj
    
 #Generating CSV FILE
 $AllFiles |Export-Csv $report –NoTypeInformation
    
 "`n"
 Write-Host "$report File has been created "
    
 "`n"
 Write-Host "The script took:`n"
 $endTime = Get-Date
 New-TimeSpan -Start $startTime -End $endTime
windows-serverwindows-server-powershelloffice-scripts-excel-dev
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.

MotoX80 avatar image
0 Votes"
MotoX80 answered
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.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @Ed7 ,

Maybe you can try this:

In line 11 modify this $root = "C:\mypath" to $root = "\\?\C:\mypath" .


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

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.

Ed7 avatar image
0 Votes"
Ed7 answered MotoX80 commented

After changing the $root for:
$root = \\?\C:\mypath"

I get this error which I am confused and not understanding what may wrong in those lines.


get-childitem : Illegal characters in path.
At C:\mypath\myscript.ps1:29 char:19
+ foreach ($file in get-childitem $root -recurse | Select-Object FullName, Directo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ChildItem], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.GetChildItemCommand



Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\mypath\myscript.ps1:69 char:13
+ $AllFiles | Export-Csv $report #–NoTypeInformation
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand



@AndreasBaumgarten and @MotoX80 Anythoughts on this



· 7
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.

It's really $root = \\?\C:\mypath"? (There a missing " in front of the \\ )

It should be $root = "\\?\C:\mypath"


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

0 Votes 0 ·
Ed7 avatar image Ed7 AndreasBaumgarten ·

Sorry that was my fault when typing here but I get the same error as mentioned before.

Not sure what is causing this.

0 Votes 0 ·

the `\\?` in front of a path should work for long paths ... at least that was the idea behind:

https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd

But it looks like it doesn't work.

Did you try to enable the "long paths" like MotoX80 wrote above?


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

0 Votes 0 ·
Show more comments
MotoX80 avatar image
0 Votes"
MotoX80 answered

After enabling the long file paths in Windows it still does not work.

Did you reboot?

What OS is this?

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.

Ed7 avatar image
0 Votes"
Ed7 answered GaryReynolds commented

Yes I have rebooted and it still does not work.

This is windows server 2012 as I am transfering some data.

· 4
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.

I am transfering some data.

Try using robocopy to copy the files.

0 Votes 0 ·

I I will use Robocopy to transfer the files.

Teh script above is to get the relative path and teh hash of the files to check integrity when the data has been transfered into the new server location as I will run the script there and then run another script to compare the files

0 Votes 0 ·

I think that robocopy is used pretty widely. I have not experienced data corruption when using it. Normally when doing a big migration, we ran it multiple time using the /mir switch, because users were still actively accessing the files. When we did the final cutover, we removed end user access to the source share and then ran a final robocopy.

Pick a subfolder that does not have the path length problem. Copy the data with robocopy, Then use your script to verify that robocopy worked correctly. If it did, then you have a very high probability that it's going to work on all of the files.

0 Votes 0 ·

To reiterate motoX80 comment. I've used robocopy on numerous server migrations and consolidations, using the same method and have never seen any data corruption issues.

Gary.

0 Votes 0 ·