Path over registry (windows) - windows

I try to get a path over the regestry of windows. My Problem now is how do I get the Path out of MATLAB_ROOT_32?
for /F "tokens=* delims='C'" %%i IN ('reg query HKLM\SOFTWARE\WOW6432NODE\Mathworks\Matlab\7.9.1 /v MATLABROOT') do (set MATLAB_ROOT_32=%%i)
echo %MATLAB_ROOT_32%
set i=
rem GOTO Split1
rem :Split1
REM -- Split the result into MATLABROOT, REG_SZ and Folder using space as delimiter
for /f "tokens=1,2,3 delims='C'" %%a in ("%MATLAB_ROOT_32%") do set useless1=%%a&set useless2=%%b&set MATLAB_x32=%%c
echo %Matlab_x32%
The plan is to get the MATLAB Path in the Matlab_x32 variable.

This works for me:
#echo off
setlocal ENABLEEXTENSIONS
set MATLAB_VERSION=8.3
set KEY_NAME=HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432NODE\MathWorks\MATLAB\%MATLAB_VERSION%
set VALUE_NAME=MATLABROOT
for /F "usebackq tokens=2*" %%A IN (`reg query "%KEY_NAME%" /v "%VALUE_NAME%" 2^>nul ^| find "%VALUE_NAME%"`) do (
set MATLABROOT=%%B
)
echo %MATLABROOT%
Just change the Matlab version to whatever you're using and it should be fine. There are variations in the output from reg depending on the OS version, but this should cope (I think!)

Related

Set An Unknown Number of Variables That areParsed From Each Line of a Text File

