Reading registry value's data with spaces - batch-file

I am making a program. When installed, It will run a batch file which needs to know where the application folder is.
The installation wizard software I am using creates a registry key that represents the program's path, but "Program Files" has a space in it, so the batch output will be "C:\Program"
My current script is:
#echo off
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_LOCAL_MACHINE\Software\MarksRTZ\AV"
set VALUE_NAME=DataPath
FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)
if defined ValueName (
echo data "%ValueValue%"
echo name "%ValueName%"
echo type "%ValueType%"
) else (
echo Not found
)
The DataPath value is set to [APPDIR]\data on installation, [APPDIR] being the location the user selected.
But like I said, that script will always output C:\Program if [APPDIR] was set to something like C:\Program Files (x86)\MarksRTZ\AV\ in the installer (Which is actually the default)
How can I fix this?
I will also note; the batch file isn't the real program, It's going to be the simple script that launches the program in the correct working directory.

Next code snipped should work with tokens=1-2* and even with setlocal DISABLEEXTENSIONS:
rem ensure %ValueName% is not visible as it could be defined already!
set "ValueName="
FOR /F "usebackq skip=2 tokens=1-2*" %%A IN (
`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set "ValueName=%%A"
set "ValueType=%%B"
set "ValueValue=%%C"
)
if defined ValueName (
echo data "%ValueValue%"
echo name "%ValueName%"
echo type "%ValueType%"
) else (
echo Not found
)
Resources:
FOR /F Loop command: against the results of another command;
SETLOCAL Set options to control the visibility of environment variables in a batch file

Related

How to get a specific value from registry file into variable as a batch script

First post here
I am working on deploying a specific .dll file (iprint.dll) into the end users IBM folder, so they are able to print. I am trying to make the script so it checks from the registry path, since the IBM Notes location can be installed in different locations. The path im looking at is the following.
KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Lotus\Notes\9.0
VALUE_NAME=Path
If i do reg query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Lotus\Notes\9.0" /v "Path"
I get the following : Path REG_SZ C:\Lotus\Notes\
But i only want the data value C:\Lotus\Notes\
My end goal is to take the value and place in a variable that i can copy/install the iprint.dll to.
I am not an expert in this at all and what everything ive learned is from google searching.
So far i got the following, which is failing, what am i doing wrong and any suggestions?
#echo on
setlocal
set __COMPAT_LAYER=RunAsInvoker
set SEE_MASK_NOZONECHECKS=1
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Lotus\Notes\9.0"
set VALUE_NAME=Path
FOR /F "usebackq skip=4 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)
if defined ValueName (
#echo Value Name = %ValueName%
#echo Value Type = %ValueType%
#echo Value Value = %ValueValue%
) else (
#echo %KEY_NAME%\%VALUE_NAME% not found.
)
copy "iPrint.dll" %ValueValue%
endlocal

Find a file and then use its name as a variable Command Prompt

How do I look for a file and assign file name to a variable?
Example:
Let's say I am looking for a file in below location. File name constantly changes and only way to pick up the right file is by looking for today's date in file name string:
X:\VoyagerBackups\PickupLocation\tsiuiolou_live_Full_PCV048DB42_201812140000.Lts.bak
Here's what I have so far in my .bat file. Execution works as expected if I hard-code file name:
echo on
Rem Determine date
Set mm1=%date:~4,2%
Set dd1=%date:~7,2%
Set yyyy1=%date:~10,4%
Set rundate1=%yyyy1%%mm1%%dd1%
Set executefile1=tsiuiolou_live_Full_PCV048DB42_%rundate1%0000.Lts.bak
set downloadfile=tsiuiolou_live_Full_PCV048DB42_%rundate1%0000.bak
extractor64.exe -F %executefile1% -E %downloadfile%
move %executefile1% X:\VoyagerBackups\BackupFiles
Don't use the locale/user settings dependant %date% variable.
Use either wmic or PowerShell instead, they both have more stable methods.
:: Q:\Test\2018\12\14\SO_53787816.cmd
#Echo off
for /f "usebackq" %%A in (`
powershell -NoP -C "(Get-Date).ToString('yyyyMMdd')"
`) Do Set "Today1=%%A"
Echo PowerShell today:%Today1%
for /f "tokens=1-3 delims=.+-" %%A in (
'wmic os get LocalDateTime^|findstr ^^[0-9]'
) do Set "Today2=%%A"
Set "Today2=%Today2:~0,8%"
Echo wmic today:%Today2%
Set "Base=X:\VoyagerBackups\PickupLocation\"
PushD "%Base%" || (Echo can't find base:%Base%&Pause&Goto :Eof)
For /f "delims=" %%A in ('Dir /B "*%Today1%*" ') Do (
Echo found file:%%~fA
)
PopD

Need help getting the regkey entry value in script

I have a batch script that will clean out temp files and all that but I need assistance getting a path from the registry into the script. Specifically the Outlook temp file regkey entry. I have the key path but I do not know how to grab the directory value from the registry and set it where I can then delete the temp files. I probably do not make an since but I just started doing scripts and I guess I just suck lol. I have done a reg query and it shows the path but obviously set /p name= or anything around that wont just set %name% as a path to where I could then do my delete script and *.*
Any help would be much appreciated!!!!!
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\14.0\Outlook\Security"
set VALUE_NAME=OutlookTemp
pause
FOR /F "usebackq skip=4 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% ) DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)
pause
if defined ValueName (
#echo Value Name = %ValueName%
#echo Value Type = %ValueType%
#echo Value Value = %ValueValue%
) else (
#echo %KEY_NAME%\%VALUE_NAME% not found.
)
pause
This is something I have tried from another post but originally I was using this to make a file to the desktop and was going to parse the file for the temp directory then delete the files in the temp folder. I also just paused it out to see what was working and what was not.
START /W REGEDIT /E C:\Users\"username"\Desktop "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\14.0\Outlook\Security"
And that makes a reg file to the desktop and I tried finding on how to parse the file and found a post that used
FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "KeyName" /v ValueName') DO SET Variable=%%B
But that didn't work for me or I am doing something incorrect. Prolly just me but lol
If you have the following in your batch file then the location you require could be used as "%ValueData%"
FOR /F "TOKENS=2*" %%I IN ('REG QUERY "HKCU\SOFTWARE\Microsoft\Office\14.0\Outlook\Security" /V OutlookSecureTempFolder') Do SET "ValueData=%%J"
You could then use the variable for your deletion thus:
DEL /S /Q "%ValueData%\*.*"

batch script was unexpected this time

after running this script shows somesoftware was unexpected this time i am using windows 7 64 bit. trying to get installed location of software using registry. if i echo THE_NAME then i get proper installed location but script exits in if else says somesoftware was unexpected this time
setlocal ENABLEEXTENSIONS
set KEY_NAME=HKEY_CURRENT_USER\Software\somesoftware
set VALUE_NAME=InstallDirectory
SET THEME_NAME=
FOR /f "usebackq tokens=2,*" %%a in (`REG QUERY "HKEY_CURRENT_USER\Software\somesoftware" /v InstallDirectory`) DO (
SET THEME_NAME=%%b
)
echo %THEME_NAME%
if defined THEME_NAME (
set PathValue= %THEME_NAME%
) else (
echo %KEY_NAME%\%VALUE_NAME% not found.
)
Your script does not make use of KEY_NAME or VALUE_NAME, and this might be just how you trimmed your example down.
I modified your script as shown below for testing, and it appeared to work as I expected.
setlocal ENABLEEXTENSIONS
set KEY_NAME=HKEY_CURRENT_USER\Software\%1
set VALUE_NAME=InstallDirectory
SET THEME_NAME=
FOR /f "usebackq tokens=2,*" %%a in (`REG QUERY "%KEY_NAME%" /v %VALUE_NAME%`) DO (
SET THEME_NAME=%%b)
echo THEME_NAME=%THEME_NAME%
if NOT ""=="%THEME_NAME%" (
set PathValue=%THEME_NAME%
echo PathValue=%PathValue%
) else (
echo %KEY_NAME%\%VALUE_NAME% not found.
)
NOTE: I changed the IF test to just test for empty string, an echo for my own debugging, I used %1 so I could test...
You will want to use this as a guide to inform your final script.

Determining OS version via Scripting

I found this script on another site but I can not get it to work and I don't know batch scripting that well
Set objWshShell = WScript.CreateObject("WScript.Shell")
strOSVersion = objWshShell.RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentVersion")
If strOSVersion = "5.1" Then
Debug.WriteLine("Windows XP")
Rem Insert Windows XP Statements
ElseIf strOSVersion = "6.0" Then
Debug.WriteLine("Windows Vista")
Rem Insert Windows Vista Statements
ElseIf strOSVersion = "6.1" Then
Debug.WriteLine("Windows 7")
Rem Insert Windows 7 Statements
End If
now if I run this I get the error on the second line
'strOSVersion' is not recognized as an internal or external command
operable program or batch file.
= was unexpected at this time.
I do not know why
It's a VB script. You can save in a file named like test.vbs
Then open a command prompt, change directory to where you saved the file. At the prompt type cscript test.vbs.
Before that, I changed the Debug.WriteLine calls to WScript.Echo instead.
This is a Batch Script (.bat) that I've put together and use often for determining OS.
#ECHO OFF
SetLocal
REM --------> EDIT BELOW <--------
REM Edit below if you would like to audit the pc's you run this on and store the information in a file, either T or F (for True or False)
set storeValue=T
REM Edit below the location on a network drive that you can write to
set sharePath=\\servername\sharepath
REM -----> DO NOT EDIT BELOW <-----
IF NOT EXIST C:\Temp MD C:\Temp
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" | findstr ProductName>C:\temp\osver.txt
set osver=
set tempLoc=C:\Temp
FOR /F "tokens=3* delims= " %%I IN (%tempLoc%\OSver.txt) DO SET osVer=%%I %%J
echo.-----------------------------------------
echo. You are running: %osVer%
echo.-----------------------------------------
IF '%storeValue%'=='F' goto end
IF '%storeValue%'=='T' goto storeValue
:storeValue
ipconfig |findstr IPv4>c:\temp\ipadd.txt
REM FOR /F "tokens=12* delims=." %%A IN (%tempLoc%\IPAdd.txt) DO SET IPAdd=%%A.%%B
FOR /F "tokens=2* delims=:" %%A IN (%tempLoc%\IPAdd.txt) DO SET IPAdd=%%A
IF EXIST %sharePath%\PC_Audit_List.txt goto audit
echo.PC Audit List>%sharePath%\PC_Audit_List.txt
echo.------------------------------------------------------------->>%sharePath%\PC_Audit_List.txt
goto audit
:audit
echo.%computername% - %IPAdd% - %osVer%>>%sharePath%\PC_Audit_List.txt
goto end
:end
IF EXIST %tempLoc%\OSver.txt del %tempLoc%\OSver.txt /f /q
IF EXIST %tempLoc%\IPAdd.txt del %tempLoc%\IPAdd.txt /f /q
EndLocal
pause
exit
I'm sure this will suit your needs, I've included an option for you to write the IP, Name and then the Version into a file.
#echo off
setlocal EnableDelayedExpansion
::Identify OS
for /F "delims=" %%a in ('ver') do set ver=%%a
set Version=
for %%a in (95=95 98=98 ME=ME NT=NT 2000=2000 5.1.=XP 5.2.=2003 6.0.=Vista 6.1.=7 6.2.=8 6.3=8.1) do (
if "!Version!" equ "this" (
set Version=Windows %%a
) else if "!ver: %%a=!" neq "%ver%" (
set Version=this
)
)
::Identify bit
if exist "%SYSTEMDRIVE%\Program Files (x86)" (
set Type=64 bit
) else (
set Type=32 bit
)
::Display result
echo %Version% %Type%
echo/
pause
Copied from: http://www.dostips.com/forum/viewtopic.php?f=3&t=4387

Resources