Solved

Using Kill with a Variable VB6

Posted on 2004-08-26
9
353 Views
Last Modified: 2010-05-02
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



0
Comment
Question by:Jimmyx1000
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 11910197
Mypath = "D:\temp\temp\"
Kill Mypath & "*.txt"
<or>
Mypath = "D:\temp\temp\*.txt"
Kill Mypath
0
 
LVL 15

Accepted Solution

by:
Colosseo earned 250 total points
ID: 11910198
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
 
LVL 5

Expert Comment

by:KarcOrigin
ID: 11910199
Hi

Take Microsoft Scripting Runtime refrence and use

Call FileSys.DeleteFolder(DirName, True)

Thx
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11910205
lol...three posts at the same time...go to bed people!

G'night...

Idle_Mind
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:KarcOrigin
ID: 11910207
Hi,

Additional:

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

Cheers!

0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11910212
go to bed idle? its 7:22am --- time to get up for me :)  sadly lol

Scott
0
 
LVL 8

Expert Comment

by:plq
ID: 11910348
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
 
LVL 8

Expert Comment

by:plq
ID: 11910350
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
 
LVL 8

Expert Comment

by:plq
ID: 11911177
Its ok for you to use the code.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now