Using Kill with a Variable VB6

Hello id like to del all files in a directory

Mypath = "D:\temp\temp"

how can i use the kill command with the path as a variable

eg

Kill Mypath *.*          ' why wont this work



Jimmyx1000Asked:
Who is Participating?
 
ColosseoConnect With a Mentor Commented:
Hi jimmy

you need to pass the folder as a string

kill Mypath *.* will not be seen as a proper folder

in your case where you have

Mypath = "D:\temp\temp"  <--- no trailing backslash

you would need to use

kill Mypath & "\*.*"

HTH

Scott
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Mypath = "D:\temp\temp\"
Kill Mypath & "*.txt"
<or>
Mypath = "D:\temp\temp\*.txt"
Kill Mypath
0
 
KarcOriginCommented:
Hi

Take Microsoft Scripting Runtime refrence and use

Call FileSys.DeleteFolder(DirName, True)

Thx
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
lol...three posts at the same time...go to bed people!

G'night...

Idle_Mind
0
 
KarcOriginCommented:
Hi,

Additional:

Kill command deletes the empty folders only. Use FileSystemObject by  taking Microsoft Scripting Runtime refrence.

Cheers!

0
 
ColosseoCommented:
go to bed idle? its 7:22am --- time to get up for me :)  sadly lol

Scott
0
 
plqCommented:
Here's a set of functions for file handling, including "deletefolder" which will obviously work on "*.*". Thought you might like the whole lot... great for deployments and writing utilities.

Public Function DeleteFile(sFile As String) As Boolean
   
    Dim scr As Object
   
    Set scr = CreateObject("Scripting.FileSystemObject")
    scr.DeleteFile sFile, True
    Set scr = Nothing
   
    If FileExists(sFile) Then
        DeleteFile = False
    Else
        DeleteFile = True
    End If
   
End Function

Public Function DeleteFolder(sFile As String) As Boolean
   
    Dim scr As Object
   
    Set scr = CreateObject("Scripting.FileSystemObject")
    scr.DeleteFolder sFile, True
    Set scr = Nothing
   
End Function



Public Function CopyFile(sSource As String, sTarget As String)
   
    Dim scr  As Object
    Dim fil1 As Object
    Dim fil2 As Object
   
    'SetContext "Copying " & sSource & " " & sTarget
   
    If Not FileExists(sSource) Then
        Err.Raise 32001, , "Cannot copy " & sSource & " because the file does not exist"
    End If
   
    Set scr = CreateObject("Scripting.FileSystemObject")
    If scr.FileExists(sTarget) Then
        Set fil1 = scr.GetFile(sTarget)
        Set fil2 = scr.GetFile(sSource)
        If fil1.DateLastModified = fil2.DateLastModified And fil1.Size = fil2.Size Then
            ' Do nothing
        Else
            scr.DeleteFile fil1.Path, True
            scr.CopyFile sSource, sTarget, True
        End If
    Else
        scr.CopyFile sSource, sTarget, True
    End If
   
    If Right(sTarget, 1) <> "\" Then
        On Error Resume Next
        Set fil1 = scr.GetFile(sTarget)
        If Not fil1 Is Nothing Then
            If scr.FileExists(fil1.Path) Then
                fil1.Attributes = 0
            End If
        End If
    End If
   
    Set scr = Nothing
   
End Function

Public Function CopyTree(sSource As String, sTarget As String, Optional bDeep As Boolean = True)
   
    Dim scr     As Object
    Dim fldRoot As Object
    Dim fld     As Object
    Dim fil     As Object
   
    'SetContext "CopyTree " & sSource & " " & sTarget
   
    Set scr = CreateObject("Scripting.FileSystemObject")
   
    Set fldRoot = scr.GetFolder(sSource)
    If bDeep Then
        For Each fld In fldRoot.SubFolders
            CreateDirectory sTarget & "\" & fld.Name
            CopyTree sSource & "\" & fld.Name, sTarget & "\" & fld.Name, bDeep
        Next
    End If
   
    If Not FolderExists(sTarget) Then
        CreateDirectory sTarget
    End If
   
   
    For Each fil In fldRoot.Files
        CopyFile fil.Path, sTarget & "\" & fil.Name
    Next
   
End Function

Public Function CreateDirectory(sDirectory As String)
   
    Dim scr As Object
    Dim sPrepath    As String
   
    'SetContext "Create Directory " & sDirectory
   
    If Not FolderExists(sDirectory) Then
        sPrepath = GetFilePath(sDirectory)
        If Not FolderExists(sPrepath) Then
            CreateDirectory sPrepath        ' recursive upwards
        End If
        Set scr = CreateObject("Scripting.FileSystemObject")
        scr.CreateFolder sDirectory
        Set scr = Nothing
   
    End If
   
   
End Function

Public Sub WriteTextFile(sName As String, sContent As String, Optional bUnicode As Boolean = True)
   
    '   ReplacePoint
    Dim scr As Object
    Dim txt As Object
   
    Set scr = CreateObject("Scripting.FileSystemObject")
    Set txt = scr.CreateTextFile(sName, True, bUnicode)
    txt.Write sContent
    txt.Close
    Set txt = Nothing
    Set scr = Nothing
   
End Sub

0
 
plqCommented:
Public Function FileExists(sFileName As String) As Boolean

    '   Returns true if a file exists, or false if it does not
   
    Dim scr As Object
   
    Set scr = CreateObject("Scripting.FileSystemObject")
    FileExists = scr.FileExists(sFileName)
    Set scr = Nothing
   
End Function
0
 
plqCommented:
Its ok for you to use the code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.