How to get desktop path with batch - windows

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

Related

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:

Save the result of a command in variable, Windows batch

I am trying to write a batch script that saves the result of a command in a variable. so I can use it later.
For example I am tryin to run this on the script:
sc queryex "Service" |find /i "pid"
but I want to save this result in a variable.
set PIDRS=sc queryex "Themes" |find /i "pid"
ECHO "%PIDRS%
Any Ideas?
for /f "tokens=* delims=" %%# in ('sc queryex "Themes" ^|find /i "pid"') do set "PIDRS=%%#"
echo %PIDRS%
This will set the entire line to PIDRS
here's how to get only the pid:
#echo off
set "rspid="
for /f "skip=9 tokens=2 delims=:" %%# in ('sc queryex "Themes"') do (
if not defined rspid set /a rspid=%%#
)
the second does not use additional FIND which in theory should make it faster.

Path over registry (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!)

How to check if a folder has more than 20 files in it in batch?

I want to write an batchfile that Deletes a folder
if the folder has more than 20 files in it. but I do not know how to do it.
I use Windows7 Ultimate.
List the files in bare format and use find command to count the number of output lines
set "nFiles=0"
for /f %%a in (
'dir /a-d /b "c:\folder\*" 2^>nul ^| find /c /v ""'
) do set "nFiles=%%a"
echo %nFiles%
List the files in usual format and use findstr to locate the line/field with the number of files
set "nFiles="
for /f %%a in (
'dir "c:\folder\*" ^| findstr /b /c:" "'
) do if not defined nFiles set "nFiles=%%a"
if not defined nFiles set "nFiles=0"
echo %nFiles%
Use a counter over the output of dir command
set "nFiles=0"
for /f %%a in ('dir /b /a-d "c:\folder\*" 2>nul ') do set /a "nFiles+=1"
echo %nFiles%
Or you can use wmic, or number the lines in the output of dir command with the usage of findstr /n, or ....
In any case
if %nFiles% gtr 20 rd /s /q "c:\folder"

How to get the number of files in directory in variable in windows batch file

I have tried following command to do this but it's not working :
for /f "delims=" %%i in ('dir "d:/Database/BARC/" /b/a-d | find /v /c "::") do set count=%%i
It is showing some error like unexpected error. How to round this error?
#echo off
for /f "delims=" %%i in ('dir "d:/Database/BARC/" /b/a-d ^| find /v /c "::"') do set count=%%i
echo %count%

Resources