Batch to delete directory with path-too-long problem.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
psychoid69
Posts: 18
Joined: 02 May 2013 06:09

Batch to delete directory with path-too-long problem.

#1 Post by psychoid69 » 30 May 2013 03:30

I've had an interesting problem yesterday. I somehow managed to create infinite loop that created directories so deep that Windows could not handle any more. To clarify, the result was i:\one\two\one\two\one\two ... \one\two ... There was 471 subdirectories in the end.

I have solved the deletion of this with the help of move. I think that what I did falls into divide and conquer type of solving problems, but it was done totally manual.

What I did was to go pretty deep into the tree with Windows Explorer, copy the path, go to cmd, change dir and then move the subdirs to i:\fix\01-10
So, move worked, even when rd wouldn't. By "cutting the tree" like that, I could delete the "branches" that were not too long anymore.

And now to the point. Can someone come up with a way to put this logic in a batch? I think the scrip should be run at the top of such tree and then go as deep as the filesystem allows, move te rest upwards, delete the remaining tree, go to the moved directory, go as deep as the filesystem allows, etc. loop until cut to small enough pieces and then just do rd /s /q on them.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch to delete directory with path-too-long problem.

#2 Post by foxidrive » 30 May 2013 03:57

Someone has to reproduce a path that is too long, to test it.

I pass. Too lazy. :D Well, on second thoughts.

Code: Select all

@echo off
:loop
md one
cd one
md two
cd two
goto loop
pause


I didn't have any trouble deleting the folder it made though. It didn't create 471 folders.

Directory of m:\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one\two\one


30/05/2013 20:02 <DIR> .
30/05/2013 20:02 <DIR> ..
0 File(s) 0 bytes

Total Files Listed:
464 File(s) 64,421,391 bytes
10095 Dir(s) 19,088,896 bytes free

m:\>rd /s /q one


psychoid69
Posts: 18
Joined: 02 May 2013 06:09

Re: Batch to delete directory with path-too-long problem.

#3 Post by psychoid69 » 30 May 2013 04:28

Oh, the problem (the loop) was created using File Server Migration Wizard.

Ricktor
Posts: 1
Joined: 30 May 2013 13:59

Re: Batch to delete directory with path-too-long problem.

#4 Post by Ricktor » 30 May 2013 14:03

thanks for the info.,you could also try long path tool. it helped me with error 1320 in Win 7., :D

psychoid69
Posts: 18
Joined: 02 May 2013 06:09

Re: Batch to delete directory with path-too-long problem.

#5 Post by psychoid69 » 31 May 2013 00:02

Hm, yeah... LongPathTool is not free... I know we can make batch file that will be free and open... ;)

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Batch to delete directory with path-too-long problem.

#6 Post by Endoro » 31 May 2013 01:05

I used a batch script to create some folders in XP:

Code: Select all

@echo off
:loop
set /a counter +=1
echo %counter%
md "%counter%" || goto:eof
cd "%counter%" || goto:eof
goto:loop

it stopped at #85 with the message "file name or extension too long".
I removed it without problems "rd /s /q 1".

I created more subfolders with a third party file manager (SpeedCommander).
I could enter the folders until #88 with the "cd" command, for #89 I got the error message "path not found".

Code: Select all

C:\1\2\3\4\5\6\7\8\9\10\11\12\13\14\15\16\17\18\19\20\21\22\23\24\25\26\27\28\29\30\31\32\33\34\35\36\37\38\39\40\41\42\43\44\45\46\47\48\49\50\51\52\53\54\55\56\57\58\59\60\61\62\63\64\65\66\67\68\69\70\71\72\73\74\75\76\77\78\79\80\81\82\83\84\85\86\87\88>

"tree" also stopped at #88:

Code: Select all

C:\1
└───2
    └───3
        └───4
            └───5
                └───6
                    └───7
                        └───8
                            └───9
                                └───10
                                    └───11
                                        └───12
                                            └───13
                                                └───14
                                                    └───15
                                                        └───16
                                                            └───17
                                                                └───18
                                                                    └───19
                                                                        └───20
                                                                            └───21
                                                                                └───22
                                                                                    └───23
                                                                                        └───24
                                                                                            └───25
                                                                                                └───26
                                                                                                    └───27
                                                                                                        └───28
                                                                                                            └───29
                                                                                                                └───30
                                                                                                                    └───31
                                                                                                                        └───32
                                                                                                                            └───33
                                                                                                                                └───34
                                                                                                                                    └───35
                                                                                                                                        └───36
                                                                                                                                            └───37
                                                                                                                                                └───38
                                                                                                                                                    └───39
                                                                                                                                                        └───40
                                                                                                                                                            └───41
                                                                                                                                                                └───42
                                                                                                                                                                    └───43
                                                                                                                                                                        └───44
                                                                                                                                                                            └───45
                                                                                                                                                                                └───46
                                                                                                                                                                                    └───47
                                                                                                                                                                                        └───48
                                                                                                                                                                                            └───49
                                                                                                                                                                                                └───50
                                                                                                                                                                                                    └───51
                                                                                                                                                                                                        └───52
                                                                                                                                                                                                            └───53
                                                                                                                                                                                                                └───54
                                                                                                                                                                                                                    └───55
                                                                                                                                                                                                                        └───56
                                                                                                                                                                                                                            └───57
                                                                                                                                                                                                                                └───58
                                                                                                                                                                                                                                    └───59
                                                                                                                                                                                                                                        └───60
                                                                                                                                                                                                                                            └───61
                                                                                                                                                                                                                                                └───62
                                                                                                                                                                                                                                                    └───63
                                                                                                                                                                                                                                                        └───64
                                                                                                                                                                                                                                                            └───65
                                                                                                                                                                                                                                                                └───66
                                                                                                                                                                                                                                                                    └───67
                                                                                                                                                                                                                                                                        └───68
                                                                                                                                                                                                                                                                            └───69
                                                                                                                                                                                                                                                                                └───70
                                                                                                                                                                                                                                                                                    └───71
                                                                                                                                                                                                                                                                                        └───72
                                                                                                                                                                                                                                                                                            └───73
                                                                                                                                                                                                                                                                                                └───74
                                                                                                                                                                                                                                                                                                    └───75
                                                                                                                                                                                                                                                                                                        └───76
                                                                                                                                                                                                                                                                                                            └───77
                                                                                                                                                                                                                                                                                                                └───78
                                                                                                                                                                                                                                                                                                                    └───79
                                                                                                                                                                                                                                                                                                                        └───80
                                                                                                                                                                                                                                                                                                                            └───81
                                                                                                                                                                                                                                                                                                                                └───82
                                                                                                                                                                                                                                                                                                                                    └───83
                                                                                                                                                                                                                                                                                                                                        └───84
                                                                                                                                                                                                                                                                                                                                            └───85
                                                                                                                                                                                                                                                                                                                                                └───86
                                                                                                                                                                                                                                                                                                                                                    └───87
                                                                                                                                                                                                                                                                                                                                                        └───88


