Home > Blockchain >  Batch Script: How can I create a blacklist?
Batch Script: How can I create a blacklist?

Time:12-06

I am trying to create a blacklist of elements which should be skipped at the run time. I have some batch files which must be executed, but some of them are not working properly so until they are fixed i want to skip them for now.

This is what I am trying to do right now:

@set BLACKLIST=(element1, element2, element3)

REM Call the ut.bat files only if they are outside the blacklist,
REM otherwise skip them.

for /F "delims=" %%i in ('dir /s/b ut.bat') do (
    for %%j in %BLACKLIST% do (
        if /I not "%%~dpi" == "%~dp0%%j\" (
            CALL %%i
        )
))

CodePudding user response:

Here is a possible solution for you:

@set BLACKLIST=(element1, element2, element3)

rem Check if the current file is in the BLACKLIST
for /F "delims=" %%i in ('dir /s/b ut.bat') do (
    rem Store the full path of the current file in a variable
    set FILEPATH=%%~dpi
    
    rem Initialize the SKIP variable to false
    set SKIP=false
    
    rem Loop through the elements in the BLACKLIST
    for %%j in %BLACKLIST% do (
        rem Compare the current file path to the path of the current blacklist element
        if /I "%FILEPATH%" == "%~dp0%%j" (
            rem If the paths match, set SKIP to true and break out of the loop
            set SKIP=true
            goto SKIP_CALL
        )
    )

    rem If SKIP is still false, call the file
    if not %SKIP% == true (
        CALL %%i
    )
    
    rem Label used to break out of the inner loop
    :SKIP_CALL
)

In this solution, we first check if the current file is in the BLACKLIST. If it is, we set a variable called SKIP to true and skip calling the file. Otherwise, we call the file.

Let me know if you have any questions.

CodePudding user response:

Your problem is that for any directory found, your code will run if that code does not match element1, and again if it does not match element2 and so on. Since it cannot match both element1 and element2, then it will run at least once.

@set "BLACKLIST=element1, element2, element3"

REM Call the ut.bat files only if they are outside the blacklist,
REM otherwise skip them.

for /F "delims=" %%i in ('dir /s/b ut.bat') do (
    SET "RUNME=Y"
    for %%j in (%BLACKLIST%) do (
            if /I "%%~dpi" == "%~dp0%%j\" SET "RUNME="
    )
    IF DEFINED RUNME CALL %%i
)

Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign " or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value" is used, then the quotes become part of the value assigned.

First. for every directory encountered, set runme to a value.

If the directory is on the blacklist, set runme to nothing, which un-defines it.

Only if the directory is not on the blacklist will every if fail and runme survive, set to something

  • Related