Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· OpenAIAi2 WildChat-4.8M (AI2)

Make a batch file with an argument containing full path to an exe file. Use powershell…

  1. User

    Make a batch file with an argument containing full path to an exe file. Use powershell to read ProductVersion property of the exe into a variable

  2. Assistant

    To create a batch file that receives an argument with the full path to an .exe file and uses PowerShell to read the ProductVersion property of that .exe file and stores it into a variable, follow these steps:

    1. Open a text editor (e.g., Notepad).
    2. Copy and paste the following code into the text editor:
    @echo off
    setlocal
    
    REM Check if an argument was provided
    if "%~1"=="" (
        echo Please provide the full path to the EXE file as an argument.
        exit /b 1
    )
    
    REM Store the full path to the EXE in a variable
    set "exepath=%~1"
    
    REM Use PowerShell to get the ProductVersion property of the EXE
    for /f "delims=" %%a in ('powershell -Command "(Get-ItemProperty -Path '%exepath%').VersionInfo.ProductVersion"') do set "productversion=%%a"
    
    REM Display the ProductVersion
    echo ProductVersion: %productversion%
    
    REM End of batch file
    endlocal
    
    1. Save the file with a .bat extension, for example, GetExeVersion.bat.
    2. Execute your batch file from the command prompt or another batch file by providing the full path to an .exe file as an argument:
    GetExeVersion.bat "C:\Path\To\Your\File.exe"
    

    This script will output the ProductVersion of the specified .exe file, if available. The ProductVersion will also be stored in the productversion variable within the batch file after execution.

    In case the ProductVersion property is not available or any other error occurs, the output from PowerShell will be empty and the productversion variable will not be set.

  3. User

    Make a bat file for the following requirement. If the first argument is empty or missing, then delete registry value "MyValue" in key "HKLM\Software\MyCompany" if any. Otherwise store existing registry value in a variable, replace the value with that of the first argument and echo the old value from the variable.

  4. Assistant

    Below is a .bat file that accomplishes your requirements. The script first checks if the first argument is empty or missing. If so, it attempts to delete the registry value "MyValue" under the key "HKLM\Software\MyCompany". If an argument is provided, it will store the current value of the registry in a variable, replace it with the first argument's value, and then echo the old value.

    @echo off
    setlocal
    
    REM Check if first argument is empty or missing
    if "%~1"=="" (
        REM Delete registry value "MyValue" if it exists
        reg delete "HKLM\Software\MyCompany" /v "MyValue" /f
        if errorlevel 1 (
            echo The registry value "MyValue" could not be deleted or does not exist.
        ) else (
            echo The registry value "MyValue" has been deleted.
        )
        goto end
    )
    
    REM Store existing registry value "MyValue" in a variable
    for /f "tokens=3" %%a in ('reg query "HKLM\Software\MyCompany" /v "MyValue" 2^>nul') do (
        set "oldValue=%%a"
    )
    
    REM Check if the registry value "MyValue" existed and fetch it
    if defined oldValue (
        REM Output the old value
        echo Old value: %oldValue%
    ) else (
        REM If it didn't exist, say so
        echo The registry value "MyValue" does not exist. It will be created.
    )
    
    REM Replace the registry value "MyValue" with the first argument
    reg add "HKLM\Software\MyCompany" /v "MyValue" /d "%~1" /f
    
    :end
    endlocal
    

    Save this code to a file with the .bat extension, for example, UpdateRegistryValue.bat. You can run this batch file from the command line or another batch file in two ways:

    1. Without arguments to delete the value:
    UpdateRegistryValue.bat
    
    1. With an argument to replace the value:
    UpdateRegistryValue.bat "YourNewValueHere"
    

    Please take note that modifying the registry can affect your system and that you should be cautious when doing so. Also, note that in order to modify the HKLM hive, you likely need to run the batch file as an administrator.

  5. User

    Allow for oldValue to be able to contain space symbol. The for loop reading query result needs to be modified

  6. Assistant

    To correctly handle spaces in the oldValue, we need to modify the for loop to account for the fact that the registry query output may include spaces in the data part (after the REG_SZ or similar registry type descriptor).

    Here's the revised script:

    @echo off
    setlocal EnableDelayedExpansion
    
    REM Check if first argument is empty or missing
    if "%~1"=="" (
        REM Delete registry value "MyValue" if it exists
        reg delete "HKLM\Software\MyCompany" /v "MyValue" /f
        if errorlevel 1 (
            echo The registry value "MyValue" could not be deleted or does not exist.
        ) else (
            echo The registry value "MyValue" has been deleted.
        )
        goto end
    )
    
    REM Initialize oldValue to indicate it has not been fetched
    set "oldValue="
    
    REM Store existing registry value "MyValue" in a variable
    for /f "tokens=2*" %%a in ('reg query "HKLM\Software\MyCompany" /v "MyValue" 2^>nul') do (
        if "%%a"=="REG_SZ" set "oldValue=%%b"
        if "%%a"=="REG_MULTI_SZ" set "oldValue=%%b"
        if "%%a"=="REG_EXPAND_SZ" set "oldValue=%%b"
        if "%%a"=="REG_DWORD" set "oldValue=%%b"
        if "%%a"=="REG_QWORD" set "oldValue=%%b"
        if "%%a"=="REG_BINARY" set "oldValue=%%b"
        if "%%a"=="REG_FULL_RESOURCE_DESCRIPTOR" set "oldValue=%%b"
        if "%%a"=="REG_LINK" set "oldValue=%%b"
        if "%%a"=="REG_NONE" set "oldValue=%%b"
    )
    
    REM Check if oldValue was set
    if defined oldValue (
        REM Output the old value
        echo Old value: !oldValue!
    ) else (
        REM If it didn't exist, say so
        echo The registry value "MyValue" does not exist. It will be created.
    )
    
    REM Replace the registry value "MyValue" with the first argument
    reg add "HKLM\Software\MyCompany" /v "MyValue" /d "%~1" /f
    
    :end
    endlocal
    

    Please follow these changes made in the updated script:

    1. Enable delayed expansion to correctly handle values when variables are expanded at execution time instead of parse time.
    2. Use "tokens=2*" to get the registry type in %%a and everything after the type (the actual value) in %%b.
    3. Assign the oldValue only when a known registry type is matched.
    4. Use !oldValue! to echo the old value correctly when delayed expansion is enabled.

    Make sure to save the batch file with the necessary privileges to access and modify the HKLM hive, and you may still need to run the batch file as an administrator to avoid permission issues.

  7. User

    Allow for the fact that %1 may end with a backslash. In this case "reg add" fails because /f becomes part of the string "%~1"

  8. Assistant

    To address the issue with the potential trailing backslash in %1, we will need to sanitize the input before attempting to add it to the registry. Here's how you can modify the batch file script to properly handle this case:

    @echo off
    setlocal EnableDelayedExpansion
    
    REM Check if first argument is empty or missing
    if "%~1"=="" (
        REM Delete registry value "MyValue" if it exists
        reg delete "HKLM\Software\MyCompany" /v "MyValue" /f
        if errorlevel 1 (
            echo The registry value "MyValue" could not be deleted or does not exist.
        ) else (
            echo The registry value "MyValue" has been deleted.
        )
        goto end
    )
    
    REM Initialize oldValue to indicate it has not been fetched
    set "oldValue="
    
    REM Store existing registry value "MyValue" in a variable
    for /f "tokens=2*" %%a in ('reg query "HKLM\Software\MyCompany" /v "MyValue" 2^>nul') do (
        if "%%a"=="REG_SZ" set "oldValue=%%b"
        if "%%a"=="REG_MULTI_SZ" set "oldValue=%%b"
        if "%%a"=="REG_EXPAND_SZ" set "oldValue=%%b"
        if "%%a"=="REG_DWORD" set "oldValue=%%b"
        if "%%a"=="REG_QWORD" set "oldValue=%%b"
        if "%%a"=="REG_BINARY" set "oldValue=%%b"
        if "%%a"=="REG_FULL_RESOURCE_DESCRIPTOR" set "oldValue=%%b"
        if "%%a"=="REG_LINK" set "oldValue=%%b"
        if "%%a"=="REG_NONE" set "oldValue=%%b"
    )
    
    REM Check if oldValue was set
    if defined oldValue (
        REM Output the old value
        echo Old value: !oldValue!
    ) else (
        REM If it didn't exist, say so
        echo The registry value "MyValue" does not exist. It will be created.
    )
    
    REM Trim any trailing backslash from the argument
    set "newData=%~1"
    if "!newData:~-1!"=="\" set "newData=!newData:~0,-1!"
    
    REM Replace the registry value "MyValue" with the first argument, ensuring no trailing backslash
    reg add "HKLM\Software\MyCompany" /v "MyValue" /d "!newData!" /f
    
    :end
    endlocal
    

    Here's a summary of what's changed and why:

    • The input %1 is now stored in a new variable newData, and we use delayed expansion to check for and remove a trailing backslash.
    • If a trailing backslash exists in newData, it is removed by using string manipulation ${variable:~0,-1}.
    • When writing the new value to the registry, !newData! is now used instead of %~1, which ensures that the trailing backslash is not included and /f is properly recognized as a command switch by reg add.

    Remember to save the script and run it with administrative privileges if you are modifying the HKLM registry hive, as standard users do not typically have permissions to write to this hive.

  9. User

    Make a batch file. Calling another batch file and read the output string into a variable. The output may be empty or a string without spaces or a quoted string with spaces. Unquote the value of the variable if it is quoted.

  10. Assistant

    Here is a .bat file that calls another batch file, captures its output into a variable, and unquotes the value if it is quoted. Please note that this solution assumes that the called batch file outputs only one line, and the output is either empty, a string without spaces, or a properly quoted string with spaces.

    Save the following code as caller.bat:

    @echo off
    setlocal EnableDelayedExpansion
    
    REM Call the other batch file and capture the output into the variable output
    for /f "delims=" %%i in ('call other.bat') do set "output=%%i"
    
    REM Check if the first and last characters are quotes
    if not "!output!"=="" (
        if "!output:~0,1!"=="^"" if "!output:~-1!"=="^"" (
            REM Remove the leading and trailing quotes
            set "output=!output:~1,-1!"
        )
    )
    
    echo The unquoted output is: !output!
    
    endlocal
    

    The code above performs the following actions:

    1. Calls other.bat (the batch file you want to get the output from).
    2. Captures the output into the variable output.
    3. If output is not empty, checks if its first and last characters are quotes.
    4. Removes the quotes by using string manipulation if they are present.
    5. Echoes the unquoted output.

    Now, create a dummy other.bat that outputs a string for testing purposes:

    @echo off
    echo "This is a quoted string with spaces"
    REM Uncomment the line below to test different outputs
    REM echo SomeStringWithoutSpaces
    REM You can also test with no output by commenting the echo lines above
    

    To run your caller.bat script, ensure both caller.bat and other.bat are in the same directory or provide the correct path to other.bat within caller.bat. Then execute caller.bat from a command prompt.

    Keep in mind that this script assumes the output is in a very specific format. If the output can have other variations (such as multiple lines or nested quotes), the script would need to become more complex to handle those cases appropriately.