473,418 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Kill all text files except those starting with ~$

I am trying to write some code that when I exit my application kills all of
the text files in a certain folder. It is possible that some of those files
are locked because someone else is using them, in which case they will have
the first two letters of their name replaced with ~$. How can I write a
routine that will delete all the normal *.txt files, but not attempt to
delete the ~$ ones, which brings up an error message and prevents the
program from closing. I thought this would be easy, but I have not managed
to do it after quite a while trying.

dixie
Nov 25 '05 #1
14 2597
"Dixie" <di***@dogmail.com> wrote in
news:43********@duster.adelaide.on.net:
I am trying to write some code that when I exit my application
kills all of the text files in a certain folder. It is
possible that some of those files are locked because someone
else is using them, in which case they will have the first two
letters of their name replaced with ~$. How can I write a
routine that will delete all the normal *.txt files, but not
attempt to delete the ~$ ones, which brings up an error
message and prevents the program from closing. I thought this
would be easy, but I have not managed to do it after quite a
while trying.

dixie

Scan the directory in question using the dir() function.
Then code an if block that deletes the offending files.

Untested code:

x = dir$("C:\directory to clean")
do while len(x) >0
if left(x,2) <> "~$" then
kill x
endif
x = dir$
loop

--
Bob Quintal

PA is y I've altered my email address.
Nov 25 '05 #2
I've got something like that working OK, except, when there is a ~$ style
file, it is locked and I always get an error, even if I am just trying to
jump over it and delete any other file that does not start with that
character combination.

dixie

"Bob Quintal" <rq******@sympatico.ca> wrote in message
news:Xn**********************@207.35.177.135...
"Dixie" <di***@dogmail.com> wrote in
news:43********@duster.adelaide.on.net:
I am trying to write some code that when I exit my application
kills all of the text files in a certain folder. It is
possible that some of those files are locked because someone
else is using them, in which case they will have the first two
letters of their name replaced with ~$. How can I write a
routine that will delete all the normal *.txt files, but not
attempt to delete the ~$ ones, which brings up an error
message and prevents the program from closing. I thought this
would be easy, but I have not managed to do it after quite a
while trying.

dixie

Scan the directory in question using the dir() function.
Then code an if block that deletes the offending files.

Untested code:

x = dir$("C:\directory to clean")
do while len(x) >0
if left(x,2) <> "~$" then
kill x
endif
x = dir$
loop

--
Bob Quintal

PA is y I've altered my email address.

Nov 26 '05 #3
How about something like this:
On error resume next
x = dir$("C:\directory to clean")
do while x<>""
kill x
x = dir$
loop

Of course that assumes that the directory contains nothing but the .txt
files you want to kill...
"Dixie" <di***@dogmail.com> wrote in message
news:43********@duster.adelaide.on.net...
I am trying to write some code that when I exit my application kills all of the text files in a certain folder. It is possible that some of those files are locked because someone else is using them, in which case they will have the first two letters of their name replaced with ~$. How can I write a
routine that will delete all the normal *.txt files, but not attempt to
delete the ~$ ones, which brings up an error message and prevents the
program from closing. I thought this would be easy, but I have not managed to do it after quite a while trying.

dixie

Nov 26 '05 #4
I don't know what it is I'm doing wrong, but here is the exact code I am
using
Function KillMergeFiles()

On Error Resume Next

Dim x As String

x = Dir$("T:\Test")

Do While x <> ""

Kill x

x = Dir$

Loop

End Function
I have that code in a module and I am running it temporarily from a button
with the command KillMergeFiles.

