Incorrect script batch RAM quantity

Discussion in 'Scripting' started by MrNico98, Feb 5, 2024.

  1. MrNico98

    MrNico98 MDL Junior Member

    May 9, 2023
    56
    7
    0
    Hi everyone, I'm trying to make a script to recognize cpu, hdd/ssd and amount of ram...I have a problem with the RAM code, as it behaves differently on different PCs and I don't know how to solve it

    Can anyone help me?

    I have attached the script and the photo where the RAM problem is located
     

    Attached Files:

  2. mxman2k

    mxman2k MDL Developer

    Jun 20, 2007
    6,395
    21,406
    210
    Try changing the parts to the green color ie remove the quotes around the values, there is a glitch in batch that not seem to like comparing numeric values when enclosed. As you have already done a precheck for the totalMemoryGB for being non defined then that will hopefully prevent a crash/error.

    Code:
    if not defined totalMemoryGB (
        powershell -Command "Write-Host 'Non trovo la RAM' -ForegroundColor Red; exit" && timeout /t 4 >nul
    ) else (
        if %totalMemoryGB% LEQ 3 (
            set "ram=-"
            powershell -Command "Write-Host 'RAM insufficiente per i nuovi SO Win10/11, requisito minimo 4gb' -ForegroundColor Red; exit" && timeout /t 4 >nul
        ) else if %totalMemoryGB% EQU 4 (
            set "ram=+"
            powershell -Command "Write-Host 'RAM minima per i nuovi SO Win10/11, espandila se possibile' -ForegroundColor DarkYellow; exit" && timeout /t 4 >nul
        ) else if %totalMemoryGB% GEQ 5 (
            set "ram=ok"
            powershell -Command "Write-Host 'RAM sufficiente per i nuovi SO Win10/11  ' -ForegroundColor Green; exit" && timeout /t 4 >nul
        )
    )
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    17,281
    91,251
    340
    set totalMemoryGB=0 on top and compare to zero is more safe than if not defined without quotes
     
  4. MrNico98

    MrNico98 MDL Junior Member

    May 9, 2023
    56
    7
    0
    I set it up as you suggested and it seems to work...can I ask why the "" had to be removed?
     
  5. mxman2k

    mxman2k MDL Developer

    Jun 20, 2007
    6,395
    21,406
    210
    Its something that the command processor seems to get confused about.

    I fell foul to that string/numeric comparison in my early days of batch programming.

    The variable needs to be checked for non defined or set as 0 otherwise the batch script will crash if the variable is unset.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. MrNico98

    MrNico98 MDL Junior Member

    May 9, 2023
    56
    7
    0
    thx u so much,
    last question, is there a way to extract the processor number piece?

    I attach the photo, in my case I would like it to set for example the value numberCPU:6200 or 6gen
     

    Attached Files:

    • cpu.png
      cpu.png
      File size:
      4.7 KB
      Views:
      17
  7. mxman2k

    mxman2k MDL Developer

    Jun 20, 2007
    6,395
    21,406
    210
    As the processor string is not consistent i not sure if that possible without a lot of if checks and a database of all cpus intel/amd.

    I have tried to get that information in my project but failed many times as my databases are always out of date so i gave up.

    Same with motherboard chipset versions and cpu socket types, m$ not seem to keep their internal data bases up to date.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,189
    6,018
    150
    with regex ... you can do lot of things ...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. MrNico98

    MrNico98 MDL Junior Member

    May 9, 2023
    56
    7
    0
    can u explain?
     
  10. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,189
    6,018
    150
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    17,281
    91,251
    340
    with quotes, it's compared as strings bit by bit, so 1 is smaller than 9
    Code:
    if "16" GTR "9" echo yes
    
    if 16 GTR 9 echo yes
     
  12. 12 lb Turkey

    12 lb Turkey MDL Member

    Nov 24, 2022
    121
    66
    10
    Is there a reason why you keep calling PS one-liners from a CMD batch? Why not write a single PS script?

    upload_2024-2-6_22-46-24.png
    View attachment 66498
     

    Attached Files:

  13. MrNico98

    MrNico98 MDL Junior Member

    May 9, 2023
    56
    7
    0