Need to add a whitelist to my .bat

So I have a .bat that deletes a certain extension, but I need to whitelist a few of them. I'm not having much luck.

xcopy "universe" "C:\Users\Administrator\Desktop\Backup\universe - %stamp%" /I
for %%i in ("universe\*.world") do if not "%%i"=="test.world" if not "%%i"=="test2.world" del /q "%%i"
exit

I made a some dummy files, but even test.world and test2.world is being deleted with everything else .world. Anyone have any idea?

If possible what I want is a simple text file that lets me add on file names onto it and let it whitelist it from there, because some of the .world names generated are pretty huge in names.

Answers


Here's a solution - the /i is needed to match upper and lower case and the %%~nxi matches the filename and extension.

for %%i in ("universe\*.world") do if /i not "%%~nxi"=="test.world" if /i not "%%~nxi"=="test2.world" del /q "%%i"

When you are trying to debug batch files ECHO is your friend. Try this:

for %%i in ("universe\*.world") do echo %%i

This will print file names like "universe\test.world", which makes the problem clear.

You need to include the "universe\" in the names you are testing for, for example:

if not "%%i"=="universe\test.world"

You can make your code more readable by splitting it over multiple lines:

for %%i in (universe\*.world) do (
  if not "%%i"=="universe\test.world" (
  if not "%%i"=="universe\test2.world" (
    echo %%i
  ))
)

Note that on the line after the ECHO you need as many closing parentheses as you have IF statements.


Need Your Help

Adding custom data attribute to html <option> tag within a @Html.EditorFor in an MVC Razor view

asp.net-mvc-3 razor

I want to add a custom data attribute to each option in my selectlist, which I could achieve with jQuery.data() or .attr().

Print character without system calls

assembly x86 nasm qemu osdev

I'm wondering how to print a character in assembly without int 0x80 and other system calls. I am doing this for a kernel. I have a working print function, but I want to write one in assembly, so I ...