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.
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.
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.
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 *.*)
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
)