how can set the DeviceID in a variable? output here is empty :(
in cmd working :
D:\>wmic logicaldisk where drivetype=5 get deviceid, volumename | find "bunny"
F: bd50-bunny-comple
here is my bat:
#echo off
d:\bunny.iso
set isoname=bunny
for /f "delims=" %%a in ('wmic logicaldisk where DriveType^="5" Get DeviceID^,volumename ^|find "%isoname%"') do (
set %%a
)
echo %DeviceID%
echo %volumename%
Regards
Based on Magoo's comment :
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
For /F "skip=1" %%a in ('wmic logicaldisk where DriveType^="5" Get DeviceID^,volumename') DO (
SET "line=%%a"
CALL :striptrailing
if not "!line!"=="" (
set "myvar=!line!"
goto :loop_end
)
)
::****************************************
:loop_end
ECHO %myvar%
pause & GOTO :EOF
::****************************************
:striptrailing
IF NOT DEFINED line GOTO :EOF
if "%line:~-1%"==" " GOTO striptrailing
GOTO :eof
::****************************************
Something like this perhaps:
#Echo Off
For /F "Skip=1 Delims=" %%A In (
'"WMIC LogicalDisk Where (DriveType='5') Get DeviceID, VolumeName"'
) Do For /F "Tokens=1-2" %%B In ("%%A") Do Set "DID=%%B" & Set "VOL=%%C"
Echo Volume %VOL% is assigned to %DID%
Timeout -1
Related
#echo off
cd /d D:\
setlocal enabledelayedexpansion
set /p "var=v3 "
set "file=Rev.bat"
for /F "tokens=1,* delims==" %%i in ('findstr "%var%" Rev.bat') do (
set "versionVar=%%~i"
set "versionVal=%%~j"
set /a sequence=%%~j+1
)
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
set "line=%%j"
if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!sequence!
echo(!line!>>!file!
)
I tried this code it doesn't find the string
ok, so if I understand you correctly, this is what you're looking for:
#echo off
cd /d D:\
setlocal enabledelayedexpansion
set "file=rev.bat"
for /F "tokens=1,* delims==" %%i in ('findstr "%~1" rev.bat') do (
set "versionVar=%%~i"
set "versionVal=%%~j"
set /a sequence=%%~j+1
)
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
set "line=%%j"
if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!sequence!
echo(!line!>>!file!
)
Explanation. We get the file content, using find to assign row id's Then we do search and replace and simply remove the row id's. Then write back everything to the file including the updated version.
note, you need to run the script with a parameter, i.e. script.cmd v3
Edit. to do more than one variable at a time by request, do:
#echo off
cd /d D:\
setlocal enabledelayedexpansion
:Begin
set /p "var=enter variable to replace [.ie v1, v2, v3]: "
set "file=rev.bat"
for /F "tokens=1,* delims==" %%i in ('findstr "%var%" rev.bat') do (
set "versionVar=%%~i"
set "versionVal=%%~j"
set /a sequence=%%~j+1
)
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
set "line=%%j"
if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!sequence!
echo(!line!>>!file!
)
I'm making a program that needs a pendrive, and I want detect if a pendrive is in the pc and distinguish he from a internal disk or anything else, somebody can help me?
Here's an example.
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For /F "Skip=2 Tokens=*" %%A In ('WMIC DiskDrive Where InterfaceType^="USB"^
Assoc /AssocClass:Win32_DiskDriveToDiskPartition 2^>Nul') Do (
For /F UseBackQ^ Delims^=^"^ Tokens^=2 %%B In ('%%A') Do (
For /F Delims^=^":^ Tokens^=6 %%C In (
'WMIC Path Win32_LogicalDiskToPartition^|Find "%%B"') Do (
For /F "Skip=1 Delims=" %%D In ('WMIC LogicalDisk Where^
"DeviceID='%%C:'" Get DeviceID^, VolumeName') Do Echo( %%D
Set "_C=!_C!%%C")))
If Not Defined _C Echo( You do not have a USB drive connected && GoTo :EndIt
If "%_C:~,1%" Equ "%_C%" GoTo :Picked
Echo( Enter the USB drive letter from the above [%_C%]:
For /F "Delims=? Tokens=2" %%A In ('Choice /C %_C%') Do Set "Letter=%%A:"
:Picked
If Not Defined Letter (Call :Task %_C%:) Else (Call :Task %Letter%)
:EndIt
>Nul Timeout 5
Exit/B
:Task
Rem Place your commands here
Echo Your selected pen drive is %1
Pause
I have a program to ping computers, check there registry, and tell me the results.
I am now trying to script it so that I don't have to know the ip address just the host name of the computer.
I found a script to give me the ip off of a hostname
for /f "tokens=1,2 delims=[]" %%a in ('ping -4 %%a ^| find "Pinging"') do set ip=%%c >nul
I have tried to simply insert this into a biger loop that uses a file to give it the host names.
for /f %%A in (%1) do (
for /f "tokens=1,2 delims=[]" %%a in ('ping -4 %%a ^| find "Pinging"') do (
set host=%%a
set ip=%%c
echo "."
echo %ip% %host%
pause
) >>%2
)
I have also tried it like this,
for /f %%a in (%1) do (
set /p hostname=%%a
for /f "tokens=1,2 delims=[]" %%b in ('ping -4 %%a ^| find "Pinging"') do set ip=%%c >nul
echo %ipaddress% %hostname%
)
Please any help on this would be greatly appreciated.
Thank you.
AFTER MUTCH HEAD BANGING
#echo off
SETLOCAL EnableDelayedExpansion
for /f %%A in (%1) do (
for /f "tokens=1,2 delims=[]" %%a in ('ping /4 /n 1 %%A ^| findstr "Pinging"') do echo "%%A %%b"
)
How can I save the SID of the current user to variable?
Based off of a similar question, the current code is as follows:
#echo off
for /f %%a in ('wmic useraccount where Name='%username%' get SID') do set "usersid=%%a"
echo %usersid%
exit /b
Is there a way I can save this to the variable %usersid%?
I believe this is an issue with the '' marks surrounding %username%.
#echo off
for /f "usebackq tokens=* delims=" %%a in (`wmic useraccount where Name^='%username%' get SID /format:value`) do (
for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"
)
set sid
On my machine I have no username alias.Here's why the additional FOR is needed
I found an answered question with the code required to do this.
Npocmaka was close to the answer (had I not incorrectly typed my original wmic command).
#echo off
for /f "delims= " %%a in ('"wmic useraccount where name='%UserName%' get sid"') do (
if not "%%a"=="SID" (
set myvar=%%a
goto :loop_end
)
)
:loop_end
echo %myvar%
I have simplified the code provided by 7th Luke.
#echo off
For /f "tokens=2 delims==" %%# in (
'"wmic useraccount where name="%UserName%" get SID /value"') Do (
echo %%#) & Pause > nul
I currently have a batch file that reads a list of computer names and pings each of these and outputs the ones that reply to a csv file with the computer name and ip address.
I now need to edit this to also find out the user of the machine. I need to contact users which are online to arrange some work done to their computer. Their can be over a hundred machines in the batch file so to manually find out each user takes time. Is there a way to do this?
`IF EXIST C:\test\new.csv (del C:\test\new.csv)
IF EXIST C:\test\final.csv (del C:\test\final.csv)
set ComputerList=C:\test\ClientList.txt
Echo Computer Name,IP Address>Final.csv
setlocal enabledelayedexpansion
echo please wait...
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0,-1!>>final.csv
)
)
findstr /V "IPAddress" final.csv >> C:\test\new.csv
echo identified machines for Install
start excel C:\test\new.csv
echo opened csv file`
The command I want to use to get the username is:
`wmic.exe /NODE: %%A COMPUTERSYSTEM GET USERNAME`
Thanks
Mark
Here is a function I wrote to do just what you are trying to do:
:GetLoggedInUser comp user
for /f %%u in (
'wmic /NODE:"%1" Computersystem get username^|find "\"') do (
if not errorlevel 1 ( for /f "tokens=2 delims=\" %%a in (
'wmic /NODE:"%1" Computersystem get username^|find "\"' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
) ELSE (for /f "skip=1" %%a in (
'wmic /NODE:"%1" Computersystem get username' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
))
Exit /b
Here is my function for pinging. It returns a 0 if the ping succeeded and a 1 otherwise.
:IsPingable comp
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b
Usage example:
for /l %%a in (1,1,255) do (
call:IsPingable 10.6.1.%%a && (
echo ping 10.6.1.%%a used )||( echo ping 10.6.1.%%a unused )
)
And here is for if you're pinging IP's and want to return the hostname as well:
:IsPingable2 comp ret
setlocal
for /f "tokens=2" %%a in (
'"ping -a -n 1 -4 "%~1" | Find "Pinging" 2>nul"') do set name=%%a
endlocal & set %~2=%name%
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b
Usage example:
#echo off
setlocal enabledelayedexpansion
for /l %%a in (1,1,255) do (
call:IsPingable2 10.6.1.%%a host && (
echo ping !host! - 10.6.1.%%a used )||( echo ping !host! - 10.6.1.%%a unused )
)
I just posted these because they just might come in handy for this type of thing in the future. You can use the :IsPingable now though.
You would use it like this in your code:
IF EXIST C:\test\final.csv (del C:\test\final.csv)
set ComputerList=C:\test\ClientList.txt
Echo Computer Name,IP Address,Logged In User>Final.csv
setlocal enabledelayedexpansion
echo please wait...
echo.
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|find "TTL="') do (
if not errorlevel 1 (
set IPadd=%%B
call :GetLoggedInUser %%B uname
echo %%A,!IPadd:~0,-1!,!uname!>>final.csv
)
)
)
echo identified machines for Install
start excel C:\test\final.csv
echo opened csv file
goto :eof
:GetLoggedInUser comp user
for /f %%u in (
'wmic /NODE:"%1" Computersystem get username^|find "\"') do (
if not errorlevel 1 ( for /f "tokens=2 delims=\" %%a in (
'wmic /NODE:"%1" Computersystem get username^|find "\"' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
) ELSE (for /f "skip=1" %%a in (
'wmic /NODE:"%1" Computersystem get username' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
))
Exit /b
The below code will count the number of lines in two files sequentially and is set to the variables SalaryCount and TaxCount.
#ECHO OFF
echo Process started, please wait...
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set SalaryCount=%%C
echo Salary,%SalaryCount%
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set TaxCount=%%C
echo Tax,%TaxCount%
Now if you need to output these values to a csv file, you could use the below code.
#ECHO OFF
cd "D:\CSVOutputPath\"
echo Process started, please wait...
echo FILENAME,FILECOUNT> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set Count=%%C
echo Salary,%Count%>> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set Count=%%C
echo Tax,%Count%>> SUMMARY.csv
The > will overwrite the existing content of the file and the >> will append the new data to existing data. The CSV will be generated in D:\CSVOutputPath