views:

36

answers:

3

Hi.

How am I able to make a batch file with an If not command, such as: 'IF NOT [extension type] Then delete all' in a folder?

Thanx.

A: 

You didn't specify this any further, and it's a little unclear what exactly it is that you want to achieve... but if you are talking about Windows .bat batch commands, the syntax to check for existence of one or several files with the extension .ext is as follows:

IF NOT EXIST *.ext (DEL *.*)

You can get more detailed help for the IF statement by typing the following inside a CMD.EXE shell:

IF /?

This will give you more examples about how to use the IF statement.

stakx
Sorry, yeah I meant Windows. And I checked If /?, but I got confused. I made an attempt, but it didn't work.And I guess I should have been more specific. That didn't work, but I changed it to IF EXIST, and it deleted all. How can I make it delete all, EXCEPT [extention type]? :)
John
A: 

I can't leave a comment so this answer is in reference to stakx's answer:

On at least some versions of Windows, del *.* will pause the batch file and ask the user to confirm the mass delete. This behavior can be avoided by using the /q switch, like this:

IF NOT EXIST *.ext (DEL /q *.*)
poke
+2  A: 

The for command combined with an if command and variable expansion can be used to delete all files except those with the specified extension. For example, this will delete all files in the current directory that do not have a .TXT extension (case insensitive):

for %%i in (*) do (
    if /i "%%~xi" neq ".TXT" del %%i
)
poke
Just be careful what folder you run this in.
aphoria