I have lifted bits and pieces of the code from multiple places and have made the below code.
The functionality of this code is basically to read all softwares and save it in a csv format so I can use it further. I want the code to save output on a folder which is on another computer which is shared on the domain. Below is the code:
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set OutputFile=C:\Domo\%computername%.csv
echo "Computer","Key","Version","Name","Publisher">"%OutputFile%"
call :getsw "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall"
call :getsw "HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall"
call :getsw "HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
ENDLOCAL
exit /b
:getsw
for /F "delims=" %%g in ('reg query "%~1" 2^>NUL') do (
set "_swRegKey=%%~g"
set "_swName="
set "_swVers="
set "_swPub="
for /F "tokens=1,2,*" %%G in ('
reg query "%%~g" /V DisplayName 2^>NUL ^| findstr /I "DisplayName"
') do set "_swName=%%~I"
for /F "tokens=1,2,*" %%G in ('
reg query "%%~g" /V DisplayVersion 2^>NUL ^| findstr /I "DisplayVersion"
') do set "_swVers=%%~I"
for /F "tokens=1,2,*" %%G in ('
reg query "%%~g" /V Publisher 2^>NUL ^| findstr /I "Publisher"
') do set "_swPub=%%~I"
call :echosw
)
exit /b
:echosw
if "!_swName!!_swVers!"=="" (
echo "%COMPUTERNAME%","unknown","!_swRegKey!","","">>"%OutputFile%"
) else (
echo "%COMPUTERNAME%","!_swRegKey!","!_swVers!","!_swName!","!
_swPub!">>"%OutputFile%"
)
exit /b
The problem is when I change the location of outputfile (Line 3) from C:\Domo\%computername%.csv to a network location like \Win-p0sck5u2hqd\csv\%computername%.csv
This gives an error saying access is denied while executing the batch file. Is there anything I am missing. please help me understand why it is not working and is there any solution.
This location \Win-p0sck5u2hqd\ is accessible from my run command.
Related
I have a registry key created by InnoSetup during installation. This key contains two information :
DisplayName
QuietUninstallString
I don't know what is the full name of my Key. The only informations I have is the path :
"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
and the value of the key "DisplayName" (MyApplicationName).
What I need to do, is to retrieve the "QuietUninstallString" value, which is supposed to be a path with "/SILENT" at the end of the path.
Right now I wrote this batch file :
#echo off
setlocal enableextensions disabledelayedexpansion
set tosearch=%1
for /f "tokens=3" %%a in ('
REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /s /f %tosearch% ^| findstr "QuietUninstallString"
') do set "value=%%a"
echo %value%
And I call it like this :
test.bat MyApplicationName
Problem is, when I run this file, it return :
"C:\Program
While I want this to be returned :
"C:\Program Files (x86)\MyFolder\MyApplicationName\unins000.exe" /SILENT
Is there a better way to retrieve the information with my batch file ?
Based upon what you've posted, you should be able to replace your entire code with this:
#For /F "EOL= Delims=" %%G In ('%SystemRoot%\System32\reg.exe Query
"HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" /S /F
"%~1" /E /D 2^>NUL') Do #For /F "Tokens=2,*" %%H In (
'%SystemRoot%\System32\reg.exe Query "%%G" /V "QuietUninstallstring" 2^>NUL'
) Do #Echo(%%I
Or if the intention is to actually run the uninstall string:
#For /F "EOL= Delims=" %%G In ('%SystemRoot%\System32\reg.exe Query
"HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" /S /F
"%~1" /E /D 2^>NUL') Do #For /F "Tokens=2,*" %%H In (
'%SystemRoot%\System32\reg.exe Query "%%G" /V "QuietUninstallstring" 2^>NUL'
) Do #Start "" %%I
Use tokens=2,* and set "value=%%b". See for /? from the prompt for documentation
I am trying to make a batch file, that finds a specific interface (their name vary from computer to computer = query). To find the interface I use a specific IP that is under that interface as a value.
The code here can find the interface in question based on the IP i insert into "IP-ADDRESS".
But my ultimate goal is to based on this search add two registry values into this interface, and therefore I need the path.
How do I get the path into a variable based on the search below?
for /f "tokens=3*" %%a in ('reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\ /s /f "IP-ADDRESS"') do set Myvar=%%b
ECHO %Myvar%
PAUSE
Thank you in advance!
The following batch-file returns here:
> Q:\Test\2018\11\16\SO_53340832.cmd
IPADDRESS:192.168.56.1 on interface:{4fe80965-dda5-466a-801d-14937fd3829c}
It uses "tokens=1,2*" and /v IPADDRESS without dash.
:: Q:\Test\2018\11\16\SO_53340832.cmd
#Echo off & SetLocal EnableExtensions EnableDelayedExpansion
Set "Key=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"
Set "Val=IPADDRESS"
For /f "tokens=1,2*" %%A in (
'reg query "%Key%" /s /v "%Val%" ^| findstr /i "^HKEY %Val%"'
) Do if /i "%%A" neq "%Val%" (rem must be HKEY
Set "Interface=%%A"
) Else (
Echo %VAL%:%%C on interface:!Interface:%Key%\=!
)
Here's one possibility for you:
#Echo Off
Set "RKP=HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"
Set "RVD=172.26.193.3"
Set "RKC="
For /F "Delims=}" %%A In ('Reg Query "%RKP%" /S /F "%RVD%" /D 2^>Nul'
) Do If Not Defined RKC Set "RKC=%%A}"
If Not Defined RKC Exit /B
Rem show the variable and value for five seconds
Set RKC
Timeout 5 >Nul
FOR /F "tokens=2*" %%A IN (
'REG QUERY "HKLM\Software\EA GAMES\Need for Speed Most Wanted" /v InstallDir'
) DO (set HBMU=%%B)
GOTO END
how I can add If %ERRORLEVEL% == 1 goto CLOSE to my code? because when i put under ) DO (set HBMU=%%B) it does not work.
Try this, after completing the rest of the registry key on line 2:
For /F "Skip=1 Tokens=2*" %%A In (
'Reg Query "HKLM\SOFTWARE\..." /V "InstallDir" 2^>Nul'
) Do Set "HBMU=%%B"
You cannot redirect both to Nul otherwise nothing will be output from the loop as variable %%B
Edit
Here's a complete script which should do what your commented code was supposed to do. (As long as your software definitely places it's information in the registry according to the Operating System architecture).
#Echo Off
Set "EAG=EA GAMES\Need for Speed Most Wanted"
Set "RKM=\"
Reg Query "HKLM\Hardware\Description\System\CentralProcessor\0"^
/V "Identifier" 2>Nul|Find /I "x86">Nul||Set "RKM=\Wow6432Node\"
For /F "EOL=HTokens=2*" %%A In ('Reg Query "HKLM\Software%RKM%%EAG%" /V^
"InstallDir" 2^>Nul') Do Start "" "%%~B\unins000.exe"
Pause
I am very new to batch just learnt a few. I agree to having shamelessly lifted this code from a website. This is the code I want for displaying list of installed software, however there is just one problem, the version and the softwares are being displayed in different lines. How can I have both in the same line?
Ex: If you run the batch you will see the versions at the beginning and then the softwares.
Any help would be greatly appreciated. Thanks in advance.
#echo off
If Exist C:\Final.txt Del C:\Final.txt
regedit /e C:\regexport.txt "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall"
regedit /e C:\regexport2.txt "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall"
regedit /e C:\regexport3.txt "HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
find "DisplayName" C:\regexport.txt > C:\regprogs.txt
find "DisplayName" C:\regexport2.txt >> C:\regprogs.txt
find "DisplayName" C:\regexport3.txt >> C:\regprogs.txt
for /f "tokens=2 delims==" %%a in (C:\regprogs.txt) do echo %%~a >> C:\installedprogs.txt
find "DisplayVersion" C:\regexport.txt > C:\regprogs.txt
find "DisplayVersion" C:\regexport2.txt >> C:\regprogs.txt
find "DisplayVersion" C:\regexport3.txt >> C:\regprogs.txt
for /f "tokens=2 delims==" %%a in (C:\regprogs.txt) do echo %%~a >> C:\installedprogs.txt
del C:\regexport.txt
del C:\regexport2.txt
del C:\regexport3.txt
del C:\regprogs.txt
sort C:\installedprogs.txt > C:\alles.txt
del C:\installedprogs.txt
:: script om alle dubbele lijnen eruit te gooien
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL EnABLEDELAYEDEXPANSION
REM -- Prepare the Prompt for easy debugging -- restore with prompt=$p$g
prompt=$g
rem The finished program will remove duplicates lines
:START
set "_duplicates=TRUE"
set "_infile=C:\alles.txt"
set "_oldstr=the"
set "_newstr=and"
call :BATCHSUBSTITUTE %_infile% %_oldstr% %_newstr%
pause
goto :SHOWINTELL
goto :eof
:BATCHSUBSTITUTE
type nul> %TEMP%.\TEMP.DAT
if "%~2"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %1|find /n /v """') do (
set "_line=%%B"
if defined _line (
if "%_duplicates%"=="TRUE" (
set "_unconverted=!_line!"
set "_converted=!_line:"=""!"
FIND "!_converted!" %TEMP%.\TEMP.DAT > nul
if errorlevel==1 (
>> %TEMP%.\TEMP.DAT echo !_unconverted!
)
)
) ELSE (
echo(>> %TEMP%.\TEMP.DAT
)
)
goto :eof
:SHOWINTELL
#echo A|move %TEMP%.\TEMP.DAT C:\allesnietdubbel.txt
del C:\alles.txt
::Alle lijnen weggooien waar 'KB' in voor komt
type C:\allesnietdubbel.txt | findstr /V KB > C:\drivers\Final.txt
goto :eof
exit
Try next approach. Edited - saves output to a csv file (rfc4180 compliant):
#ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "_csvfile=%TEMP%\%COMPUTERNAME%_39082171.csv" change to match your needs
> "%_csvfile%" (
rem (facultative, optional) CSV header
echo "version","displayname","comment"
call :getsw "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall"
call :getsw "HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall"
call :getsw "HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
)
ENDLOCAL
goto :eof
:getsw
for /F "delims=" %%g in ('reg query "%~1" 2^>NUL') do (
set "_swRegKey=%%~g"
set "_swName="
set "_swVers="
for /F "tokens=1,2,*" %%G in ('
reg query "%%~g" /V DisplayName 2^>NUL ^| findstr /I "DisplayName"
') do set "_swName=%%~I"
for /F "tokens=1,2,*" %%G in ('
reg query "%%~g" /V DisplayVersion 2^>NUL ^| findstr /I "DisplayVersion"
') do set "_swVers=%%~I"
call :echosw
)
goto :eof
:echosw
SETLOCAL EnableDelayedExpansion
if "%_swName%%_swVers%"=="" (
rem comment up next ECHO command if you don't require such information
ECHO "","","unknown !_swRegKey:HKEY_LOCAL_MACHINE=HKLM!"
) else (
echo "!_swVers!","!_swName!",""
)
ENDLOCAL
goto :eof
Sample output (largely narrowed down for demonstration here):
==> D:\bat\SO\39082171.bat
==> type "%TEMP%\%COMPUTERNAME%_39082171.csv"|findstr /I "comment KB unknown"
"version","displayname","comment"
"","","unknown HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager"
"12.0.31101","Visual Studio 2013 Update 4 (KB2829760)",""
"12.0.30112","Update for Microsoft Visual Studio 2013 (KB2932965)",""
"1","Update for (KB2504637)",""
==>
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(helpful particularities) Windows CMD Shell Command Line Syntax
(%~G, %~1 etc. special page) Command Line arguments (Parameters)
(special page) EnableDelayedExpansion
(2>NUL, 2>&1, | etc. special page) Redirection
(2^>NUL, ^| etc.) Escape Characters, Delimiters and Quotes
I am currently trying to use reg query commands to find the data value "teststring" in a value with a random name in HKCU\Software\random characters. I want the script to find the teststring value and then delete the parent key HKCU\Software\random characters. I have tried various ways with scripting and this is what I have so far, however I keep getting syntax errors and can't get reg query to find what I need it to:
#echo off
setlocal enabledelayedexpansion
set Key="HKCU\Software"
set STRING="teststring"
for /f "delims=" %%a in ('reg.exe query "%Key%" /f "%STRING%" /d /s') do (
call :GetValueName Value "%%a"
ECHO reg.exe delete "%Key%" /v "!Value!" /f
)
goto :eof
:GetValueName
set Return=
for %%a in (%~2) do (
if "%%a"=="REG_SZ" (
set %1=!Return:~1!&goto :eof
) else (
set Return=!Return! %%a
)
)
:eof
Here's a simpler way to do it.
Remove the ECHO (not the echo) AFTER you are satisfied with what is displayed.
#echo off
set "Key=HKCU\Software"
set "STRING=teststring"
for /f "delims=" %%a in ('reg query "%Key%" /v test /s /d /f "%STRING%" /t REG_SZ') do (
echo %%a | findstr /v "REG_SZ" | findstr /v /c:"End of search:" >nul && ECHO reg.exe delete "%%a" /f
)
pause