Apologies for my poorly worded question and my scatterbrain workings. Essentially I want to set an unknown number of variables that are parsed from each line of a text file.
I have written batch file to create symbolic links for network shares to a C:\Volumes folder.
#echo off
echo:
set /p dest=ENTER FOLDER PATH:
set dest="%dest%"
net use %dest%
if not exist "C:\Volumes" MD "C:\Volumes"
for %%i in (%dest%) do (set "fold=%%~ni")
mklink /d "c:\VOLUMES\%fold%" "%dest%"
pause
What I want to try is the same theory but have the script point at a text file mounts.txt with a list of folder paths and for a for loop to cycle through the list make a symbolic link for each path in the list. I have toyed with counters and cannot get it working correctly. I don't think I am going about it the right way at all.
Contents of mounts.txt
\\10.19.10.238\Masters\Removed bin\Work here
\\10.19.10.241\Scanning\WIP\to process
This does not work:
#echo off
setlocal enableDelayedExpansion
set i=1
:add
Set /a "i+=1"
for /F "tokens=*" %%A in (mounts.txt) do (set dest%i%=%%A)
if exist %dest%%i% goto:add
echo %dest%
echo %dest%%i%
echo !dest!
echo !dest!%i%
pause
Nor this:
#echo off
setlocal enableDelayedExpansion
set i=0
For /F "Tokens=1* Delims=] EOL=" %%A In ('Find /N /V ""^<"mounts.txt"') Do (
set /a i=i+1
set "dest!i!=%%B"
)
For /l %%a in (1,1,4) do echo _dest%%a is !dest%%a!
For /l %%a in (1,1,4) do set dest%%a=!dest%%a!
echo !dest!
pause
I did get something like this to work to an extent but cannot figure out how to use the dest[1], dest[2] as variables in other processes further down in the script.
#echo off
setlocal enabledelayedexpansion
set counter=0
for /f "tokens=*" %%a In (mounts.txt) do (
set /a counter+=1
set "dest[!counter!]=%%a"
)
set dest[
And the list could be added to with many more. If %dest%n variables can be set, the use the same theory to set different %fold% variables based on each %dest%n then maybe the links can be set using the same process as the original script.
Any help is appreciated. Thank you.
If your question actually describes what you want to do, you don't need to set variables. Just use the lines from the text file with a for /f loop:
for /f "delims=" %%i in (mount.txt) do mklink /d "C:\VOLUMES\%%~ni" "%%i"
It's safer to quote the filename, especially if you want/need to use the FQFN. Therefore you need the usebackq option, else a quoted string will be processed as a string, not a filename:
for /f "usebackq delims=" %%i in ("C:\full path\mount.txt") do mklink /d "C:\VOLUMES\%%~ni" "%%i"
You seemingly want to dynamically set the variables as dummy array's and return results based on the line numbers. If so, use the counter you created to become the max of the for /L loop, here is an example by just echoing the results:
#echo off & set cnt=0
setlocal enabledelayedexpansion
for /F "usebackq delims=" %%i in ("mounts.txt") do (
set /a cnt+=1
set "var[!cnt!]=%%i"
)
for /L %%a in (1,1,!cnt!) do echo !var[%%a]!
Edit
As highlighted by #Compo in the comments; after the initial code block where we increment the %cnt% variable, we no longer require using delayedexpansion on the %cnt% variable specifically and can therefore use %cnt% instead of !cnt!:
#echo off & set cnt=0
setlocal enabledelayedexpansion
for /F "usebackq delims=" %%i in ("mounts.txt") do (
set /a cnt+=1
set "var[!cnt!]=%%i"
)
for /L %%a in (1,1,%cnt%) do echo !var[%%a]!
This is a courtesy example, just to show you how you could have still used find.exe, as in one of your examples, and additionally includes a different method of iterating the variables with unknown increments:
#Echo Off
SetLocal EnableExtensions
Rem The next line ensures that there are no variables defined with names beginning _
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
Rem The next lines parse the directory paths content file and defines the incrementing variables
For /F "Tokens=1,* Delims=]" %%G In (
'"%SystemRoot%\System32\find.exe /N "\" 0< "C:\Users\roar\My Directory\mounts.txt" 2>NUL"'
) Do (
Set "_Path%%G]=%%H"
Set "_Name%%G]=%%~nxH"
)
Rem The next lines iterate the defined variables beginning _Path[ and runs commands against them
For /F "Tokens=2 Delims=[]" %%G In ('"(Set _Path[) 2>NUL"') Do (
SetLocal EnableDelayedExpansion
MkLink /D "C:\Volumes\!_Name[%%G]!" "!_Path[%%G]!"
EndLocal
)
Rem The next line undefines any previously defined variables named beginning with _
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
Please note that is likely you may need to run this script elevated, in order for the MkLink command to create the symbolic links.

How to set FULL computer name to a variable in windows batch file

When right clicking >Computer > Properties, my full computer name is 50 characters.
If I set Cname=%COMPUTERNAME% and echo %Cname%, the FULL name is truncated from 50 characters down to just the Host name of 14 chars
In a batch file, how is the FULL computer name extracted?
This example is based purely on the vbscript you stated was retuning the string you require:
#Echo Off
Set "Pre=HKLM\SYSTEM\CurrentControlSet"
For /F "Delims==" %%A In ('Set _ 2^>Nul') Do Set "%%A="
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Control\Computername\ActiveComputerName" /V "ComputerName" 2^>Nul
')Do Set "_ActiveComputerName=%%B"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Control\Computername\ComputerName" /V "ComputerName" 2^>Nul
')Do Set "_ComputerName=%%B"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Services\TCPIP\Parameters" /V "HostName" 2^>Nul
')Do Set "_HostName=%%B"
Set _
Simply choose the example which outputs the string you were requiring for your purposes.
Either:
#Echo Off
Set "Pre=HKLM\SYSTEM\CurrentControlSet"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Control\Computername\ActiveComputerName" /V "ComputerName" 2^>Nul
')Do Set "ActiveComputerName=%%B"
Set ActiveComputerName
Pause
Else:
#Echo Off
Set "Pre=HKLM\SYSTEM\CurrentControlSet"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Control\Computername\ComputerName" /V "ComputerName" 2^>Nul
')Do Set "Computer-Name=%%B"
Set Computer-Name
Pause
Or:
#Echo Off
Set "Pre=HKLM\SYSTEM\CurrentControlSet"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Services\TCPIP\Parameters" /V "HostName" 2^>Nul
')Do Set "Host-Name=%%B"
Set Host-Name
Pause
For the reference for Computer Name and Full computer name
https://superuser.com/questions/640046/what-is-the-difference-between-computer-name-and-full-computer-name
Now coming to your question, You can get your computer name with following ways:
%computername%
net config workstation | findstr /C:"Full Computer name"
wmic computersystem get name
Reference :
https://www.windows-commandline.com/find-computer-name-from-command-line/
There is no limitation of DOS console, I have tested as below:

store part of outcome command to variable

Hello dear people and others,
Today i wanted to create a simple script, thought it would be easy to store the outcome to var of the following command:
wmic bios get serialnumber | findstr /N /V SerialNumber
Outcome:
2:H3GK4S1
3:
The problem is when i try to get the serial with wmic, it returns the string as expected but also an empty string/line. When i try to store the serial to a variable it stores it and then directly overwrites it with the empty string. This is the function i nearly got working now:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
And this gives the following output:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
2:H3GK4S1
3:
As can be seen above, the loop overwrites the serial var, if someone can help me towards the right directon to get this working, would be mad.
At the Command Prompt:
For /F "Tokens=1* Delims==" %g In ('WMIC BIOS Get SerialNumber /Value') Do #For /F "Tokens=*" %i In ("%h") Do #Set "serial=%i" & Echo %i
Or in a batch file:
#For /F "Tokens=1* Delims==" %%g In ('WMIC BIOS Get SerialNumber /Value'
) Do #For /F "Tokens=*" %%i In ("%%h") Do #Set "serial=%%i" & Echo %%i
#Pause
EditIf you're happy to use a labelled section in your batch file:
#Echo Off
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
Pause
GoTo :EOF
:Sub
Set "serial=%*"
GoTo :EOF
Try like this:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber /format:value') DO for /f "tokens=* delims=" %# in ("%g") do set "serial=%#"
echo %serial%
Mind that's a command that should be executed in the command prompt directly.For a batch file you'll need to double the % in the for loop tokens.
In a batch file, you can also use a goto to end the loop after the first iteration :
#echo off
for /f "tokens=2 delims=:" %%a in ('wmic bios get serialnumber ^| findstr /N /V SerialNumber') do (
set "$var=%%a"
goto:next
)
exit/b
:next
echo Result=^> [%$var: =%]

Store randomized folder in a batch script?

I have a script that automatically backup NAPS2 (Scanner program) newest scan result to another drive, and also rename it according its timestamp...
Problem is, NAPS2 uses randomized folder name as the parentfolder of its scan result... e.g
"C:\Users\username\AppData\Roaming\NAPS2\recovery\4j4v.fbmv\scanresult001.jpg"
Here's the code, how do i point to that randomized folder?
SET ROOTDIR=C:\Users\Operator\AppData\Roaming\NAPS2\recovery\
for /f %%i in ('dir %ROOTDIR% /b /AD /od /t:w') do set LATEST_DIR=%%i >NUL
SET RANDOMDIR=%ROOTDIR%%LATEST_DIR%
for /F "delims=" %%a in ('dir /b /od "%RANDOMDIR%\*.jpg"') do set Youngest=%%a
echo Backing up %Youngest%
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set mydate=%YYYY%%MM%%DD%#%HH%%Min%%Sec%
copy "%Youngest%" "D:\Backup\%mydate%.jpg"
Output
C:\>for /F "delims=" %a in ('dir /b /od "C:\Users\Operator\AppData\Roaming\NAPS2
\recovery\jfwkui0s.pmd \*.jpg"') do set Youngest=%a
File Not Found
C:\>echo Backing up
Backing up
C:\>for /F "delims=" %a in ('wmic OS Get localdatetime | find "."') do set dt=%a
:\>set dt=20160818125756.950000+420
C:\>set YYYY=2016
C:\>set MM=08
C:\>set DD=18
C:\>set HH=12
C:\>set Min=57
C:\>set Sec=56
C:\>set mydate=20160818#125756
C:\>copy "" "D:\Backup\20160818#125756.jpg"
The system cannot find the path specified.
I think it's probably because the randomized folder has "." in it.. I tried "New Folder" which has space in it and it doesnt work as well
To do this use the approach shown in https://stackoverflow.com/a/30311479/6550457
You'll want to do it only for directories though so instead of a-d use ad
SET ROOTDIR=C:\Users\username\AppData\Roaming\NAPS2\recovery\
for /f %%i in ('dir %ROOTDIR% /b /AD /od /t:w') do set LATEST_DIR=%%i>NUL
echo Latest Dir = %LATEST_DIR%
so in your script you'd use it as:
SET ROOTDIR=C:\Users\username\AppData\Roaming\NAPS2\recovery\
for /f %%i in ('dir %ROOTDIR% /b /AD /od /t:w') do set LATEST_DIR=%%i>NUL
SET RANDOMDIR=%ROOTDIR%%LATEST_DIR%
echo Backing up %RANDOMDIR%
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set mydate=%YYYY%%MM%%DD%#%HH%%Min%%Sec%
copy "%RANDOMDIR%" "E:\Backup\%mydate%.png"

How to get desktop path with batch

I want to get the desktop path from Windows Registry using a batch file. I created the following code, but it prints:
Desktop REG_SZ C:\Documents and Settings\Usuario\Escritorio
Any clue how I can set only the path in DESKTOP_DIR variable?
Here's the code:
#ECHO OFF
SET DESKTOP_REG_ENTRY="HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
SET DESKTOP_REG_KEY="Desktop"
SET DESKTOP_DIR=
FOR /F "tokens=*" %%a IN ('REG QUERY %DESKTOP_REG_ENTRY% /v %DESKTOP_REG_KEY% ^| FINDSTR "REG_SZ"') DO (
ECHO %%a
)
ECHO Desktop dir: %DESKTOP_DIR%
PAUSE
Like this :
#echo off
SET DESKTOP_REG_ENTRY="HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
SET DESKTOP_REG_KEY="Desktop"
SET DESKTOP_DIR=
FOR /F "tokens=1,2*" %%a IN ('REG QUERY %DESKTOP_REG_ENTRY% /v %DESKTOP_REG_KEY% ^| FINDSTR "REG_SZ"') DO (
set DESKTOP_DIR="%%c"
)
ECHO Desktop dir: %DESKTOP_DIR%
PAUSE

Resources