Page 1 of 2
Batch to delete directory with path-too-long problem.
Posted: 30 May 2013 03:30
by psychoid69
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.
Re: Batch to delete directory with path-too-long problem.
Posted: 30 May 2013 03:57
by foxidrive
Someone has to reproduce a path that is too long, to test it.
I pass. Too lazy.
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
Re: Batch to delete directory with path-too-long problem.
Posted: 30 May 2013 04:28
by psychoid69
Oh, the problem (the loop) was created using File Server Migration Wizard.
Re: Batch to delete directory with path-too-long problem.
Posted: 30 May 2013 14:03
by Ricktor
thanks for the info.,you could also try long path tool. it helped me with error 1320 in Win 7.,
Re: Batch to delete directory with path-too-long problem.
Posted: 31 May 2013 00:02
by psychoid69
Hm, yeah... LongPathTool is not free... I know we can make batch file that will be free and open...
Re: Batch to delete directory with path-too-long problem.
Posted: 31 May 2013 01:05
by Endoro
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.
Re: Batch to delete directory with path-too-long problem.
Posted: 31 May 2013 02:31
by psychoid69
Endoro, thank you for your valuable input!
Just to clarify - file manager that succeeded with deletion was SpeedCommander?
Re: Batch to delete directory with path-too-long problem.
Posted: 31 May 2013 06:30
by Endoro
yes, of course. you can have a fully functioning
test version to remove the annoying path.
Re: Batch to delete directory with path-too-long problem.
Posted: 01 Jun 2013 14:07
by carlos
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
Re: Batch to delete directory with path-too-long problem.
Posted: 02 Jun 2013 03:31
by foxidrive
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.
Re: Batch to delete directory with path-too-long problem.
Posted: 10 Apr 2016 12:42
by Olyrd
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?
Re: Batch to delete directory with path-too-long problem.
Posted: 18 Apr 2016 06:27
by pieh-ejdsch
Can you give me that errormessage
Phil
Re: Batch to delete directory with path-too-long problem.
Posted: 18 Apr 2016 19:06
by penpen
You could also use the "subst" commmand, to shorten the involved pathes.
penpen
Re: Batch to delete directory with path-too-long problem.
Posted: 15 Mar 2020 23:44
by Randy_Vran
Long Path Tool will let you easily delete, copy or rename long path files.
Re: Batch to delete directory with path-too-long problem.
Posted: 20 Mar 2020 07:28
by HannahDaniel
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.