Solved

How to kill/delete files in a folder

Posted on 2009-04-08
6
553 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

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

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

747 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

11 Experts available now in Live!

Get 1:1 Help Now