?
Solved

How to delete files with long file names

Posted on 2006-05-24
12
Medium Priority
?
4,906 Views
Last Modified: 2012-05-05

I'm using the FileSystemObject in VB6 and am having problems deleting lots of folders, some of which contain files with very long names.

e.g.

Just for illustration, assume I have (not my actual code, but this shows the problem):

Dim sFolder as string
Dim sFile as string

sFolder = "\\server\share\folder\"
sFile = sFolder & "XXX.txt"

(where XXX is a very long series of characters such that len(sFile) > 256)

Then:

Dim oFS as new FileSystemObject
oFS.DeleteFolder(sFolder)

This causes a "Path not Found" error.  I can understand why - although the filename sFile is not used directly by my code, FSO clearly tries to delete each file in the folder in turn before deleting the folder.  Note that oFS.FileExists(sFile) give the answer FALSE, which is clearly wrong!!!

Anyway, I have a lot of these folders, but only some have the issue.  How can I elegantly delete folders with this problem?

Thanks!

Jim
0
Comment
Question by:JimbobW
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 16751868
can u try defining sFile like this

dim sFile as String * 1000

or some size sensible :)


then u do

sFile = sFolder & "xxx.txt"

to remove the nulls, call trimnull

i.e.

Private Function TrimNull(sStr As String) As String
 
    Dim iPos As Integer
   
    iPos = InStr(sStr, Chr$(0))
    If iPos Then
        TrimNull = Left$(sStr, iPos - 1)
    Else
        TrimNull = sStr
    End If
End Function


sFile = TrimNull(sFile)


Now see if that works
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16751878
the alternative is to use

dir(sFile)

this returns "" if not there else xxx.txt if it exists

and if the filesystem scripting delete dont work, try the old method - kill
0
 

Author Comment

by:JimbobW
ID: 16751915

Sorry - no - didn't explain myself properly.  In my code above I've only included sFile just to easily talk about the problem and demonstrate/explain what is going on.

My actual code only has the sFolder and DeleteFolder(sFolder) stuff in it.  I'm not interested in the files contained in the folder - I just want to delete the folder.  Indeed the folder I want to delete may have other folders/subfolders/files in it, and I don't want to spend time traversing this structure to find potential problems.  I just want to delete sFolder in one easy hit!!!!

Thanks,

Jim
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:JimbobW
ID: 16751974

BTW ...

rmdir(sFolder)
Kill(sFolder)

... also both fail with an error!

0
 
LVL 3

Expert Comment

by:fullcontact
ID: 16752020
Tried  rmdir /S /Q
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16752057
ok,

does the scripting object give u the short name version i.e. 8.3?

what if you surrounded the long filename in double quotes, would that work? You really should do that if it contains spaces




0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16752071
how to get the short name

http://www.freevbcode.com/ShowCode.asp?ID=3212


I just wonder whether something like this will work

Dim oFS as new FileSystemObject
oFS.DeleteFolder(chr(34) & sFolder & chr(34))

0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 2000 total points
ID: 16753199
Your will need to use some API.


Option Explicit

Private Const FO_DELETE             As Long = &H3
Private Const FOF_SILENT            As Long = &H4
Private Const FOF_NOCONFIRMATION    As Long = &H10

Private Type SHFILEOPSTRUCT
   hWnd        As Long
   wFunc       As Long
   pFrom       As String
   pTo         As String
   fFlags      As Integer
   fAborted    As Boolean
   hNameMaps   As Long
   sProgress   As String
 End Type

Private Declare Function SHFileOperationA Lib "shell32" ( _
    SHT As SHFILEOPSTRUCT) As Long

Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" ( _
    Destination As Any, _
    ByVal Length As Long)
       
Public Sub RemoveDirectory( _
    ByVal RemoveDir As String)
   
    Dim SHT As SHFILEOPSTRUCT
   
    ZeroMemory SHT, Len(SHT)
   
    SHT.hWnd = 0
    SHT.wFunc = FO_DELETE
    SHT.pTo = 0
    SHT.pFrom = RemoveDir
    SHT.fFlags = FOF_SILENT Or FOF_NOCONFIRMATION
   
    SHFileOperationA SHT


End Sub


Private Sub Command1_Click()
RemoveDirectory "D:\test"
End Sub
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 16755240
You can try the unicode version of DeleteFile() API (DeleteFileW) which supports file-lengths above MAX_PATH (256).

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/deletefile.asp

Form1:
----------------
Option Explicit

Private Declare Function DeleteFileW Lib "kernel32" (ByVal lpFilename As Long) As Long
Private Sub Form_Load()
    '
End Sub
Private Sub Command1_Click()
    Dim file As String
    file = "\\?\c:\test.txt"
    Call DeleteFileW(StrPtr(file))
End Sub
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 16758603
Hi Jim,

"BTW ...

rmdir(sFolder)
Kill(sFolder)

... also both fail with an error!"


Are the files in question locked by the operating system or some other process?  Are they open in another application?  If you log-off, or re-boot, and then re-try your code is there any improvement?

(This utility may also help: [ http://ccollomb.free.fr/unlocker/ ] - "Unlocker" {1.8.1} by Cedrcik 'Nitch' Collomb)


What are error messages you receive?

BFN,

fp.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 16758616
...or
<snagglepuss>
"Cedrick", even
</snagglepuss>

:)
0
 

Author Comment

by:JimbobW
ID: 16758949

fanpages:  Files were definitely not locked
zzzzzooc:  I'm trying to delete the directory - I don't know what files are in it
rockiroads:  The folder paths are quite short anyway (it is the files inside that cause the problem) so something like GetShortPath(sFolder) won't help

egl1044:  Worked perfectly - thanks - points to you!

Thanks all for your help / ideas!!!!  It's a great website this for inexperienced programmers like myself!!!!

Jim


0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question