deleting directory
William Labbett

hi guys,

is there a way to delete a directory and all it's contents with A5 ?

Thanks.

verthex

If you could somehow invoke dos commands from A5 sure.

SiegeLord

Yes, by recursively looking through a directory using al_open_directory /al_read_directory (using al_get_fs_entry_mode to tell if the current entry is a directory and needs to be recursed into) and then using al_remove_fs_entry to actually delete the file/empty directory.

There's obviously no convenience function for that, but doing the above shouldn't take more than 10-20 lines.

William Labbett

Thanks.

Maybe it'd be easier to do with a scipt ?

I'm using a script to compile and run my program.

I've tried using the rmdir command but it says command not found, even though it's in the bash docs.

SiegeLord

Well, in that case you'd use rm:

rm -rf directory_name

Be careful with what you put in the directory_name lest you delete something you didn't want to delete.

William Labbett

Thanks. Still getting a command not found.

#!bin/bash
rm -rf "C:\pongping8\feature_hits_circle_dir_01" 

cd C:/pongping8
if mingw32-make 
then 
./pong_game.exe 
fi
read -p "press a key"

C:\pongping8\make_pongping.sh: line 2: rm: command not found
mingw32-make: `pong_game.exe' is up to date.
Press any key to continue . . .

SiegeLord

You're using bash... on Windows? That's surprising.

You'll need to find the Windows equivalent then, I guess. ???

William Labbett

On Windows, MSYS can be used to use the bash commands. Although I only get this when I call it from MSYS :

william@william-PC ~
$ cd C:/pongping8

william@william-PC /c/pongping8
$ ./make_pongping.sh
sh: ./make_pongping.sh: bin/sh^M: bad interpreter: No such file or directory

william@william-PC /c/pongping8
$

J-Gamer

Shouldn't that be /bin/sh?

William Labbett

I don't know. I changed it to that to see if it would work. I've changed it back and I still get the same output.
I've never know wht that line does.

J-Gamer

It tells the system that the program should be opened with bin/sh. As it seems it is already being opened with sh, try removing the line.(I'm just guessing here)

William Labbett

Okay, I tried that but I still get this :

C:\pongping8\make_pongping.sh: line 2: rm: command not found
mingw32-make: `pong_game.exe' is up to date.
Press any key to continue . . .

Haven't got a clue why it's not found.

I must be doing something stupid.

J-Gamer

This file works for me in Ubuntu 11.10.

#!/bin/sh
touch "blargh"
rm blargh

Does msys have rm? Try the steps of the program manually on the command-line.

William Labbett

Manually it works okay but not from the script.

jmasterx

On Windows, I think the command is 'rmdir' if you're on Windows maybe that's why it is not working?

William Labbett

I tried that. command not found again.

Dario ff

rd?

jmasterx

As well as to be restricting yourself to Windows, you might as well do it right and use RemoveDirectory http://msdn.microsoft.com/en-us/library/aa365488%28VS.85%29.aspx

Or this for non-empty directories:

#SelectExpand
1void silently_remove_directory(LPCTSTR dir) // Fully qualified name of the directory being deleted, without trailing backslash 2{ 3 SHFILEOPSTRUCT file_op = { 4 NULL, 5 FO_DELETE, 6 dir, 7 "", 8 FOF_NOCONFIRMATION | 9 FOF_NOERRORUI | 10 FOF_SILENT, 11 false, 12 0, 13 "" }; 14 SHFileOperation(&file_op); 15}

gnolam
jmasterx said:

On Windows, I think the command is 'rmdir'

rd.
rd /s for a recursive delete including all files (equivalent to UNIX's rm -r). rd /s /q for recursive delete in quiet mode (don't ask for permission before deleting stuff - equivalent to UNIX's rm -rf).

Sirocco

Remove is portable, and although I haven't tried to use it in that fashion, is supposedly able to remove directories as well as files. I'm not sure if it can get the directory and all the files in one shot, but it'd be worth trying.

William Labbett

Thanks jmasterx.

I got that to work..

I'm still puzzled about why none of rm -rf, rmdir, or rd work when other commands like

cd work fine.

J-Gamer
Edgar Reynaldo
system("rmdir /s /q directory_to_delete");

Trent Gamblin

cd works because it's both a bash and cmd.exe command. rmdir and rd don't work in bash because they're not bash commands. rm doesn't work because you don't have rm installed. You can get many unix utils like rm for Windows here.

Edgar Reynaldo

I think the question is why doesn't a .sh script using rm work in MSYS, when it already has rm. And it should work...

Thread #609309. Printed from Allegro.cc