Question

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.

Was it helpful?

Solution

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"

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top