"rd /s /q 1" could not remove this long path ("path not found", but the file manager did it.
Imo you can code a command line tool with C, Pascal ... (VB?) which can also remove such long paths.

psychoid69
Posts: 18
Joined: 02 May 2013 06:09

Re: Batch to delete directory with path-too-long problem.

#7 Post by psychoid69 » 31 May 2013 02:31

Endoro, thank you for your valuable input!

Just to clarify - file manager that succeeded with deletion was SpeedCommander?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Batch to delete directory with path-too-long problem.

#8 Post by Endoro » 31 May 2013 06:30

yes, of course. you can have a fully functioning test version to remove the annoying path.

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Batch to delete directory with path-too-long problem.

#9 Post by carlos » 01 Jun 2013 14:07

The problem is not with the number of directorys. Is about the length of the path.
256 is the limit of the path.
If you want bypass the limitation you need use unc notation appending this at the begin of path: \\?\

Example, currently I'm not in windows, but for example if you want do this:

Code: Select all

cd /d c:\
md  aaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccddddddddddddddddddddddddddddfffffffffffffffffffggggggggggggggggggggggggggggggggggggggggggggggggggggggggggghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh




and the length of the directory path is greater than 256 you can not do, but using unc notation you can:

Code: Select all

md \\?\C:\aaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccddddddddddddddddddddddddddddfffffffffffffffffffggggggggggggggggggggggggggggggggggggggggggggggggggggggggggghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch to delete directory with path-too-long problem.

#10 Post by foxidrive » 02 Jun 2013 03:31

carlos wrote:The problem is not with the number of directorys. Is about the length of the path.
256 is the limit of the path.


The OP did say that there were 471 folders, each of three characters.

FWIW I think backslashes are counted in the path limit too.

Olyrd
Posts: 25
Joined: 05 Feb 2016 07:36

Re: Batch to delete directory with path-too-long problem.

#11 Post by Olyrd » 10 Apr 2016 12:42

Please accept my apologies for resurrecting this thread but I'd also like a sollution to this issue.

What we known conclusively is that a directory with a depth of more that 256 characters (slashes included) cannot be deleted by normal means. On the web you find people recommending 7-zip manager to delete, or using robocopy /mir command but sometimes these do not work, sometimes they do but is not guaranteed.

As now we have those hybrids that can run VBS and Powershell, and other languages as well. Isn't there something in VBS or Powershell that can be used to remove long paths?

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Batch to delete directory with path-too-long problem.

#12 Post by pieh-ejdsch » 18 Apr 2016 06:27

Can you give me that errormessage

Code: Select all

dir /s /b m:\ 1>nul


Phil

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch to delete directory with path-too-long problem.

#13 Post by penpen » 18 Apr 2016 19:06

You could also use the "subst" commmand, to shorten the involved pathes.


penpen

Randy_Vran
Posts: 1
Joined: 15 Mar 2020 23:43

Re: Batch to delete directory with path-too-long problem.

#14 Post by Randy_Vran » 15 Mar 2020 23:44

Long Path Tool will let you easily delete, copy or rename long path files.

HannahDaniel
Posts: 7
Joined: 20 Feb 2020 06:40

Re: Batch to delete directory with path-too-long problem.

#15 Post by HannahDaniel » 20 Mar 2020 07:28

Long path tools or super copying programs that support long path names will help you in your issue and as a solution out of the box for the path names, are they all in subdir's or spread out all over the place? You could actually do the above for these also, however, if they're all over the place, creating 100+ separate jobs would be counterproductive. If they are within the same tree structure, create a batch that starts further down into the subdir, then you can exclude that in the batch file that copies the rest of the structure, if that makes sense. The switch to exclude a folder is /XD dir (so, it would look like /XD \\server\share\sub1\sub2\sub3) which will definitely shorten your path names.

Post Reply