Solved

Using Kill with a Variable VB6

Posted on 2004-08-26
9
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 86

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

697 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