sylphaxiom
sylphaxiom

Reputation: 161

IF NOT EXIST causing failure in batch file

I'm trying to execute a script that pulls a bunch of information for me. It was working until I removed my pause break points and re-introduced the IF NOT EXIST (which was working before I added the CPU get name and the part that pulls RAM info). Now the file will not run at all.

Using pause and echo, I have managed to find out that the script stops at IF NOT EXIST. If I add a pause between %location% and ( the script pauses then starts working from there. I have no idea why this is and I need this to run as silently as possible with NO interaction by the user.


REM Set save location

REM set location="\\colfs1\public\inventory\%computername%.txt"
SET location="c:\users\jpell\desktop\%computername%.txt"

REM If file is not there create it and populate info
pause

IF NOT EXIST %location% (
pause
    systeminfo | findstr /i /b /c:"host name" /c:"os name" /c:"os version" /c:"original" /c:"system manu" /c:"system model" >> %location%
pause
    @echo.  >> %location%
pause
    wmic /append:%location% bios get serialnumber
pause
    wmic /append:%location% computersystem get systemtype
pause
    wmic /append:%location% cpu get name
pause
REM Script to pull RAM info taken from StackOverflow

    Set "WMIC_TOTMEM=wmic ComputerSystem get TotalPhysicalMemory /format:Value"
    Set "WMIC_Capacity=wmic memorychip get capacity /format:Value"
    Set "CAP=Capacity"
    Set "TOT=TotalPhysicalMemory"
    Call :GetTOTMEM %TOT% TotalPhysicalMemory
    Call :GetCapacityMem %CAP% Capacity
    Call :Convert %TotalPhysicalMemory% TotalPhysicalMemory_Converted
    Call :Convert %Capacity% Capacity_Converted
    echo   -------------------------------------------------
    echo     TotalPhysicalMemory = %TotalPhysicalMemory%
    echo     Memorychip Capacity = %Capacity%
    echo   -------------------Converted---------------------
    echo TotalPhysicalMemory = %TotalPhysicalMemory_Converted% >> %location%
    echo Memorychip Capacity = %Capacity_Converted%  >> %location%
    echo   -------------------------------------------------
    GOTO :Continue
pause
    ::-------------------------------------------------------
    :GetCapacityMem
    FOR /F "tokens=2 delims==" %%I IN (
      '%WMIC_Capacity% ^| find /I "%~1" 2^>^nul'
    ) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
    Exit /b
    ::-------------------------------------------------------
    :GetTOTMEM
    FOR /F "tokens=2 delims==" %%I IN (
      '%WMIC_TOTMEM% ^| find /I "%~1" 2^>^nul'
    ) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
    Exit /b
    ::-------------------------------------------------------
    :Convert
    Set "VBS=%Temp%\%Random%.vbs"
    (
        echo wscript.echo Convert("%~1"^)
        echo 'Function to format a number into typical size scales
        echo Function Convert(iSize^)
        echo    aLabel = Array("bytes", "KB", "MB", "GB", "TB"^)
        echo    For i = 0 to 4
        echo        If iSize ^> 1024 Then
        echo            iSize = iSize / 1024
        echo        Else
        echo            Exit For
        echo        End If
        echo    Next
        echo    Convert = Round(iSize,2^) ^& " " ^& aLabel(i^)
        echo End Function
    )>"%VBS%"
    for /f "delims=" %%a in ('Cscript //NoLogo "%VBS%"') do set "%2=%%a" 
    Del "%VBS%"
    Exit /b
    ::-------------------------------------------------------

REM Continue the script after pulling RAM

    :Continue:
    @echo. >> %location%
    @echo Current User: >> %location%
    whoami >> %location%
    @echo.  >> %location%
    @echo Forticlient or Sophos: >> %location%
    wmic product get version,vendor | findstr /i /c:"forti" /c:"sophos" >> %location%
pause
    )
pause

This script pulls a bunch of hardware information from a computer and puts it into a text file. This file is then used for inventory purposes. The information is populating with the pause in there and when the IF NOT EXIST is REMed out.

Upvotes: 0

Views: 66

Answers (1)

user6811411
user6811411

Reputation:

Yes, follow your own comment and reverse the logic:

if exist "%location%" goto :Jumpover

to avoid :labels and pseudo labels ::-- inside (code blocks)

All your pause commands for debugging could also be put in a variable %dbg%
and switched on/off on demand to not have to change much of the code.

Untested:


REM Set save location

REM set location="\\colfs1\public\inventory\%computername%.txt"
SET location="c:\users\jpell\desktop\%computername%.txt"

REM If file is not there create it and populate info
set "dbg=pause"
::set "dbg=Rem"

%dbg%

IF EXIST "%location%" goto :JumpOver

%dbg%
systeminfo | findstr /i /b /c:"host name" /c:"os name" /c:"os version" /c:"original" /c:"system manu" /c:"system model" >> %location%
%dbg%
@echo.  >> %location%
%dbg%
wmic /append:%location% bios get serialnumber
%dbg%
wmic /append:%location% computersystem get systemtype
%dbg%
wmic /append:%location% cpu get name
%dbg%
REM Script to pull RAM info taken from StackOverflow

Set "WMIC_TOTMEM=wmic ComputerSystem get TotalPhysicalMemory /format:Value"
Set "WMIC_Capacity=wmic memorychip get capacity /format:Value"
Set "CAP=Capacity"
Set "TOT=TotalPhysicalMemory"
Call :GetTOTMEM %TOT% TotalPhysicalMemory
Call :GetCapacityMem %CAP% Capacity
Call :Convert %TotalPhysicalMemory% TotalPhysicalMemory_Converted
Call :Convert %Capacity% Capacity_Converted
echo   -------------------------------------------------
echo     TotalPhysicalMemory = %TotalPhysicalMemory%
echo     Memorychip Capacity = %Capacity%
echo   -------------------Converted---------------------
echo TotalPhysicalMemory = %TotalPhysicalMemory_Converted% >> %location%
echo Memorychip Capacity = %Capacity_Converted%  >> %location%
echo   -------------------------------------------------
GOTO :Continue
%dbg%
::-------------------------------------------------------
:GetCapacityMem
FOR /F "tokens=2 delims==" %%I IN (
  '%WMIC_Capacity% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::-------------------------------------------------------
:GetTOTMEM
FOR /F "tokens=2 delims==" %%I IN (
  '%WMIC_TOTMEM% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::-------------------------------------------------------
:Convert
Set "VBS=%Temp%\%Random%.vbs"
(
    echo wscript.echo Convert("%~1"^)
    echo 'Function to format a number into typical size scales
    echo Function Convert(iSize^)
    echo    aLabel = Array("bytes", "KB", "MB", "GB", "TB"^)
    echo    For i = 0 to 4
    echo        If iSize ^> 1024 Then
    echo            iSize = iSize / 1024
    echo        Else
    echo            Exit For
    echo        End If
    echo    Next
    echo    Convert = Round(iSize,2^) ^& " " ^& aLabel(i^)
    echo End Function
)>"%VBS%"
for /f "delims=" %%a in ('Cscript //NoLogo "%VBS%"') do set "%2=%%a" 
Del "%VBS%"
Exit /b
::-------------------------------------------------------

REM Continue the script after pulling RAM

:Continue:
@echo. >> %location%
@echo Current User: >> %location%
whoami >> %location%
@echo.  >> %location%
@echo Forticlient or Sophos: >> %location%
wmic product get version,vendor | findstr /i /c:"forti" /c:"sophos" >> %location%
%dbg%

:JumpOver
%dbg%```

Upvotes: 1

Related Questions