Make a batch file with an argument containing full path to an exe file. Use powershell…
- 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
- Assistant
To create a batch file that receives an argument with the full path to an
.exefile and uses PowerShell to read theProductVersionproperty of that.exefile and stores it into a variable, follow these steps:- Open a text editor (e.g., Notepad).
- 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- Save the file with a
.batextension, for example,GetExeVersion.bat. - Execute your batch file from the command prompt or another batch file by providing the full path to an
.exefile as an argument:
GetExeVersion.bat "C:\Path\To\Your\File.exe"This script will output the
ProductVersionof the specified.exefile, if available. TheProductVersionwill also be stored in theproductversionvariable within the batch file after execution.In case the
ProductVersionproperty is not available or any other error occurs, the output from PowerShell will be empty and theproductversionvariable will not be set. - 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.
- Assistant
Below is a
.batfile 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 endlocalSave this code to a file with the
.batextension, for example,UpdateRegistryValue.bat. You can run this batch file from the command line or another batch file in two ways:- Without arguments to delete the value:
UpdateRegistryValue.bat- 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
HKLMhive, you likely need to run the batch file as an administrator. - User
Allow for oldValue to be able to contain space symbol. The for loop reading query result needs to be modified
- Assistant
To correctly handle spaces in the
oldValue, we need to modify theforloop 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 endlocalPlease follow these changes made in the updated script:
- Enable delayed expansion to correctly handle values when variables are expanded at execution time instead of parse time.
- Use
"tokens=2*"to get the registry type in%%aand everything after the type (the actual value) in%%b. - Assign the
oldValueonly when a known registry type is matched. - 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
HKLMhive, and you may still need to run the batch file as an administrator to avoid permission issues. - 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"
- 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 endlocalHere's a summary of what's changed and why:
- The input
%1is now stored in a new variablenewData, 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/fis properly recognized as a command switch byreg add.
Remember to save the script and run it with administrative privileges if you are modifying the
HKLMregistry hive, as standard users do not typically have permissions to write to this hive. - The input
- 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.
- Assistant
Here is a
.batfile 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! endlocalThe code above performs the following actions:
- Calls
other.bat(the batch file you want to get the output from). - Captures the output into the variable
output. - If
outputis not empty, checks if its first and last characters are quotes. - Removes the quotes by using string manipulation if they are present.
- Echoes the unquoted output.
Now, create a dummy
other.batthat 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 aboveTo run your
caller.batscript, ensure bothcaller.batandother.batare in the same directory or provide the correct path toother.batwithincaller.bat. Then executecaller.batfrom 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.
- Calls