none
vbscript recursive directory search not finding long names ~250+

    Question

  • Problem: I can't list folders/file names that have paths being approximately 250 characters or longer. I know this exists and it offers a great solution (http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/888256be-4e72-4656-88ba-bcca7d7b29e0), but it is not an all inclusive solution. Here's how:

    I have a folder located here:

    C:\Windows\testing

    This part of the path is 18 characters

    I then have a folder inside of that called:

    this is a really long folder. In fact it is 244 characters long (I couldn't make it any longer and I had to map a drive to C-Windows-testing so I could name this so long)asdasdasjklhdljaskdfkjlhasdkjlfhklasjdhfkjlasdhfkjlhadskjlfhadskjlfhkjadsl

    In side of that folder there is a text document called myfile.txt

    This long folder name is 244 characters long. If you put the two folder lengths together and add the "\" then you have 263 character path. Then if you add the file name and an additional backslash you will have 274 characters long for the total file path.

    Well when I run the following code:

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    ScanDirectory(objFSO.GetFolder("C:\Windows\testing"))
    
    Sub ScanDirectory(TheFolder)
    	wscript.echo ""
    	wscript.echo "Scanning Folder: " & TheFolder
    	wscript.echo ""
    	ScanFiles TheFolder
    	For Each Folder in TheFolder.SubFolders : ScanDirectory Folder
    	Next
    End Sub
    
    Sub ScanFiles(Folder)
    	For Each File in Folder.Files : wscript.echo File.Path & vbtab & File.Size & " bytes"
    	Next
    End Sub
    
    wscript.echo "Done"

    I get this:

    C:\Windows\testing>cscript Testing.vbs
    Microsoft (R) Windows Script Host Version 5.8
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    
    Scanning Folder: C:\Windows\testing
    
    C:\Windows\testing\Testing.vbs  482 bytes
    
    Scanning Folder: C:\Windows\testing\New folder
    
    C:\Windows\testing\New folder\20 Q (2).docx     12103 bytes
    C:\Windows\testing\New folder\AE.xlsx   11892 bytes
    C:\Windows\testing\New folder\Both - Results.txt        3409226 bytes
    C:\Windows\testing\New folder\Both.txt  8581577 bytes
    C:\Windows\testing\New folder\Temp.docx 11498 bytes
    
    Scanning Folder: C:\Windows\testing\New folder\New folder - Copy
    
    C:\Windows\testing\New folder\New folder - Copy\20 Q (2).docx   12103 bytes
    C:\Windows\testing\New folder\New folder - Copy\AE.xlsx 11892 bytes
    C:\Windows\testing\New folder\New folder - Copy\Both - Results.txt      3409226 bytes
    C:\Windows\testing\New folder\New folder - Copy\Both.txt        8581577 bytes
    C:\Windows\testing\New folder\New folder - Copy\Temp.docx       11498 bytes
    Done
    
    C:\Windows\testing>

    There is no error message or any type of feedback; it acts as if the directory is nonexistent. I could map a temporary drive to C:\Windows\testing so it will scan. Once I mapped the drive I made this change in the code:

    From

    ScanDirectory(objFSO.GetFolder("C:\Windows\testing"))

    To

    ScanDirectory(objFSO.GetFolder("G:\"))

    This is what I get back:

    C:\Windows\testing>cscript Testing.vbs
    Microsoft (R) Windows Script Host Version 5.8
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    
    Scanning Folder: G:\
    
    G:\Testing.vbs  467 bytes
    
    Scanning Folder: G:\New folder
    
    G:\New folder\20 Q (2).docx     12103 bytes
    G:\New folder\AE.xlsx   11892 bytes
    G:\New folder\Both - Results.txt        3409226 bytes
    G:\New folder\Both.txt  8581577 bytes
    G:\New folder\Temp.docx 11498 bytes
    
    Scanning Folder: G:\New folder\New folder - Copy
    
    G:\New folder\New folder - Copy\20 Q (2).docx   12103 bytes
    G:\New folder\New folder - Copy\AE.xlsx 11892 bytes
    G:\New folder\New folder - Copy\Both - Results.txt      3409226 bytes
    G:\New folder\New folder - Copy\Both.txt        8581577 bytes
    G:\New folder\New folder - Copy\Temp.docx       11498 bytes
    
    Scanning Folder: G:\this is a really long folder. In fact it is 244 characters long (I couldn't make it any longer and I had to map a drive to C-Windows-testing so I could name this so long)asdasdasjklhdljaskdfkjlhasdkjlfhklasjdhfkjlasdhfkjlhadskjlfhadskjlfhkjadsl
    
    G:\this is a really long folder. In fact it is 244 characters long (I couldn't make it any longer and I had to map a drive to C-Windows-testing so I could name this so long)asdasdasjklhdljaskdfkjlhasdkjlfhklasjdhfkjlasdhfkjlhadskjlfhadskjlfhkjadsl\myfile.txt      1 bytes
    Done
    

    This lists the folder, the file, and the files size. Well that is great, but that means I would have to do this for every folder to see if there something that is not scan-able. I originally thought I could just check to see if the length of the folder was greater than 200, but my example above shows that the first part of the path can be small (as small as 3 characters long). The script I am writing this for is for something with millions of files and folders (massive file server with lots of file paths being too long due to Windows drive mapping allowing it to be named longer)

    Is there any way to detect when a folder is too long to normally be scanned? I have tried all kinds of error handling with no success. I would love to use the WMI method (http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/18/how-can-i-list-the-files-in-a-folder-and-all-its-subfolders.aspx) to see if it works, but the file server is not using Windows. So I have to do everything by \\servername\sharename



    Saturday, November 17, 2012 5:32 AM

Answers

  • You could wrap your script around robocopy, as robocopy does not have this limitation. Have a look at this (PowerShell) script that can be used to find long path names:

    http://gallery.technet.microsoft.com/Function-to-get-file-and-475aeb3a/view/Discussions#content

    robocopy.exe c:\pathyouwanttosearch c:\doesnotexist /l /e /b /np /fp /njh /njs



    Jaap Brasser
    http://www.jaapbrasser.com

    Monday, November 19, 2012 6:57 AM
    Moderator

All replies

  • The following batch file will identify folders with excessively long names. You could easily embed it in a VBScript file:

    @echo off
    dir /s /b /a-d "C:\Windows\Testing" 1> nul 2>"%temp%\log.txt"
    find  /i "too long" "%temp%\log.txt" > nul || goto :eof 
    echo The folder "%1" contains path names in excess of 260 characters.

    Saturday, November 17, 2012 12:14 PM
  • I knew about the dir command, but each scan takes a day or two so having to run the "too long path" script and then run the actual scan can take up to three days and that isn't very appealing. Is there not a vbscript approach?
    Sunday, November 18, 2012 12:26 AM
  • A VBScript approach to do what?

    How is any program (or script) going to find very long path names without scanning the entire directory structure?

    Bill

    Sunday, November 18, 2012 1:32 AM
    Moderator
  • I knew about the dir command, but each scan takes a day or two so having to run the "too long path" script and then run the actual scan can take up to three days and that isn't very appealing. Is there not a vbscript approach?

    Why didn't you say that you knew about the "dir" command?

    The "dir" command is by far the fastest method to enumerate a folder tree. Nothing will beat it. If it takes too long for you then you need to specify in greater detail how you wish to restrict your search for an excessively long path. So far you only said "I can't list folders/file names that have paths being approximately 250 characters or longer. I know this exists.". It exists where?

    Sunday, November 18, 2012 7:19 AM
  • I had a script that would run the dir command based off of the search syntax. But I was unable to get the file size, last modified date, and created date using the dir command.

    So I took a vbscript approach that recursively finds all files and folders. But when I was testing out the script I noticed that the TheFolder.SubFolders code does not collect the folders with path names exceeding something around 250 plus characters.

    The Folder.Files code also does not collect files that has a path that exceeds something around 250 plus characters.

    In my example above I showed that even though the folder exists it will not be picked up using the vbscript code. I had to map a drive to be able to pick it up


    Monday, November 19, 2012 12:55 AM
  • Sorry, I should of been more specific. When I said "I know this exists and it offers a great solution (http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/888256be-4e72-4656-88ba-bcca7d7b29e0), but it is not an all inclusive solution." I was referring to the solution available on the other thread for the same issue I am having. My main issue is not being able to "collect" files and folders into the object array when I run these commands:

    TheFolder.SubFolders

    Folder.Files 

    These commands act like the folder or file is non-existent even thought it clearly is there. There is no error message, flag, feedback, nothing. So I was wondering if there was a vbscript solution to find these.


    Monday, November 19, 2012 1:03 AM
  • You could wrap your script around robocopy, as robocopy does not have this limitation. Have a look at this (PowerShell) script that can be used to find long path names:

    http://gallery.technet.microsoft.com/Function-to-get-file-and-475aeb3a/view/Discussions#content

    robocopy.exe c:\pathyouwanttosearch c:\doesnotexist /l /e /b /np /fp /njh /njs



    Jaap Brasser
    http://www.jaapbrasser.com

    Monday, November 19, 2012 6:57 AM
    Moderator