Powershell Copying Long Path Folders

Sep 5, 2012 at 12:52 AM
Edited Sep 5, 2012 at 12:52 AM

I'm using AlphaFS to handle long file paths that continually appear within my work enviroment.

What I've been attempting to do is setup a copy function to copy a folder and its files to a new location.

I've been using the following code to acheive this:

 

# RecursePath function.
Function RecursePath([string]$SourcePath, [string]$DestPath){

    # for each subdirectory in the current directory..       
        [Alphaleonis.Win32.Filesystem.Directory]::GetDirectories($SourcePath) | % {
            $ShortDirectory = $_
            $LongDirectory = [Alphaleonis.Win32.Filesystem.Path]::GetLongPath($ShortDirectory)

            # Create the directory on the destination path.
            [Alphaleonis.Win32.Filesystem.Directory]::CreateDirectory($LongDirectory.Replace($SourcePath, $DestPath))

            # For each file in the current directory..                                              
            [Alphaleonis.Win32.Filesystem.Directory]::GetFiles($ShortDirectory) | % {

                $ShortFile = $_
                $LongFile = [Alphaleonis.Win32.Filesystem.Path]::GetLongPath($ShortFile)

                # Copy the file to the destination path.                                                                       
                [Alphaleonis.Win32.Filesystem.File]::Copy($LongFile, $LongFile.Replace($SourcePath, $DestPath), $true)                             
        }

    # Loop.
    RecursePath $ShortDirectory $DestPath
    }
}

 

This code works, but have issues when being with restricted folders (I have full security permissions), returning an error message of "Accesss is Denied"

Is there a way to force a copy? The copy works when doing a standard powershell 'Copy-Item', however it cannot handle long paths.

Developer
Sep 5, 2012 at 5:50 AM

Hi Tiazi,

Probably something with SE_SECURITY privilege, which is required if you are not running as administrator.

Did you already looked at: Alphaleonis.Win32.Security.PrivilegeEnabler()

Please see this discussion also: http://alphafs.codeplex.com/discussions/258879

Regards,
Yomodo

Sep 7, 2012 at 5:47 AM
Edited Sep 7, 2012 at 5:49 AM

Hey Yomodo,

Sorry for not being clear in my orginal question, this is running as an Administrator.

When the the file copy is structured like:

[Alphaleonis.Win32.Filesystem.File]::Copy($LongFile, $LongFile.Replace($SourcePath, $DestPath), $true)

It returns the error message "The process cannot access the file because it is being used by another process", which leads me to suspect that I may be locking myself out in the above first post code.

When I change it around to:

[Alphaleonis.Win32.Filesystem.File]::Copy($LongFile, $DestPath, $true)

It returns the access denied.

I'll keep looking into it to see if I can figure it out, any advise would be greatly appreciated. Thanks.

Sep 28, 2012 at 3:32 AM

I resolved my issue, was flawed logic causing the problem. The following code works:

Function RecursePath([string]$RootPath, [string]$SourcePath, [string]$DestPath){
        
    $RootPath = [Alphaleonis.Win32.Filesystem.Path]::GetLongPath($RootPath)
    $DestPath = [Alphaleonis.Win32.Filesystem.Path]::GetLongPath($DestPath)
        
    # for each subdirectory in the current directory..       
        [Alphaleonis.Win32.Filesystem.Directory]::GetDirectories($SourcePath) | % {
            $ShortDirectory = $_
            $LongDirectory = [Alphaleonis.Win32.Filesystem.Path]::GetLongPath($ShortDirectory)

            Write-Host "Creating directory $($LongDirectory.Replace($RootPath, $DestPath))"
            
            # Create the directory on the destination path.
            [Alphaleonis.Win32.Filesystem.Directory]::CreateDirectory($LongDirectory.Replace($RootPath, $DestPath))

            # For each file in the current directory..                                              
            [Alphaleonis.Win32.Filesystem.Directory]::GetFiles($LongDirectory) | % {

                $ShortFile = $_
                $LongFile = [Alphaleonis.Win32.Filesystem.Path]::GetLongPath($ShortFile)
                
                Write-Host "Copying file to $($LongFile.Replace($RootPath, $DestPath))"
                
                # Copy the file to the destination path.                                                                     
                [Alphaleonis.Win32.Filesystem.File]::Copy($LongFile, $LongFile.Replace($RootPath, $DestPath), $true)                             
        }

    # Loop.
    RecursePath $SourcePath $ShortDirectory $DestPath
    }
}
Marked as answer by Yomodo on 1/19/2014 at 9:43 PM
Developer
Sep 28, 2012 at 8:05 AM

Tiazi, thank you for reporting back!

Glad you've solved it!

Regards,
Yomodo

Dec 7, 2012 at 10:14 AM

"Long Path Tool" is a new brand tool specially made to solve the particular problems effectively. It can remove the blocked file path very easier and it will solve your problem as well. This tool is very helpful to solve below mentioned problems :-

1. Error Copying File or Folder.
2. Cannot remove folder.
3. The filename or extension is too long.
4. Path too deep.
5. Destination Path Too Long.
6. Could not find this item.
7. Filename is not valid.

Developer
Feb 20, 2013 at 11:26 AM
Tiazi,

You can use Directory.Copy() or DirectoryInfo.CopyTo() to achieve the same.

Regards,
Yomodo
Mar 18, 2013 at 6:56 PM
Edited Mar 18, 2013 at 6:56 PM
Use Long Path tool. It fix my problems, i dont have time to rename files so i download this software. I had a lot of files to rename. Try this one it really helps .
http://LongPathTool.com
Developer
Mar 18, 2013 at 7:11 PM
No, it does not.
Mar 18, 2013 at 10:46 PM
Sorry, I thought I'd made it clear that I'd resolved my issue in my Sep 28, 2012 post. I got the longfile path code working using AlphaFS in powershell and have been running it without any issues in a number of scripts.
Developer
Mar 18, 2013 at 11:39 PM
Tiaz,

Have you looked at the Directory.Copy() / DirectoryInfop.Copy() methods?

These methods do recursive copying or moving of directories, probably faster and robust.

Regards,
Yomodo
Mar 19, 2013 at 12:26 AM
It was sometime ago so I'm 100% sure, but I don't think I could get the Directory.Copy() to work.

When I get some free time, I'll go back and test out Directory.Copy() . I'll let you know my results once I get a chance to play with the method.
Mar 20, 2013 at 3:04 PM
You can always use robocopy for simplicity sake.