revolt
revolt

Reputation: 241

Bat file for removing all files in a folder except some files and some extensions

I need a bat file for subj. I have following:

for %%v in (*.*) do if not %%v == _clean.bat if not %%v == gc.dll if not %%v == dmc.exe if not %%v == lib.exe if not %%v == libunres.exe if not %%v == link.exe if not %%v == make.exe if not %%v == men.exe if not %%v == sc.exe if not %%v == scppn.exe if not %%v == sc.ini if not %%v == *.cpp if not %%v == *.c del %%v

It works almost ok - it keeps files with the names, but it deletes also *.c and *.cpp files, which I do not want to delete.

Solved!

@echo off
for %%v in (*.*) do (
if not %%v == _clean.bat if not %%v == gc.dll if not %%v == dmc.exe if not %%v == lib.exe if not %%v == libunres.exe if not %%v == link.exe if not %%v == make.exe if not %%v == men.exe if not %%v == sc.exe if not %%v == scppn.exe if not %%v == sc.ini if not %%~xv == .cpp if not %%~xv == .c del %%v)

Upvotes: 0

Views: 119

Answers (2)

Aacini
Aacini

Reputation: 67296

I think this way is simpler and easier to maintain:

@echo off
setlocal EnableDelayedExpansion

set "excludeFile= _clean.bat gc.dll dmc.exe lib.exe libunres.exe link.exe make.exe men.exe sc.exe scppn.exe sc.ini "
set "excludeExt= .cpp .c "

for %%v in (*.*) do (
   if "!excludeFile: %%v =!" equ "%excludeFile%" if "!excludeExt: %%~Xv =!" equ "%excludeExt%" del %%v
)

Just be sure that the exclude lists begin and end with space.

Upvotes: 1

Bk_
Bk_

Reputation: 99

Try the following:

@echo off
for %%v in (*.*) do (
if not %%v == _clean.bat if not %%v == gc.dll if not %%v == dmc.exe if not %%v == lib.exe if not %%v == libunres.exe if not %%v == link.exe if not %%v == make.exe if not %%v == men.exe if not %%v == sc.exe if not %%v == scppn.exe if not %%v == sc.ini if not %%~xv == .cpp if not %%~xv == .c del %%v)

For further details, use help for to get a complete list of the modifiers - there are quite a few!

Upvotes: 1

Related Questions