Click to See Complete Forum and Search --> : Searching Through Files on the Drive


Mikenell
01-21-2001, 05:45 AM
In C, how would I make a program search through all of the files on the drive for a specific one and then delete it? Or is there a dos command that will search through all the files on the drive and in the subdirs for it and then delete all the file specified which i could then use with the system function? I thought about using the find command in dos, then saving the file's location in a string, then removing the file at the location of the string. If theres another way to do it that anyone can think of then that'd be cool too, but it has to be in dos.
Thanks
Mikenell

Mikenell
01-21-2001, 06:09 AM
Actually, if its easier in another language, anything that can compile into a self excecuting binary file will do.
Thanks
Mikenell

prince_kenshi
01-21-2001, 06:12 AM
In dos, you can use the /s flag with dir to search in subdirectories. For instance, to search for mp3's on your c:, you could try
dir c:\*.mp3 /s
I'm not sure if it's the same in other versions of dos, but in Win 2K, /b will show the files in bare format. This is needed for deleting. You can redirect all the files names to the delete command, like so:
dir c:\*.mp3 /s /b | del
That would delete all mp3's on your c:. Check on that /b first though. Type dir /? to find out. If dir doesn't return the results in bare format, it will confuse the hell out of del.

------------------
Prince Kenshi
Son of Bahamut

prince_kenshi
01-21-2001, 06:31 AM
It seems I was wrong about my last post. It could lead up to something though. The pipe doesn't pass the files as arguments to the command, it passes them after the command is executed. If you replaced the pipe with a greater than sign and specified a file name instead of the del command, you could make a list of the files you need to delete and perhaps make a program that would run it. The tough part is that if you piped the list from the dir, it wouldn't restart the delete command for every file. Several months ago I made a c++ program that listed all my mp3's. I couldn't figure out how to make it scan subdirectories though, so I had to put in all the subdirectories into the program. If you can find a programming equivalent of dir /b /s, deleting the files should be a snap.

------------------
Prince Kenshi
Son of Bahamut

Mikenell
01-21-2001, 06:37 AM
Wow Thanks. That command is cool, now if I could only find a way to assign the results of a dir c:\*.mp3 /s /b to strings I think I could then do system("del %s", string); couldn't I?
Mikenell

prince_kenshi
01-21-2001, 06:49 AM
Well like I said, you could issue this command:
dir *.mp3 /s /b > mp3list.txt
and it will make a list of the files seperated by return carriages. Then perhaps you could make your program read the file one line at a time into an array and use it for the del command. I'm afraid I only learned basic c so I can't really tell you the functions to use. I hope this will be enough to help you figure it out, especially since it's about time for me to go to sleep. Good luck and tell me if it works.

------------------
Prince Kenshi
Son of Bahamut

Mikenell
01-21-2001, 07:43 AM
Thanks a lot, i think i can use that redirection, and i have to to read the path from the file, now i need a way to rename or delete a file without the system command.

Mikenell
01-21-2001, 08:15 AM
Done it, its messy but it works.


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
char path[500];

void main()
{
FILE *fp;
system("dir file.to.delete /s /b > tmp.dat");
fp = fopen("tmp.dat", "r");
fscanf(fp, "%s", path);
fclose(fp);
system("del tmp.dat");
rename(path, "c:\\file.bla");
system("del c:\\file.bla");
}


Mikenell

[This message has been edited by Mikenell (edited 21 January 2001).]

YaRness
01-21-2001, 08:55 AM
i posted some links to some info about DOS .bat files. if you search around, you might be able to find one that someone already wrote.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

TheLinuxDuck
01-21-2001, 04:23 PM
Mikenell:

The function to delete a file in C is unlink. The format is:

int unlink(const char *filename);

On success, it returns a 0; on error, it returns -1, and sets errno.

Hope that helps! http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
I have a belly button.
:wq