# DOS command to delete all except one file



## sulasno (Jul 3, 2010)

```
for %i in (C:\language) do if not %i == English.ini del /q  %i
```
what's wrong with the above command ? trying to delete all *.ini files except English.ini in a folder

what's does "==" do in the above command ?


----------



## Squashman (Apr 4, 2003)

```
C:\Language>dir /b
English.ini
Spanish.ini

C:\Language>for %i in (*.ini) do if not "%i"=="English.ini" del /q %i

C:\Language>if not "English.ini" == "English.ini" del /q English.ini

C:\Language>if not "Spanish.ini" == "English.ini" del /q Spanish.ini

C:\Language>dir /b
English.ini

C:\Language>
```


----------



## sulasno (Jul 3, 2010)

still no go but I use an inelegant workaround;

REN "C:\language\English.ini" English.dat
DEL /Q C:\language\*.ini
REN "C:\language\English.dat" English.ini


----------



## Squashman (Apr 4, 2003)

sulasno said:


> still no go but I use an inelegant workaround;
> 
> REN "C:\language\English.ini" English.dat
> DEL /Q C:\language\*.ini
> REN "C:\language\English.dat" English.ini


How does that not work. I just did it right from the command line and showed you that it worked. I showed you what was in the directory before I ran the command. I then ran the command and then showed you what was left in the directory.


----------



## sulasno (Jul 3, 2010)

ooops sorry
it works but it would mean that I would have to input all the languages in the batch file
(and there will be errors if the language is not available in the folder)


----------



## Squashman (Apr 4, 2003)

sulasno said:


> ooops sorry
> it works but it would mean that I would have to input all the languages in the batch file
> (and there will be errors if the language is not available in the folder)


Not sure what you mean by all the languages. You just said you needed all the ini files deleted from that one folder except for the English.ini file. You also didn't say you were running this from a batch file. Your example showed that you were running it from the command line because you only used one "%" in your example with the FOR Loop. You need to use two "%" in a batch file.


----------



## sulasno (Jul 3, 2010)

this is the line in the batch file that is not working

for %i in (C:\language\*.ini) do if not "%i"=="English.ini" del /q %i


----------



## Squashman (Apr 4, 2003)

sulasno said:


> this is the line in the batch file that is not working
> 
> for %i in (C:\language\*.ini) do if not "%i"=="English.ini" del /q %i


Two problems with that line of code. If you have the batch file echoing all the ouput to the screen you would see the problem.

1) I just told you in my previous post that when using the FOR command in a batch file you need to use two "%" signs with the FOR loop variable. If you would read the documentation for the command you would realize that.


> H:\>for /?
> Runs a specified command for each file in a set of files.
> 
> FOR %variable IN (set) DO command [command-parameters]
> ...


2) Now lets look at the what the actual command output looks like when you run that command.

I just have two files in my Language folder. Spanish.ini and English.ini

```
H:\>for %i in (C:\language\*.ini) do if not "%i"=="English.ini" del /q %i

H:\>if not "C:\language\English.ini" == "English.ini" del /q C:\language\English.ini

H:\>if not "C:\language\Spanish.ini" == "English.ini" del /q C:\language\Spanish.ini
```
In the first line of output you can see that the For Loop variable %I is assigned *"C:\language\English.ini"* and you are comparing that too *English.ini*. Because you used the NOT option with the IF statement it is a True statement and it deletes the *English.ini* file.

What you are trying to accomplish is to get the %I variable assigned as *English.ini* so that your If comparison is FALSE and doesn't delete the English.ini file.

If you read the very bottom of the help for the FOR command you will see that this is possible using the Modifiers for the For Loop variable.

I would suggest you type: *For /?* and read all the help for the FOR command. But here is the part you need to make your batch file work the way you want it to. I am teaching you to fish here.



> In addition, substitution of FOR variable references has been enhanced.
> You can now use the following optional syntax:
> 
> %~I - expands %I removing any surrounding quotes (")
> ...


----------



## sulasno (Jul 3, 2010)

thanks for the info
will try and figure it out

guess I finally got it to work with this

for %%i in (*.ini) do if not "%%i"=="English.ini" del /q "%%i


----------



## Squashman (Apr 4, 2003)

sulasno said:


> thanks for the info
> will try and figure it out
> 
> guess I finally got it to work with this
> ...


yes, that will work if the Current Working Directory is where your files are located.


----------

