1.

Solve : delete program?

Answer»

I'm using this program to delete stuff but it doesn't delete folders how can i make it delete folders
Code: [Select]echo off
echo y | del %1
echo the file %1 was deleted
pause
del removes files; RD removes directories (folders), but they have to be empty first.
how can the program cheak if its a folder or file
This should work on an XP machine. The RD command will only delete empty directories unless you use switches to override the default. Not recommended. You may delete more than you bargained for.

Code: [Select]echo off
if exist %1\nul (rd %1) else (echo y | del %1)

This is batch code, so SAVE the SNIPPET with a bat extension and run from the command prompt as scriptname.bat

The code expects a file or directory name to be passed on the command line.

Good luck.  sorry sidewinder that dosent work
this does though but THANKS for your help
Code: [Select]echo off
if exist %1/.. goto fol > nul
echo y | del %1
goto :eof

:fol
echo y | del %1
echo y | rd %1
that'a exactly what sidewinder posted, but your checking for .. instead of nul, and you used the wrong slash (the forward slash (/) is used for web ADDRESSES and UNC paths (and Linux pathnames). the backslash (\) is used as a path separator on windows systems. Most file accesses can be done with both since they convert forward slashes to backslashes as appropriate, but bear in mind that the  "natural" path separator is \.

this is functionally identical to your "changed" version:

Code: [Select]if exist %1\nul (echo y | rd %1) else (echo y | del %1)

I can't think of any reason you'd need to but you can change nul to . or ..

Your version makes no sense... if you detect it's a directory, you still use del on it. This has the effect of deleting the contents of the folder.

if you want the same ability with SideWinders script, you should read his comment re: switches. you can use the /s switch to make rd/rmdir remove a directory and all it's sub-directories in a single blow. if you use /q you won't even need to pipe a "y" response to it, either. so:

Code: [Select]Echo off
if exist %1\nul (rd %1 /s /q) else (echo y | del %1)
the only reason i said that he got it wrong was i didnt checck if it works using cmd i just draged the folder on top. I must have missed the part where you wanted drag and drop capabilities:

Code: [Select]echo off
:next   
   if exist %1\nul (echo y | rd %1) else (echo y | del %1)
   if .%2 equ . goto :eof
   shift
   goto next

When coding drag and drop, you should code for the situation where the user drags multiple files, folders or a combination of both.

Why not just drag and drop to the recycle bin? Not only do you logically remove the files and folders but you can easily restore them in case of the over enthusiastic user. With batch files or Windows scripts there is no provision for a pit stop in the recycle bin.

Good luck. Quote from: Sidewinder on March 30, 2010, 03:03:52 PM



Why not just drag and drop to the recycle bin? Not only do you logically remove the files and folders but you can easily restore them in case of the over enthusiastic user. With batch files or Windows scripts there is no provision for a pit stop in the recycle bin.


Additionally, if they want to permanently delete them, you can hold shift when you drop it on the recycle bin as well.


Discussion

No Comment Found