Solved

How to kill/delete files in a folder

Posted on 2009-04-08
6
557 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
[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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

738 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