Solved

How to kill/delete files in a folder

Posted on 2009-04-08
6
555 Views
Last Modified: 2013-11-27
I am working in Access 2002 and I am trying to kill/delete files (if any) in my windows xp explorer folder.  I wrote the following constant to identify the path.  However when it gets to the if statement A_ImportDir_MIC actually equals the path so it drops into my if statement to kill the files even though there are no files in the folder Micros email attachments.  

How can I rewrite the script to make sure its looking for the files within the folder with the *.txt extensions and if there are none get out of the if statement?  Thank you.


<CODE>
Option Compare Database   'Use database order for string comparisons
Const A_ImportDir_MIC = "C:\Micros email attachments\"

'KILL THE FILES
      If A_ImportDir_MIC <> "" Then
        Kill "C:\Micros email attachments\*.txt"
      End If
0
Comment
Question by:Mahonek
  • 4
6 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 24100888
you could try looping using dir

dim sFolder as string
dim sFile as string

    sFolder = "c:\myfolder\"
    sFile = Dir$(sFolder)
    Do While Len(sFile) > 0
         kill sFolder & sFile
         sFile= Dir$
    Loop

other way is to use the file system object
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 125 total points
ID: 24100911
   Dim fso As Object
    Dim sourceFolder As Object
    Dim f As Object
    Dim sFolder As String
   
   
    Set fso = CreateObject("Scripting.FileSystemObject")
   
    sFolder = "c:\myfolder\"
    Set sourceFolder = fso.GetFolder(sFolder)
    For Each f In sourceFolder.Files
        Kill sFolder & f.Name
    Next f
0
 
LVL 44

Expert Comment

by:GRayL
ID: 24101015
If Dir(A_importDir_MIC & "*.txt") <> "" Then
  Kill A_importDir_MIC & "*.txt"
End If
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 65

Expert Comment

by:rockiroads
ID: 24101040
scripts I did deletes all files. Using the orignal dir method, you can add in *.txt to bring back text files only

a better way to use scripting is to create the file object then use the delete method instead of kill


0
 

Author Closing Comment

by:Mahonek
ID: 31568205
Great!  Thanks.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24101939
No worries, Sorry but I forgot to check for the txt instructions but that can be done by some simple string manipulation
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

773 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