Windows Cmd Tip - Handling files with long names


Your question asks how to delete a file when the filename itself is too long. If the path is too long and not just the filename itself, you can easily just rename the folders to make the path shorter.

But to delete a file whose name is more than 255 characters:

    Open a command prompt by running "CMD.EXE"
    Navigate to the folder holding the file
    Use the command DIR /X which will display the short names of files.
    Delete using the short name.

i.e. if the file is named "verylongfilename.fil", the shortname will display as something like "verylo~1.fil" and you can delete using that name.

Please note that although Windows itself can handle paths far longer than 255 characters, most of the GUI -- like Windows Explorer -- is bound by the shorter limit.
------------------------------------------------------------------

Article for Windows 8:
Assuming you are on windows:

What I always do is use subst to create a driveletter asccociation with part of the path.

Then go to the new driveletter and navigate to the files that have long names. You should now be able to rename/delete/etc them. The reason this works is because the path itself is no longer containing >255 chars.
To create a subst folder

    Start a commandprompt (no admin privileges needed)
    Use cd to navigate to the folder you want to go (you can use tab to autocomplete names
    type subst j: . to create the driveletter association. (instead of the . you can also type the entire path)
    Now in explorer you have a new letter. Go to it and do whatever you need to do to the .cache files.
    Go back to your cmd window and type subst /d j: to remove the drive or alternatively, restart your pc.

EDIT: A trick I learned which can shorten this quite a bit. From the explorer window where you have trouble, click the addressbar so you can type in it, then type cmd and press Enter to start a command prompt at that location, then proceed at step 3. :)

EDIT2: Okay, this trick can be expanded even further. If you are in explorer at the right path, just type in subst j: . in the addressbar and press enter. A command window will pop up, execute the command and close again, and there you go, a new drive pops up in explorer.
----------------------------------------------------------------


From a command prompt:

dir /X

This will list your files/folders in short name format. Then use the short name exactly as written to delete the file:

del LONGF~1.txt
...........................

Example:
C:\Users\Ashish Jain\Desktop\Mails\SPRS>dir /x
 Volume in drive C is Windows
 Volume Serial Number is 20FE-37BF

 Directory of C:\Users\Ashish Jain\Desktop\Mails\SPRS

26-Apr-17  11:11 AM    [DIR]                       .
26-Apr-17  11:11 AM    [DIR]                       ..
20-Feb-17  09:24 PM           319,488 RE~ETI~1.MSG RE   ~ETIAE-2599    SPRS GUI issue   MENA   R-ISC-VAS Update Etisalat Remarks for FR  MobileUM CRR(Centralized Roaming Replicator) FR-CRR(Centralized Roaming Replicator)-44342 No GUI access for Managing GT Pool Categories.msg
               1 File(s)        319,488 bytes
               2 Dir(s)  72,805,195,776 bytes free

C:\Users\Ashish Jain\Desktop\Mails\SPRS>del RE~ETI~1.MSG
--------------------------------------------------------------

No comments:

Post a Comment