PC Review


Reply
Thread Tools Rate Thread

Check to see if files exists and delete it.

 
 
Steven M. Britton
Guest
Posts: n/a
 
      16th Jun 2004
How can I check to see if there are files in a directory,
and if the directory contains files delete all of them?

-Steve
 
Reply With Quote
 
 
 
 
Wayne Morgan
Guest
Posts: n/a
 
      16th Jun 2004
You can use the Shell command to do this from a Dos window. You can also use
the VBA Dir, Kill, ChDir, RmDir, and SetAttr commands.

To simply delete all files in a folder use

Kill <path>\*.*

However, this won't remove subdirectories, open files, or files set as
Hidden, System, or Read Only. To do that, you'll have to use the other
commands. To remove subdirectories, you have to run through the directory
tree recursively. I tend to find it easiest just to put Deltree (from Dos 6,
Win9x) in the path or somewhere that I know where it is at and Shell to a
command prompt calling Deltree. You would call Command.com or Cmd.exe with
the /C switch.

--
Wayne Morgan
Microsoft Access MVP


"Steven M. Britton" <(E-Mail Removed)> wrote in message
news:1d0b601c453bf$0c4c4340$(E-Mail Removed)...
> How can I check to see if there are files in a directory,
> and if the directory contains files delete all of them?
>
> -Steve



 
Reply With Quote
 
Steven M. Britton
Guest
Posts: n/a
 
      16th Jun 2004
Wayne:

I only want to do this in VBA, I use the kill command
however if there are no files to delete it tells me "File
not found" and exits the sub.

I guess I should further explain my problem. I have
Access exporting .txt files via a TransferText and a
query, once I export he .txt files I use Name so that I
can rename them as .CSV files for other various uses. A
problem arises when I try to rename the file and it
already exist, because the user forgot to erase the
previous file.

Any Ideas?

-Steve
>-----Original Message-----
>You can use the Shell command to do this from a Dos

window. You can also use
>the VBA Dir, Kill, ChDir, RmDir, and SetAttr commands.
>
>To simply delete all files in a folder use
>
>Kill <path>\*.*
>
>However, this won't remove subdirectories, open files, or

files set as
>Hidden, System, or Read Only. To do that, you'll have to

use the other
>commands. To remove subdirectories, you have to run

through the directory
>tree recursively. I tend to find it easiest just to put

Deltree (from Dos 6,
>Win9x) in the path or somewhere that I know where it is

at and Shell to a
>command prompt calling Deltree. You would call

Command.com or Cmd.exe with
>the /C switch.
>
>--
>Wayne Morgan
>Microsoft Access MVP
>
>
>"Steven M. Britton" <(E-Mail Removed)> wrote in message
>news:1d0b601c453bf$0c4c4340$(E-Mail Removed)...
>> How can I check to see if there are files in a

directory,
>> and if the directory contains files delete all of them?
>>
>> -Steve

>
>
>.
>

 
Reply With Quote
 
Paul Overway
Guest
Posts: n/a
 
      16th Jun 2004
On Error resume next

'If the file doesn't exist...ignore the error
Kill "C:\somefile.txt"

On Error Goto err_Handler
'Resume error handling


--
Paul Overway
Logico Solutions, LLC
www.logico-solutions.com


"Steven M. Britton" <(E-Mail Removed)> wrote in message
news:1d6d801c453e4$18d83d10$(E-Mail Removed)...
> Wayne:
>
> I only want to do this in VBA, I use the kill command
> however if there are no files to delete it tells me "File
> not found" and exits the sub.
>
> I guess I should further explain my problem. I have
> Access exporting .txt files via a TransferText and a
> query, once I export he .txt files I use Name so that I
> can rename them as .CSV files for other various uses. A
> problem arises when I try to rename the file and it
> already exist, because the user forgot to erase the
> previous file.
>
> Any Ideas?
>
> -Steve
> >-----Original Message-----
> >You can use the Shell command to do this from a Dos

> window. You can also use
> >the VBA Dir, Kill, ChDir, RmDir, and SetAttr commands.
> >
> >To simply delete all files in a folder use
> >
> >Kill <path>\*.*
> >
> >However, this won't remove subdirectories, open files, or

> files set as
> >Hidden, System, or Read Only. To do that, you'll have to

