Find file paths longer than 260 characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Find file paths longer than 260 characters

#1 Post by drgt » 26 Oct 2016 14:39

I used: dir /s /b | sort /r /+261 /o out.txt

I expected to see the longest path on top.
However, I get about 830 lines with path longer than 260 and then there are more (longer than 260) listed starting on line 258214!

Why is that?

Also, Upon the file creation, I get about 50 warnings that "The directory name ... is too long"

Is my output a complete list?

Thanks
win xp sp2

edit: Can a directory or list of directories including their subdirectories be excluded from dir *.txt /s /on /b ?

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

Re: Find file paths longer than 260 characters

#2 Post by penpen » 27 Oct 2016 03:34

drgt wrote:I used: dir /s /b | sort /r /+261 /o out.txt

I expected to see the longest path on top.
However, I get about 830 lines with path longer than 260 and then there are more (longer than 260) listed starting on line 258214!

Why is that?
The List is sorted in reversed sort order for names longer than or equal to 261 characters. ('z' to 'a' instead of 'a' to 'z' for example).
In common this has nothing to do with the length of the filename String:
aaa
b
ccc

drgt wrote:Also, Upon the file creation, I get about 50 warnings that "The directory name ... is too long"

Is my output a complete list?
No, the directories which are too long are not listed.
In addition hidden files/directories are not listed, too.

drgt wrote:Can a directory or list of directories including their subdirectories be excluded from dir *.txt /s /on /b ?
No, you have to use another command/batch (for example the 'for'-command).


penpen

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Find file paths longer than 260 characters

#3 Post by drgt » 27 Oct 2016 05:52

Let me make it a bit clearer

In the out file, the first 7 lines are 265 characters long.
Lines 8-16 are 273 characters long.
Lines 17-24 --> 266
...
832-907 --> 74
...
258214 --> 261
...

I did not understand what you mean by
aaa
b
ccc

"for example the 'for'-command"

Could you please show?

Thanks

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

Re: Find file paths longer than 260 characters

#4 Post by penpen » 27 Oct 2016 07:09

drgt wrote:I did not understand what you mean by
aaa
b
ccc
This is a proof, that the content of a string is not bound to its length.
If you sort the above strings using "sort" or "sort /r", the result always is that "b" is sorted between "aaa" and "ccc".
The reason is the (natural) character order of the first character in each string:
'a' < 'b' < 'c'.

drgt wrote:Could you please show?
Example for listing all files "C:\Users\" and all its subdirectories, except all files/(sub)directories in "C:\Users\penpen" (untested - actually i'm posting from mobile phone):

Code: Select all

@echo off
for /r "C:\Users\" %%a in (.) do (
   echo %%a | >nul findstr "^C:\Users\penpen" || (
      pushd "%%~a"
      2>nul dir /A:-h-d /b
      2>nul dir /A:h-d /b
      popd
   )
)
goto :eof


penpen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Find file paths longer than 260 characters

#5 Post by Squashman » 27 Oct 2016 08:08

Penpen,
I think they are not understanding your example because they are sorting on position 260.

So using your example:

Code: Select all

H:\sort>dir /b |sort /R /+3
ccc
aaa
b


But I think they think the longest will always sort to the top but that is not the case if the position you are sorting on has a blank.

Code: Select all

H:\sort>dir /b |sort /R /+3
ccc
aaa
aa b
b

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

Re: Find file paths longer than 260 characters

#6 Post by pieh-ejdsch » 27 Oct 2016 09:52

Can you give the result of this codeline, please?

Code: Select all

dir /s /b 1>nul 2>lt &&<lt set /p "X="&del lt &cmd /v/s/c"cls&echo(!X!:"


Phil

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Find file paths longer than 260 characters

#7 Post by drgt » 27 Oct 2016 10:06

@penpen
Respectfully, please explain the code you wrote above. :oops:

@Squashman
Paths do not have blank spaces.

So how we get the longest to be listed first?

@pieh-ejdsch
Hey Phil, would you please explain the code.
(You guys are so advanced you could have me try del *.* and answer yes too!) xaxaxaxaxa

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

Re: Find file paths longer than 260 characters

#8 Post by pieh-ejdsch » 27 Oct 2016 10:17

this codeline will Output the first errormessage of Dir.

I want use this to find Paths longer than X and will become Driveletters Auto.

For german Output i use a Script.

also you can find Long pathes with Robocopy.

Phil

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

Re: Find file paths longer than 260 characters

