LiefLayer
LiefLayer

Reputation: 1073

Windows batch if not equal do not execute if clause

I have a .bat file to execute in a windows server 2012 R2 machine. Problem is that I need to add an "if not equals" but when I try to execute it the code does not work. I know that problem is in the if because I tried without the if and it works.

setlocal enabledelayedexpansion

set anno=%date:~6,4%
set mese=%date:~3,2%
set giorno=%date:~0,2%

set ore=%time:~0,2%
set minuti=%time:~3,2%
set secondi=%time:~6,2%

e:
mkdir "E:\Presenze\%anno%%mese%%giorno%%ore%%minuti%%secondi%"

move \\gbjob09\Info-Bit\Sql_Ges\PRESENZE\2019\*.old "E:\Presenze\%anno%%mese%%giorno%%ore%%minuti%%secondi%"
cd "E:\Presenze\%anno%%mese%%giorno%%ore%%minuti%%secondi%"

for /f %%a in ('dir /b *.old') do (
    set originalname=%%a 
    set timbrnumber=!originalname:~4,4!
    if NOT %timbrnumber% == "0584" (
        if NOT %timbrnumber% == "0585" (
            set shortname=9!originalname:~4,4!
            echo !shortname!  
            for /f "tokens=*" %%b in (%%a) do (
                echo %%b!shortname!>> "Presenze.txt"
            )
        )
    )
)

copy "E:\Presenze\%anno%%mese%%giorno%%ore%%minuti%%secondi%\Presenze.txt" "E:\Presenze\presenze.txt"

exit

Like you can see the problem is only this really small part:

if NOT %timbrnumber% == "0584" (

I already tried to remove the double if but it still does not work.

I know that the old code (this one) works:

setlocal enabledelayedexpansion

set anno=%date:~6,4%
set mese=%date:~3,2%
set giorno=%date:~0,2%

set ore=%time:~0,2%
set minuti=%time:~3,2%
set secondi=%time:~6,2%

e:
mkdir "E:\Presenze\%anno%%mese%%giorno%%ore%%minuti%%secondi%"

move \\gbjob09\Info-Bit\Sql_Ges\PRESENZE\2019\*.old "E:\Presenze\%anno%%mese%%giorno%%ore%%minuti%%secondi%"
cd "E:\Presenze\%anno%%mese%%giorno%%ore%%minuti%%secondi%"

for /f %%a in ('dir /b *.old') do (
    set originalname=%%a 
    set shortname=9!originalname:~4,4!
    echo !shortname!  
    for /f "tokens=*" %%b in (%%a) do (
        echo %%b!shortname!>> "Presenze.txt"
    )
)

copy "E:\Presenze\%anno%%mese%%giorno%%ore%%minuti%%secondi%\Presenze.txt" "E:\Presenze\presenze.txt"

exit

I only added a variable:

set timbrnumber=!originalname:~4,4!

that should work (it shouold get 4 characters after exclude 4 characters of the originalname variable) like shortname print 9 and 4 characters after exclude 4 characters of the originalname variable, but I'm not really sure because I don't really know windows batch (this is the first time that I use it and the original code was not mine). After that I only need to check if timbrnumber is not "0584" and it's not "0585" but everything I tried was a fail.

Upvotes: 0

Views: 6182

Answers (1)

user7818749
user7818749

Reputation:

In short, as your variable is being set and used inside a code block and double quoting is required both sides of the ==, this section should fix your issue:

if NOT "1!timbrnumber! == "10584" (
    if NOT "!1timbrnumber!" == "10585" (

which can be better written as:

if 1!timbrnumber! neq 10584 (
    if 1!timbrnumber! neq 10585 (

Another observation, your time:

set ore=%time:~0,2%
set minuti=%time:~3,2%
set secondi=%time:~6,2%

can be better created as:

 set mytime=%time::=%
 echo %mytime:0,6%

Upvotes: 1

Related Questions