How can delete file by Marco

H

Hello

Hi,

I have problem to delete files, because I only want to delete some
file the name have / include the words of "test" on file name, and I
don't know the path in my local drive (C:). How can I do?

Here is my code for specific path, but how can I change the code for
delete if the file name have / include the words of "test" under my
local drive (C:). Thanks~

On Error Resume Next
Kill "C:\Users\*test*.xl*"
On Error GoTo 0
 
H

Hello

I also need to delete the file (have / include file name "test") if
the file is open too (active workbook). And don't want to shown the
pop up message on the screen.
 
G

GS

It happens that Hello formulated :
I also need to delete the file (have / include file name "test") if
the file is open too (active workbook). And don't want to shown the
pop up message on the screen.

Uh.., you can't delete an open file so that's not going to fly!
 
H

Hello

is it any way change the file to read only, then can delete files?
if ignore the open file this problem, how can I delete the files
(have / include file name "test"?
 
G

GS

So, how can I change the code to delete file name have / include
"test"?

I expect you'd need to do a search of all drives/folders to locate all
files that you're looking for. Sounds like you need to spend some time
reading the VB help file. Otherwise, someone who has code for that
needs to step up. (Sorry, but I don't have anything readily available)
 
G

GS

Hello expressed precisely :
is it any way change the file to read only, then can delete files?
if ignore the open file this problem, how can I delete the files
(have / include file name "test"?

No! Windows will not allow you to delete files in use.
 
H

Hello

Hi Gary,

I don't need the code for delete files in use now.
Could you teach me how can I change the code to delete files when I
need to run this marco on my teammate's (users) computer, not on my
computer, so I cannot search all of drives / folders.

On Error Resume Next
Kill "C:\Users\*test*.xl*"
On Error GoTo 0

Thanks
 
D

Dave Peterson

Activeworkbook.ChangeFileAccess Mode:=xlReadOnly
kill activeworkbook.fullname
 
H

Hello

Hi Dave,

How can I delete file have / include "test" in file name under C drive
and don't know the path? I want to delete all files have / include
"test" .
 
J

Jim Cone

Maybe you should look up the definition of the terms "angry mob" or "fired"...
"New Testament Verses"
"Test Results-Five Year Project"
"My court testimony"
--
Jim Cone
Portland, Oregon USA
http://www.mediafire.com/PrimitiveSoftware
(List Files: specific files/folders with hyperlinks)




"Hello" <[email protected]>
wrote in message
news:87079ae1-f43a-4c69-8c44-5ac9e71e2d15@n36g2000pre.googlegroups.com...
 
C

Clif McIrvin

GS said:
I expect you'd need to do a search of all drives/folders to locate all
files that you're looking for. Sounds like you need to spend some time
reading the VB help file. Otherwise, someone who has code for that
needs to step up. (Sorry, but I don't have anything readily available)


Here's a code snippet that finds all the workbook files in the current
folder and calls another procedure for each one. As Garry suggests,
spend some time digging into the VBA help ... perhaps this will help you
along:

' Get list of workbook files in "current" folder
filename = ActiveWorkbook.Path & "\*.xl??"
filename = Dir(filename)

Do
'Debug.Print filename
ExtendIndex ActiveWorkbook.Path & "\" & filename
filename = Dir()
Loop Until filename = ""

Dir is a VBA function, there are related functions that allow you to
change the current directory, etc. .... there is also a file system
object that I have never worked with but might be better suited to your
need.
 
G

GS

Dave Peterson laid this down on his screen :
Activeworkbook.ChangeFileAccess Mode:=xlReadOnly
kill activeworkbook.fullname

Thanks, Dave! I didn't think this was possible but I see I was wrong.
This adds a whole new dynamic to file management...
 
D

Dave Peterson

First, I wouldn't do this kind of thing using excel.

I'd use windows search/find to look for:
*test*.xl*
Then I could look at the results and check to see if each should be deleted.

If I didn't have to verify the results, I could just select all the files on the
right hand side window (by hitting ctrl-a) and hit the delete key.

But if you wanted to search all of drive C: including subfolders, then you could
use application.filesearch in xl2003 and before.

If you're using xl2007 or higher (works in previous versions, too), you can use
code at Ron de Bruin's site to loop through subfolders.

He has a couple of ways here:
http://www.rondebruin.nl/fso.htm
and
http://www.rondebruin.nl/copy3.htm

Again, I wouldn't use excel for this -- even though it's possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top