#9 Post by penpen » 27 Oct 2016 10:42

Squashman wrote:Penpen,
I think they are not understanding your example because they are sorting on position 260.
Aha! Ok, it didn't cross my mind to use an example with offset, or to explain why the offset is irrelevant.

It's all the same to me (but that may not apply to others):
If there is an offset, then the lines are compaired as if they have that offset less characters at the beginning.

So i think i should extend my example up to an offset of n (the '?' char is placeholder for any single character):

Code: Select all

:: example strings to be sorted using an offset of 1 (== no offset)
aaa
b
ccc

:: example strings to be sorted using an offset 2
?aaa
?b
?ccc

:: example strings to be sorted using an offset 3
??aaa
??b
??ccc

:: example strings to be sorted using an offset n
??????????.... n-1 times.....????????????aaa
??????????.... n-1 times.....????????????b
??????????.... n-1 times.....????????????ccc
After sorting (no matter if default, or reversed order) the string ending on "b" always is sorted betweed the strings ending on "aaa" or "ccc" for the same reason, as for any other offsets:
The order of the first characters compared to each other is 'a' < 'b' < 'c'.


drgt wrote:please explain the code you wrote above.
Ok.
The "for /R" loops on the the path specified, and all its directories (in this case "C:\Users\" and all subdirs).
It also filters these directories by the set specified (in this case the '.', which simply means any).
Within each directorie i check if that path should be ignored (i selected the path "C:\Users\penpen" and all its subdirs to be ignored; the '^' character is a regular expression which means "line start"):
I perform the check by echoing the actual pathname via a pipe to findstr which checks, if the pathname starts with the path to be ignored;
if findstr finds the string specified, then it returns 0 else 1;
The "||" is a conditional execution statement, which only executes if the leading statement exited with != 0.
So within the inner compbound statement (or block), i change to the actual looped directory (using pushd),
list all non directories (== files) that are not hidden, after that all files that are hidden, and then
i return to the directory before changing to the actual one.

The loop should result in a complete list of files of "C:\Users\" and all its subdirectories without all files within "C:\Users\penpen" (still untested for the same rason).
(You probably have to change these parameters to match your requirements.)

Sidenote:
This only is an example implementation to demonstrate how to exclude subdirectories.
It does not solve the issue "The directory name ... is too long".
Depending on your directory structure it may be sufficient to use subst; extending my above example (assumed Z: is an unused volume on your system):

Code: Select all

@echo off
subst "Z:" "C:\Users"
for /r "Z:\" %%a in (.) do (
   echo %%a | >nul findstr "^Z:\penpen" || (
      pushd "%%~a"
      2>nul dir /A:-h-d /b
      2>nul dir /A:h-d /b
      popd
   )
)
subst /d "Z:"
goto :eof


penpen

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Find file paths longer than 260 characters

#10 Post by drgt » 27 Oct 2016 11:19

Ok. Thank you all for your input.

To sum it up
using
dir /a-d /on /s /b >c:\out.txt on the root of the drive

will make a file in which all files of the drive including hidden and system will be listed

AND

if at the command prompt window I get 50 "This directory ..... is too long", that means that I have ONLY THESE 50 directories whose length is longer than 260 characters.

CORRECT?

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

Re: Find file paths longer than 260 characters

#11 Post by penpen » 27 Oct 2016 12:44

Yes, both is correct, but depending on your system creating a file on your root of drive may be prohibited.

I'm unsure about the following things (i never tested it):
- It might be that the error message "The directory name ... is too long" is produced multiple times (for every file in a directory with >260 charcters in it).
- Also it might be that the directory listing in a directory with a too long subdir, the listing is aborted, so there might be more pathes than these 50 directories with too long path names in your case.
- Another thing might be, that this directory name too long message only contains a fixed size prefix of the complete pathname of that directory.
You have to check these things out.

You should notice that i seem to have overcomplicated things... with the for loop, as you could also exclude pathes by using findstr directly with the dir output:

Code: Select all

dir /a:-d /on /s /b | findstr /v "^C:\path\to\exclude" >"c:\some path\out.txt"

But i fear you have to use some kind of a for loop to collect those overlong pathnames.

Maybe you could filter the "overlong candidates" using something like this (and use a for /F loop on the resulting text file to get the path names; untested):

Code: Select all

2>&1 (>nul dir /a:-d /on /s /b) | findstr /v "^C:\path\to\exclude" >"c:\some path\out.txt"


penpen

Post Reply