0

I'm 99% done with my batchfile and now I'm facing the very last obstactle which just doesn't seem to want to go away. This is the current script:

@echo off 

SET SPT4="C:\Testfolder\Test.Screen.txt"
SET SPT6="C:\Testfolder\Test.Screen.Temp.txt"

Type %SPT4% | Findstr "SerialNumberID= UserFriendlyName=" > C:\Testfolder\Temp.txt

Echo Screenmodel: > %SPT6%

for /f "delims=" %%x in (C:\Testfolder\Temp.txt) DO (
    echo %%x
    ::Processing the gained informations.
    set SN1=%%x
    echo %SN1%
    set "list=%SN1: =%"
    echo %list%
    set "list=%x:{= %"
    set "list=%list:}= %"
    set "list=%list:SerialNumberID= %"
    set "list=%list:UserFriendlyName= %"

    Echo %list%

    SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
    (
        FOR %%y IN (%list%) DO (
            cmd /c exit %%y
            <nul set /p =!=exitcodeAscii!
        ) >> %SPT6%
    )
    Echo. >> %SPT6%
)

del C:\Testfolder\Temp.txt

timeout 10 /nobreak > nul

The script first defines the variables and filters out a file with the screeninformations provided in another file so I end up with only 4 lines which are saved in the Temp.txt.

Here are the informations from the file "Temp.txt"

SerialNumberID={89,86,51,84,49,55,52,49,52,49,0,0,0,0,0,0}

UserFriendlyName={66,49,57,45,54,32,76,69,68,0,0,0,0}

SerialNumberID={53,90,48,57,54,55,51,52,78,66,0,0,0,0,0,0}

UserFriendlyName={69,65,50,52,52,87,77,105,0,0,0,0,0}

The script itself works fine but I need to remove all the unnecessary characters like "= { }". So in order to do so I want to pass the informations to the var %list% but when I want to echo it I only recieve a message like

Echo disabled / Echo Command deactivated

The final output also is not a clear name but a blank line.

When I replace the %list with %%x the whole thing works as intended but the first letter is missing from all the lines due to the "= { }"

Can someone please give me an idea on what to try to solve this issue? I know it's something really small I'm probably missing.

Solution:

I had to replace the middle part of the code with this:

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
(
    for /f "delims=" %%x in (C:\Testfolder\Temp.txt) DO (
        ::Processing the gained informations.
        set SN1=%%x
        echo !SN1!
        set "list=!SN1!"
        echo 1 !list!
        set "list=!list:}= !"
        echo 2 !list!
        set "list=!list:{= !"
        echo 3 !list!
        set "list=!list:SerialNumberID= !"
        echo 4 !list!
        set "list=!list:UserFriendlyName= !"
        echo 5 !list!

        echo.
        SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
        (
            FOR %%y IN (!list!) DO (
                cmd /c exit %%y
                <nul set /p =!=exitcodeAscii!
            ) >> %SPT6%
        )
        Echo. >> %SPT6%
    )
)

So the actual problem was I tried to use variables like %var% instead of !var!

VarmintLP
  • 27
  • 9
  • 1
    you need [delayed expansion](https://stackoverflow.com/a/30284028/2152082) . As it's currently written, none of your variables gets updated inside the loop. – Stephan Jun 20 '18 at 09:30
  • Alright Stephan. Thank you very much for the final push. I somehow found out what caused all the trouble. It was not really an error with the EnableDelayedExpansion but on how I tried to use the variables. So the problem was solved by replacing the "%" with "!" so the variables were used properly. Found it by searching for that "Duplicate" – VarmintLP Jun 20 '18 at 09:57
  • 1
    yes, `setlocal enabledelayedexpansion`to *enable* it, `!var!` to actually *use* it. I agree: quite confusing, if you are not used to it. `:)` – Stephan Jun 20 '18 at 10:15
  • @Stephan that's my problem. I can work with batch files but I'm not really used to it. And now that I finally got the last bit to work I only need to see if everything works in the final script to get all the computer informations I need for my companies database. :D Took me long enough with many breaks and many programms and items to filter out (only the software lists haha) – VarmintLP Jun 20 '18 at 11:21

0 Answers0