> use the other
> >commands. To remove subdirectories, you have to run

> through the directory
> >tree recursively. I tend to find it easiest just to put

> Deltree (from Dos 6,
> >Win9x) in the path or somewhere that I know where it is

> at and Shell to a
> >command prompt calling Deltree. You would call

> Command.com or Cmd.exe with
> >the /C switch.
> >
> >--
> >Wayne Morgan
> >Microsoft Access MVP
> >
> >
> >"Steven M. Britton" <(E-Mail Removed)> wrote in message
> >news:1d0b601c453bf$0c4c4340$(E-Mail Removed)...
> >> How can I check to see if there are files in a

> directory,
> >> and if the directory contains files delete all of them?
> >>
> >> -Steve

> >
> >
> >.
> >



 
Reply With Quote
 
Alex Ivanov
Guest
Posts: n/a
 
      16th Jun 2004
As Wayne noted you can use the dir function:
if dir("c:\path\")>"" then kill "c:\path\*.*"

--
Please reply to NG only. The email address is not monitored.

Alex.

"Paul Overway" <(E-Mail Removed)> wrote in message
news:OoZfqU%(E-Mail Removed)...
> On Error resume next
>
> 'If the file doesn't exist...ignore the error
> Kill "C:\somefile.txt"
>
> On Error Goto err_Handler
> 'Resume error handling
>
>
> --
> Paul Overway
> Logico Solutions, LLC
> www.logico-solutions.com
>
>
> "Steven M. Britton" <(E-Mail Removed)> wrote in message
> news:1d6d801c453e4$18d83d10$(E-Mail Removed)...
> > Wayne:
> >
> > I only want to do this in VBA, I use the kill command
> > however if there are no files to delete it tells me "File
> > not found" and exits the sub.
> >
> > I guess I should further explain my problem. I have
> > Access exporting .txt files via a TransferText and a
> > query, once I export he .txt files I use Name so that I
> > can rename them as .CSV files for other various uses. A
> > problem arises when I try to rename the file and it
> > already exist, because the user forgot to erase the
> > previous file.
> >
> > Any Ideas?
> >
> > -Steve
> > >-----Original Message-----
> > >You can use the Shell command to do this from a Dos

> > window. You can also use
> > >the VBA Dir, Kill, ChDir, RmDir, and SetAttr commands.
> > >
> > >To simply delete all files in a folder use
> > >
> > >Kill <path>\*.*
> > >
> > >However, this won't remove subdirectories, open files, or

> > files set as
> > >Hidden, System, or Read Only. To do that, you'll have to

> > use the other
> > >commands. To remove subdirectories, you have to run

> > through the directory
> > >tree recursively. I tend to find it easiest just to put

> > Deltree (from Dos 6,
> > >Win9x) in the path or somewhere that I know where it is

> > at and Shell to a
> > >command prompt calling Deltree. You would call

> > Command.com or Cmd.exe with
> > >the /C switch.
> > >
> > >--
> > >Wayne Morgan
> > >Microsoft Access MVP
> > >
> > >
> > >"Steven M. Britton" <(E-Mail Removed)> wrote in message
> > >news:1d0b601c453bf$0c4c4340$(E-Mail Removed)...
> > >> How can I check to see if there are files in a

> > directory,
> > >> and if the directory contains files delete all of them?
> > >>
> > >> -Steve
> > >
> > >
> > >.
> > >

>
>



 
Reply With Quote
 
Steven M. Britton
Guest
Posts: n/a
 
      16th Jun 2004
What would I use to see if a specific file is in the
directory?

-Steve
>-----Original Message-----
>As Wayne noted you can use the dir function:
>if dir("c:\path\")>"" then kill "c:\path\*.*"
>
>--
>Please reply to NG only. The email address is not

monitored.
>
>Alex.
>
>"Paul Overway" <(E-Mail Removed)>

wrote in message
>news:OoZfqU%(E-Mail Removed)...
>> On Error resume next
>>
>> 'If the file doesn't exist...ignore the error
>> Kill "C:\somefile.txt"
>>
>> On Error Goto err_Handler
>> 'Resume error handling
>>
>>
>> --
>> Paul Overway
>> Logico Solutions, LLC
>> www.logico-solutions.com
>>
>>
>> "Steven M. Britton"

<(E-Mail Removed)> wrote in message
>> news:1d6d801c453e4$18d83d10$(E-Mail Removed)...
>> > Wayne:
>> >
>> > I only want to do this in VBA, I use the kill command
>> > however if there are no files to delete it tells

me "File
>> > not found" and exits the sub.
>> >
>> > I guess I should further explain my problem. I have
>> > Access exporting .txt files via a TransferText and a
>> > query, once I export he .txt files I use Name so that

I
>> > can rename them as .CSV files for other various

uses. A
>> > problem arises when I try to rename the file and it
>> > already exist, because the user forgot to erase the
>> > previous file.
>> >
>> > Any Ideas?
>> >
>> > -Steve
>> > >-----Original Message-----
>> > >You can use the Shell command to do this from a Dos
>> > window. You can also use
>> > >the VBA Dir, Kill, ChDir, RmDir, and SetAttr

commands.
>> > >
>> > >To simply delete all files in a folder use
>> > >
>> > >Kill <path>\*.*
>> > >
>> > >However, this won't remove subdirectories, open

files, or
>> > files set as
>> > >Hidden, System, or Read Only. To do that, you'll

have to
>> > use the other
>> > >commands. To remove subdirectories, you have to run
>> > through the directory
>> > >tree recursively. I tend to find it easiest just to

put
>> > Deltree (from Dos 6,
>> > >Win9x) in the path or somewhere that I know where it

is
>> > at and Shell to a
>> > >command prompt calling Deltree. You would call
>> > Command.com or Cmd.exe with
>> > >the /C switch.
>> > >
>> > >--
>> > >Wayne Morgan
>> > >Microsoft Access MVP
>> > >
>> > >
>> > >"Steven M. Britton" <(E-Mail Removed)> wrote in

message
>> > >news:1d0b601c453bf$0c4c4340$(E-Mail Removed)...
>> > >> How can I check to see if there are files in a
>> > directory,
>> > >> and if the directory contains files delete all of

them?
>> > >>
>> > >> -Steve
>> > >
>> > >
>> > >.
>> > >

>>
>>

>
>
>.
>

 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      17th Jun 2004
If Len(Dir("C:\path\filename.ext") > 0 Then
' File filename.ext exists in folder C:\path
Else
' File filename.ext does not exist in folder C;\path
End If

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Steven M. Britton" <(E-Mail Removed)> wrote in message
news:1d3de01c453f1$69d113b0$(E-Mail Removed)...
> What would I use to see if a specific file is in the
> directory?
>
> -Steve
> >-----Original Message-----
> >As Wayne noted you can use the dir function:
> >if dir("c:\path\")>"" then kill "c:\path\*.*"
> >
> >--
> >Please reply to NG only. The email address is not

> monitored.
> >
> >Alex.
> >
> >"Paul Overway" <(E-Mail Removed)>

> wrote in message
> >news:OoZfqU%(E-Mail Removed)...
> >> On Error resume next
> >>
> >> 'If the file doesn't exist...ignore the error
> >> Kill "C:\somefile.txt"
> >>
> >> On Error Goto err_Handler
> >> 'Resume error handling
> >>
> >>
> >> --
> >> Paul Overway
> >> Logico Solutions, LLC
> >> www.logico-solutions.com
> >>
> >>
> >> "Steven M. Britton"

> <(E-Mail Removed)> wrote in message
> >> news:1d6d801c453e4$18d83d10$(E-Mail Removed)...
> >> > Wayne:
> >> >
> >> > I only want to do this in VBA, I use the kill command
> >> > however if there are no files to delete it tells

> me "File
> >> > not found" and exits the sub.
> >> >
> >> > I guess I should further explain my problem. I have
> >> > Access exporting .txt files via a TransferText and a
> >> > query, once I export he .txt files I use Name so that

> I
> >> > can rename them as .CSV files for other various

> uses. A
> >> > problem arises when I try to rename the file and it
> >> > already exist, because the user forgot to erase the
> >> > previous file.
> >> >
> >> > Any Ideas?
> >> >
> >> > -Steve
> >> > >-----Original Message-----
> >> > >You can use the Shell command to do this from a Dos
> >> > window. You can also use
> >> > >the VBA Dir, Kill, ChDir, RmDir, and SetAttr

> commands.
> >> > >
> >> > >To simply delete all files in a folder use
> >> > >
> >> > >Kill <path>\*.*
> >> > >
> >> > >However, this won't remove subdirectories, open

> files, or
> >> > files set as
> >> > >Hidden, System, or Read Only. To do that, you'll

> have to
> >> > use the other
> >> > >commands. To remove subdirectories, you have to run
> >> > through the directory
> >> > >tree recursively. I tend to find it easiest just to

> put
> >> > Deltree (from Dos 6,
> >> > >Win9x) in the path or somewhere that I know where it

> is
> >> > at and Shell to a
> >> > >command prompt calling Deltree. You would call
> >> > Command.com or Cmd.exe with
> >> > >the /C switch.
> >> > >
> >> > >--
> >> > >Wayne Morgan
> >> > >Microsoft Access MVP
> >> > >
> >> > >
> >> > >"Steven M. Britton" <(E-Mail Removed)> wrote in

> message
> >> > >news:1d0b601c453bf$0c4c4340$(E-Mail Removed)...
> >> > >> How can I check to see if there are files in a
> >> > directory,
> >> > >> and if the directory contains files delete all of

> them?
> >> > >>
> >> > >> -Steve
> >> > >
> >> > >
> >> > >.
> >> > >
> >>
> >>

> >
> >
> >.
> >



 
Reply With Quote
 
Wayne Morgan
Guest
Posts: n/a
 
      17th Jun 2004
There are several ways around the error as you've seen mentioned here. The
error number is 53 if you want to trap it in an error handler or you could
just place an On Error Resume Next before the statement. Remember to
reinitiate your normal error handling after the statement if you do this.
You could also use Dir to see if at least one file meeting the criteria
exists first.

--
Wayne Morgan
Microsoft Access MVP


"Steven M. Britton" <(E-Mail Removed)> wrote in message
news:1d6d801c453e4$18d83d10$(E-Mail Removed)...
> Wayne:
>
> I only want to do this in VBA, I use the kill command
> however if there are no files to delete it tells me "File
> not found" and exits the sub.
>
> I guess I should further explain my problem. I have
> Access exporting .txt files via a TransferText and a
> query, once I export he .txt files I use Name so that I
> can rename them as .CSV files for other various uses. A
> problem arises when I try to rename the file and it
> already exist, because the user forgot to erase the
> previous file.
>
> Any Ideas?
>
> -Steve
> >-----Original Message-----
> >You can use the Shell command to do this from a Dos

> window. You can also use
> >the VBA Dir, Kill, ChDir, RmDir, and SetAttr commands.
> >
> >To simply delete all files in a folder use
> >
> >Kill <path>\*.*
> >
> >However, this won't remove subdirectories, open files, or

> files set as
> >Hidden, System, or Read Only. To do that, you'll have to

> use the other
> >commands. To remove subdirectories, you have to run

> through the directory
> >tree recursively. I tend to find it easiest just to put

> Deltree (from Dos 6,
> >Win9x) in the path or somewhere that I know where it is

> at and Shell to a
> >command prompt calling Deltree. You would call

> Command.com or Cmd.exe with
> >the /C switch.
> >
> >--
> >Wayne Morgan
> >Microsoft Access MVP
> >
> >
> >"Steven M. Britton" <(E-Mail Removed)> wrote in message
> >news:1d0b601c453bf$0c4c4340$(E-Mail Removed)...
> >> How can I check to see if there are files in a

> directory,
> >> and if the directory contains files delete all of them?
> >>
> >> -Steve

> >
> >
> >.
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check whether URL exists Greg Lovern Microsoft Excel Programming 1 15th Mar 2011 11:13 PM
How to check if a query exists and if it finds to delete Jack Microsoft Access VBA Modules 3 17th Sep 2009 07:34 PM
Check for a shape - then change or delete it if it exists =?Utf-8?B?TW9uYS1BQkU=?= Microsoft Excel Programming 2 30th May 2007 05:37 PM
Can you check if an object exists, and only it it does, delete it =?Utf-8?B?Q3JhaWc=?= Microsoft Access Macros 2 19th Dec 2003 01:35 PM
Check to see if Doc exists Ryan Microsoft Access Form Coding 7 14th Nov 2003 10:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:42 AM.