When I run it, nothing happens to the files in that folder (there are
currently 30 .txt files in there.

dixie

"MacDermott" <ma********@nospam.com> wrote in message
news:8f****************@newsread3.news.atl.earthli nk.net...
How about something like this:
On error resume next
x = dir$("C:\directory to clean")
do while x<>""
kill x
x = dir$
loop

Of course that assumes that the directory contains nothing but the .txt
files you want to kill...
"Dixie" <di***@dogmail.com> wrote in message
news:43********@duster.adelaide.on.net...
I am trying to write some code that when I exit my application kills all

of
the text files in a certain folder. It is possible that some of those

files
are locked because someone else is using them, in which case they will

have
the first two letters of their name replaced with ~$. How can I write a
routine that will delete all the normal *.txt files, but not attempt to
delete the ~$ ones, which brings up an error message and prevents the
program from closing. I thought this would be easy, but I have not

managed
to do it after quite a while trying.

dixie


Nov 26 '05 #5
I have turned my attack to a different method

Sub DeleteFilesThatIDontWant()

Dim fso

Dim sfol As String

sfol = "T:\Test"

Set fso = CreateObject("Scripting.FileSystemObject")

On Error Resume Next

If Not fso.FolderExists(sfol) Then

MsgBox sfol & " is not a valid folder/path.", vbInformation, "Invalid
Source"

Else

fso.deleteFile (sfol & "\*.txt")

End If

End Sub
That will delete all the txt files, but when it comes across a ~$xxxxx.txt
file which is in use, it fails to delete ANY of the files. My problem is
how do I make it ignore that file and delete the others?

dixie

"Dixie" <di***@dogmail.com> wrote in message
news:43******@duster.adelaide.on.net...
I don't know what it is I'm doing wrong, but here is the exact code I am
using

Function KillMergeFiles()
On Error Resume Next
Dim x As String
x = Dir$("T:\Test")
Do While x <> ""
Kill x
x = Dir$
Loop
End Function

I have that code in a module and I am running it temporarily from a button
with the command KillMergeFiles.

When I run it, nothing happens to the files in that folder (there are
currently 30 .txt files in there.

dixie

"MacDermott" <ma********@nospam.com> wrote in message
news:8f****************@newsread3.news.atl.earthli nk.net...
How about something like this:
On error resume next
x = dir$("C:\directory to clean")
do while x<>""
kill x
x = dir$
loop

Of course that assumes that the directory contains nothing but the .txt
files you want to kill...
"Dixie" <di***@dogmail.com> wrote in message
news:43********@duster.adelaide.on.net...
I am trying to write some code that when I exit my application kills all

of
the text files in a certain folder. It is possible that some of those

files
are locked because someone else is using them, in which case they will

have
the first two letters of their name replaced with ~$. How can I write a
routine that will delete all the normal *.txt files, but not attempt to
delete the ~$ ones, which brings up an error message and prevents the
program from closing. I thought this would be easy, but I have not

managed
to do it after quite a while trying.

dixie



Nov 26 '05 #6
Dixie wrote:
I have turned my attack to a different method

Sub DeleteFilesThatIDontWant()

Dim fso

Dim sfol As String

sfol = "T:\Test"

Set fso = CreateObject("Scripting.FileSystemObject")

On Error Resume Next

If Not fso.FolderExists(sfol) Then

MsgBox sfol & " is not a valid folder/path.", vbInformation, "Invalid
Source"

Else

fso.deleteFile (sfol & "\*.txt")

End If

End Sub
That will delete all the txt files, but when it comes across a ~$xxxxx.txt
file which is in use, it fails to delete ANY of the files. My problem is
how do I make it ignore that file and delete the others?


Can you delete the files from Explorer?
Nov 26 '05 #7
Try this instead:
x = Dir$("T:\Test\*.txt")

"Dixie" <di***@dogmail.com> wrote in message
news:43******@duster.adelaide.on.net...
I don't know what it is I'm doing wrong, but here is the exact code I am
using
Function KillMergeFiles()

On Error Resume Next

Dim x As String

x = Dir$("T:\Test")

Do While x <> ""

Kill x

x = Dir$

Loop

End Function
I have that code in a module and I am running it temporarily from a button
with the command KillMergeFiles.

When I run it, nothing happens to the files in that folder (there are
currently 30 .txt files in there.

dixie

"MacDermott" <ma********@nospam.com> wrote in message
news:8f****************@newsread3.news.atl.earthli nk.net...
How about something like this:
On error resume next
x = dir$("C:\directory to clean")
do while x<>""
kill x
x = dir$
loop

Of course that assumes that the directory contains nothing but the .txt
files you want to kill...
"Dixie" <di***@dogmail.com> wrote in message
news:43********@duster.adelaide.on.net...
I am trying to write some code that when I exit my application kills all
of
the text files in a certain folder. It is possible that some of those

files
are locked because someone else is using them, in which case they will

have
the first two letters of their name replaced with ~$. How can I write

a routine that will delete all the normal *.txt files, but not attempt to
delete the ~$ ones, which brings up an error message and prevents the
program from closing. I thought this would be easy, but I have not

managed
to do it after quite a while trying.

dixie



Nov 26 '05 #8
Try: Kill "T:\Test\*.txt"
That will delete all txt files in the T:\Test folder in one line of code.

If that doesn't work then they must be openned and locked by something.

Ian.

"Dixie" <di***@dogmail.com> wrote in message
news:43******@duster.adelaide.on.net...
I don't know what it is I'm doing wrong, but here is the exact code I am
using
Function KillMergeFiles()

On Error Resume Next

Dim x As String

x = Dir$("T:\Test")

Do While x <> ""

Kill x

x = Dir$

Loop

End Function
I have that code in a module and I am running it temporarily from a button
with the command KillMergeFiles.

When I run it, nothing happens to the files in that folder (there are
currently 30 .txt files in there.

dixie

"MacDermott" <ma********@nospam.com> wrote in message
news:8f****************@newsread3.news.atl.earthli nk.net...
How about something like this:
On error resume next
x = dir$("C:\directory to clean")
do while x<>""
kill x
x = dir$
loop

Of course that assumes that the directory contains nothing but the .txt
files you want to kill...
"Dixie" <di***@dogmail.com> wrote in message
news:43********@duster.adelaide.on.net...
I am trying to write some code that when I exit my application kills all

of
the text files in a certain folder. It is possible that some of those

files
are locked because someone else is using them, in which case they will

have
the first two letters of their name replaced with ~$. How can I write a
routine that will delete all the normal *.txt files, but not attempt to
delete the ~$ ones, which brings up an error message and prevents the
program from closing. I thought this would be easy, but I have not

managed
to do it after quite a while trying.

dixie



Nov 26 '05 #9
So trap the error generated and resume next or jump to an appropriately
positioned label.

There are times where you can't do anything other than use the error
condition as a flag for program branching.

--
Terry Kreft

"Dixie" <di***@dogmail.com> wrote in message
news:43********@duster.adelaide.on.net...
I've got something like that working OK, except, when there is a ~$ style
file, it is locked and I always get an error, even if I am just trying to
jump over it and delete any other file that does not start with that
character combination.

dixie

"Bob Quintal" <rq******@sympatico.ca> wrote in message
news:Xn**********************@207.35.177.135...
"Dixie" <di***@dogmail.com> wrote in
news:43********@duster.adelaide.on.net:
I am trying to write some code that when I exit my application
kills all of the text files in a certain folder. It is
possible that some of those files are locked because someone
else is using them, in which case they will have the first two
letters of their name replaced with ~$. How can I write a
routine that will delete all the normal *.txt files, but not
attempt to delete the ~$ ones, which brings up an error
message and prevents the program from closing. I thought this
would be easy, but I have not managed to do it after quite a
while trying.

dixie

Scan the directory in question using the dir() function.
Then code an if block that deletes the offending files.

Untested code:

x = dir$("C:\directory to clean")
do while len(x) >0
if left(x,2) <> "~$" then
kill x
endif
x = dir$
loop

--
Bob Quintal

PA is y I've altered my email address.


Nov 26 '05 #10
Kill "T:\Test\" & x

The code you have will only work if t:\test is the current directory.
--
Terry Kreft

"Dixie" <di***@dogmail.com> wrote in message
news:43******@duster.adelaide.on.net...
I don't know what it is I'm doing wrong, but here is the exact code I am
using
Function KillMergeFiles()

On Error Resume Next

Dim x As String

x = Dir$("T:\Test")

Do While x <> ""

Kill x

x = Dir$

Loop

End Function
I have that code in a module and I am running it temporarily from a button
with the command KillMergeFiles.

When I run it, nothing happens to the files in that folder (there are
currently 30 .txt files in there.

dixie

"MacDermott" <ma********@nospam.com> wrote in message
news:8f****************@newsread3.news.atl.earthli nk.net...
How about something like this:
On error resume next
x = dir$("C:\directory to clean")
do while x<>""
kill x
x = dir$
loop

Of course that assumes that the directory contains nothing but the .txt
files you want to kill...
"Dixie" <di***@dogmail.com> wrote in message
news:43********@duster.adelaide.on.net...
I am trying to write some code that when I exit my application kills all

of
the text files in a certain folder. It is possible that some of those

files
are locked because someone else is using them, in which case they will

have
the first two letters of their name replaced with ~$. How can I write a
routine that will delete all the normal *.txt files, but not attempt to
delete the ~$ ones, which brings up an error message and prevents the
program from closing. I thought this would be easy, but I have not

managed
to do it after quite a while trying.

dixie



Nov 26 '05 #11
The method you had should work fine except you had'nt taken into account
concatenating the target directory with the file name you obtained from
Dir$.


--
Terry Kreft

"Dixie" <di***@dogmail.com> wrote in message
news:43********@duster.adelaide.on.net...
I have turned my attack to a different method

Sub DeleteFilesThatIDontWant()

Dim fso

Dim sfol As String

sfol = "T:\Test"

Set fso = CreateObject("Scripting.FileSystemObject")

On Error Resume Next

If Not fso.FolderExists(sfol) Then

MsgBox sfol & " is not a valid folder/path.", vbInformation, "Invalid
Source"

Else

fso.deleteFile (sfol & "\*.txt")

End If

End Sub
That will delete all the txt files, but when it comes across a ~$xxxxx.txt
file which is in use, it fails to delete ANY of the files. My problem is
how do I make it ignore that file and delete the others?

dixie

"Dixie" <di***@dogmail.com> wrote in message
news:43******@duster.adelaide.on.net...
I don't know what it is I'm doing wrong, but here is the exact code I am
using

Function KillMergeFiles()
On Error Resume Next
Dim x As String
x = Dir$("T:\Test")
Do While x <> ""
Kill x
x = Dir$
Loop
End Function

I have that code in a module and I am running it temporarily from a
button with the command KillMergeFiles.

When I run it, nothing happens to the files in that folder (there are
currently 30 .txt files in there.

dixie

"MacDermott" <ma********@nospam.com> wrote in message
news:8f****************@newsread3.news.atl.earthli nk.net...
How about something like this:
On error resume next
x = dir$("C:\directory to clean")
do while x<>""
kill x
x = dir$
loop

Of course that assumes that the directory contains nothing but the .txt
files you want to kill...
"Dixie" <di***@dogmail.com> wrote in message
news:43********@duster.adelaide.on.net...
I am trying to write some code that when I exit my application kills
all
of
the text files in a certain folder. It is possible that some of those
files
are locked because someone else is using them, in which case they will
have
the first two letters of their name replaced with ~$. How can I write
a
routine that will delete all the normal *.txt files, but not attempt to
delete the ~$ ones, which brings up an error message and prevents the
program from closing. I thought this would be easy, but I have not
managed
to do it after quite a while trying.

dixie



Nov 26 '05 #12
"Dixie" <di***@dogmail.com> wrote in
news:43******@duster.adelaide.on.net:
I don't know what it is I'm doing wrong, but here is the exact
code I am using
Function KillMergeFiles()

On Error Resume Next

Dim x As String

x = Dir$("T:\Test")

Do While x <> ""

Kill x

The line above should read

Kill "T:\Test" & "\" & x

--
Bob Quintal

PA is y I've altered my email address.
Nov 26 '05 #13
Thats alright, I will actually usie a DLookup to get the folder so it can be
more generic.

dixie

"Terry Kreft" <te*********@mps.co.uk> wrote in message
news:NL********************@karoo.co.uk...
Kill "T:\Test\" & x

The code you have will only work if t:\test is the current directory.
--
Terry Kreft

"Dixie" <di***@dogmail.com> wrote in message
news:43******@duster.adelaide.on.net...
I don't know what it is I'm doing wrong, but here is the exact code I am
using
Function KillMergeFiles()

On Error Resume Next

Dim x As String

x = Dir$("T:\Test")

Do While x <> ""

Kill x

x = Dir$

Loop

End Function
I have that code in a module and I am running it temporarily from a
button with the command KillMergeFiles.

When I run it, nothing happens to the files in that folder (there are
currently 30 .txt files in there.

dixie

"MacDermott" <ma********@nospam.com> wrote in message
news:8f****************@newsread3.news.atl.earthli nk.net...
How about something like this:
On error resume next
x = dir$("C:\directory to clean")
do while x<>""
kill x
x = dir$
loop

Of course that assumes that the directory contains nothing but the .txt
files you want to kill...
"Dixie" <di***@dogmail.com> wrote in message
news:43********@duster.adelaide.on.net...
I am trying to write some code that when I exit my application kills
all
of
the text files in a certain folder. It is possible that some of those
files
are locked because someone else is using them, in which case they will
have
the first two letters of their name replaced with ~$. How can I write
a
routine that will delete all the normal *.txt files, but not attempt to
delete the ~$ ones, which brings up an error message and prevents the
program from closing. I thought this would be easy, but I have not
managed
to do it after quite a while trying.

dixie



Nov 26 '05 #14
Thanks Terry, I've accepted your advice there. It is error number 70 and I
have trapped it and allowed the code to branch to an appropriate label.

Thanks to all who contributed. Appreciated.
dixie

"Terry Kreft" <te*********@mps.co.uk> wrote in message
news:NL********************@karoo.co.uk...
So trap the error generated and resume next or jump to an appropriately
positioned label.

There are times where you can't do anything other than use the error
condition as a flag for program branching.

--
Terry Kreft

"Dixie" <di***@dogmail.com> wrote in message
news:43********@duster.adelaide.on.net...
I've got something like that working OK, except, when there is a ~$ style
file, it is locked and I always get an error, even if I am just trying to
jump over it and delete any other file that does not start with that
character combination.

dixie

"Bob Quintal" <rq******@sympatico.ca> wrote in message
news:Xn**********************@207.35.177.135...
"Dixie" <di***@dogmail.com> wrote in
news:43********@duster.adelaide.on.net:

I am trying to write some code that when I exit my application
kills all of the text files in a certain folder. It is
possible that some of those files are locked because someone
else is using them, in which case they will have the first two
letters of their name replaced with ~$. How can I write a
routine that will delete all the normal *.txt files, but not
attempt to delete the ~$ ones, which brings up an error
message and prevents the program from closing. I thought this
would be easy, but I have not managed to do it after quite a
while trying.

dixie

Scan the directory in question using the dir() function.
Then code an if block that deletes the offending files.

Untested code:

x = dir$("C:\directory to clean")
do while len(x) >0
if left(x,2) <> "~$" then
kill x
endif
x = dir$
loop

--
Bob Quintal

PA is y I've altered my email address.



Nov 26 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: sea | last post by:
I have text files in the following format: 123,34, ,345,890, 123,23 .. .. .. As you can see, the problem is that (1) the commas can occur in
10
by: ross | last post by:
I want to do some tricky text file manipulation on many files, but have only a little programming knowledge. What are the ideal languages for the following examples? 1. Starting from a certain...
7
by: David Mitchell | last post by:
I use a function to read all of the files from a couple of directories (and subfolders) and update a table(tblfiles) with the fullpath and file name, the filesize and the date the file was created....
1
by: dixie | last post by:
I am trying to delete the temporary files that MS Word leaves behind sometimes when it fails to clean up properly. Files like ~$lename.dot. I have used the Kill command, but find that this...
3
by: Scott | last post by:
Hello, I am running into a problem with my code and can't seem to figure out the solution. I know it has to do with the pciture box control and unloading the image inthe picture box but I can't...
5
by: Dino Buljubasic | last post by:
My application can allow a user to open a file for viewing by fetching file data from database, creating the file in a temp directory and starting appropriate process (i.e. Adobe or any other...
5
by: g0c | last post by:
hi all, i really cannot find a simple exmaple for preg_match function i whiled the files in the directory ... $d = dir('.') or die($php_errormsg); while (false !== ($f = $d->read())) { if...
13
by: kbperry | last post by:
Hi all, Background: I need some help. I am trying to streamline a process for one of our technical writers. He is using Perforce (version control system), and is constantly changing his word...
10
by: Kenneth Brody | last post by:
I recently ran into an "issue" related to text files and ftell/fseek, and I'd like to know if it's a bug, or simply an annoying, but still conforming, implementation. The platform is Windows,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.