Solved

Using Kill with a Variable VB6

Posted on 2004-08-26
9
357 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

776 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