Powershell Tip #94: Delete files and folder longer than 259 characters

By | March 5, 2016

Tip: You can delete files and folders longer than 259 characters (long path).

I saw some people wasting a lot of time by map /unmap multiple drives to reduce length, delete the files manually, etc.
So I decided to explain that in an article in case you need to delete long path files.

I will show an easy way using the module AlphaFS to create, copy, remove and delete files / folders which support more than 32 000 characters.
Download (AlphaFS.2.0.1.zip) : https://github.com/alphaleonis/AlphaFS/releases/latest

The module is also available on PowerShell Gallery although the current version has only very few cmdlets available :
https://www.powershellgallery.com/packages/PSAlphaFS/1.0.0.0

I will publish another article to show how to do that without using the external module AlphaFS (although this module uses the same technique by importing kernel32.dll).

In this post, there are 2 parts :

Part 1 (Files)      : Create, copy, move, search and delete
Part 2 (Folders) : Search and delete folders

===

DeleteFile function
https://msdn.microsoft.com/en-us/library/aa363915(VS.85).aspx

ANSI            : The name is limited to MAX_PATH characters (260 characters).
UNICODE : To extend this limit to 32 767 wide characters, call the Unicode version of the function and prepend “\?\” to the path.”

===

Download (AlphaFS.2.0.1.zip) : https://github.com/alphaleonis/AlphaFS/releases/latest

Then, you have to unblock the file, otherwise, you will receive this eror : Import-Module : Could not load file or assembly ‘file:///C:\Demo\AlphaFS.2.0.1\lib\net451\AlphaFS.dll’ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

Import-Module-error-powershell-alphafs

To unblock the file:

PowerShell : Unblock-File -Path 'C:\Demo\AlphaFS.2.0.1.zip'

Unblock-File-Powershell-AlphaFS-library-cmdlet

or

GUI : right click, Properties and Unblock.

Unblock-File-Powershell-AlphaFS-library

Let’s create a path of 362 characters for the test.

long-path-windows-larger-than-259-characters-alphafs-module

I have now a very long path created for this demo:

===

PART 1: FILES

# Create

It will create a file to : \\fs01\Test\subfolder1\…\subfolder30\file1.txt

long-path-windows-larger-than-259-characters-create-file-alphafs

===

# Move

I created a demo file here : \\fs01\Test\file2.txt
I move this demo file from \\fs01\Test\file2.txt to \\fs01\Test\subfolder1\…\subfolder30\file2.txt

long-path-windows-larger-than-259-characters-move-file-alphafs

===

# Copy

I created a demo file here : \\fs01\Test\file3.txt
I copy this demo file from \\fs01\Test\file3.txt to \\fs01\Test\subfolder1\…\subfolder30\file3.txt

long-path-windows-larger-than-259-characters-copy-file-alphafs

===

# Search

Note: The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

It will return 3 files :

File1 : Created
File2: Moved
File3 : Copied

alphafs-module-long-path-windows-larger-than-259-characters-searchfiles

===

# Delete (one single file)

The file1.txt has been deleted.

long-path-windows-larger-than-259-characters-create-file-alphafs-deleted-multiple-files-module-alphafs

===

# Delete (multiple files)

The file2.txt and file3.txt have been deleted.

module-alphafs-very-long-path-windows-larger-than-259-characters-delete-multiples-files

===

PART 2: FOLDERS

# Search (one folder and subfolders)

We search a subfolder “subfolder28”:

alphafs-module-long-path-windows-larger-than-259-characters-searchfolders

# Delete (one folder and subfolders)

We delete the folder \\fs01\Test\subfolder1\…\subfolder29\*.* and all subfolders.

long-path-windows-larger-than-259-characters-delete-folder-and-subfolders-alphafs-module

To specify that the deletion is recursive I set $true for boolean parameter recursive.

long-path-windows-larger-than-259-characters-delete-folder-and-subfolders-recursively

To list overload definitions : [Alphaleonis.Win32.Filesystem.Directory]::Delete

long-path-windows-larger-than-259-characters-overload-definitions

To list static methods of File Class :  [Alphaleonis.Win32.Filesystem.File] | Get-Member -Static -MemberType Method

long-path-windows-larger-than-259-characters-method

Online documentation

File Class
http://alphafs.alphaleonis.com/doc/2.0/api/html/F5429666.htm

Directory Class
http://alphafs.alphaleonis.com/doc/2.0/api/html/26653682.htm


previous-buttonnext-button

2 thoughts on “Powershell Tip #94: Delete files and folder longer than 259 characters

  1. Pingback: Powershell Tip #93: Use the “2 in 1” cmdlet Tee-Object | Powershell Guru

  2. Pingback: Powershell Tip #95: Find the Geolocation of an IP Address | Powershell Guru

Leave a Reply

Your email address will not be published. Required fields are marked *