Batch Scripting echo available drives error - batch-file

Im having a very weird problem where i want to print out the available usb drives connected on my laptop but when i try to do so this is what comes up on the command line.
for /f "delims=" %%a in ('wmic logicaldisk where drivetype^=2 get deviceid ^| find ":"') do set "List=!List!%%a"
:usb
echo Avaiable Drive ==^> !List!
set /p "Drive=Enter the letter of the Backup drive : "
echo !List! | find /i "%Drive::=%:" && Echo drive OK || Echo drive do not exist && goto:usb
This my code im fairly new to scripting, can someone help me fix the problem or improve the code as i dont know if its the best way to do this.

Your problem is the ugly wmic line ending, which includes an additional CR (Carriage Return). There are many ways to eliminate it, but in your situation simply use delims=: does the trick (and additionally removes the colon, which is a good idea for the following).
Let me suggest another way for User input (using choice, which eliminates the need to re-check the drive):
#echo off
setlocal enabledelayedexpansion
set "list= "
for /f "delims=:" %%a in ('wmic logicaldisk where drivetype^=2 get deviceid ^| find ":"') do set "List=!List!%%a"
choice /m "Enter the letter of the Backup drive : " /c %list%
set drive=!list:~%errorlevel%,1!:
echo Drive is %drive%

Related

Aquiring USB Drive Letters from command and use them as choice variables

I'm trying to create a batch file for backing up folders to a usb drive. In a certain point I want the batch to show a list of all avalaible USB Drives connected to the computer and then asking the user to select the drive letter of one of them to proceed.
As for the the list: I want to combine those two outputs in one line per usb drive
wmic logicaldisk where DriveType^=2 get deviceid
wmic diskdrive where mediatype^="removable media" get Caption, SerialNumber
As for the choice command: How can i use the drive letters as choices for the user to select from?
The following code should:
Determine your USB drive letter(s).
If there are none found notify you then close.
If only one is found, select it automatically.
If several are found, offer a selection choice.
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set "USBDrv="
For /F Tokens^=2^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
DiskDrive Where "InterfaceType='USB'" Assoc:List
/AssocClass:Win32_DiskDriveToDiskPartition 2^>NUL
^| %SystemRoot%\System32\findstr.exe "^__R"'
) Do For /F Tokens^=4^ Delims^=^" %%H In ('%SystemRoot%\System32\wbem\WMIC.exe
Path Win32_LogicalDiskToPartition ^| %SystemRoot%\System32\findstr.exe /C:"%%G"
2^>Nul') Do Set "USBDrv=!USBDrv!%%H"
If Not Defined USBDrv Echo No USB drive connected.&& GoTo :EndIt
If "%USBDrv:~2%" == "" (GoTo Selected) Else Echo Which USB drive %USBDrv::=: %?
For /F "Tokens=2 Delims=?" %%G In ('%SystemRoot%\System32\choice.exe
/C %USBDrv::=% 2^>NUL') Do Set "USBDrv=%%G:"
:Selected
Call :Task %USBDrv%
:EndIt
%SystemRoot%\System32\timeout.exe /T 5 1>NUL
GoTo :EOF
:Task
Rem Place your commands here [%1 will be the USB drive e.g. E:].
All you should need to do is to place your backup command(s) etc. at the bottom.

Only run code when connected to wifi Batch

I need to run a batch file only if it's connected to Wifi and specifically not Bluetooth LAN
I have this code but it returns this and still runs the code while an internet connection isn't present
Node - DEVICENAME
ERROR:
Description = Invalid query
Code:
#echo off
For /f "usebackq" %%A in (
`wmic path WIN32_NetworkAdapter where 'NetConnectionID="Wi-Fi"' get NetConnectionStatus`
) do if %%A equ 7 (goto end)
<code to run>
:end
You don't need a for loop:
wmic path WIN32_NetworkAdapter where 'NetConnectionID="Wi-Fi"' get NetConnectionStatus |find "7" >nul && goto :eof
echo code to run
If you want to make it more secure, instead of find "7" use findstr /rc:"^7 *$"
(your original approach fails because the = has to be escaped: ... where 'NetConnectionID^="Wi-Fi"' get ... and due to the unusual wmic output, there are CR in your %%A, which messes up the if syntax; You can see both issues with echo on (at least you can see that strange things happen))
The output of WMIC is also the reason for that strange findstr pattern, I used. (there are trailing spaces after the 7).
If you don't already know the name of the wireless interface connection, (which is a configurable property), then you could probably use something more like this:
#For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe NIC
Where "Not NetConnectionID Is Null And NetConnectionStatus='2'" Get
NetConnectionID /Format:MOF 2^>NUL') Do #%SystemRoot%\System32\netsh.exe WLAN^
Show Interfaces 2>NUL | %SystemRoot%\System32\findstr.exe /E /L ": %%G" 1>NUL^
&& <code to run>
If your target systems are still using Windows 7, (which has a known issue locating some of the XSL files used in the /Format option), then the following alternative may work for you:
#For /F "Skip=1 Delims=" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe NIC Where
"Not NetConnectionID Is Null And NetConnectionStatus='2'" Get NetConnectionID
2^>NUL') Do #For /F "Tokens=*" %%H In ("%%G") Do #%SystemRoot%\System32\netsh.exe^
WLAN Show Interfaces 2>NUL | %SystemRoot%\System32\findstr.exe /E /L ": %%H" 1>NUL^
&& <code to run>
You would obviously change your provided, and replicated above, <code to run>, to one or more actual valid commands

Batch Scripting Backing up files in USB

Iv got my code to show a list of USb devices connected to my laptop and i can select the USB as well but when i try to back up a folder into the USB what i get instead is a folder named f in the folder i was trying to save. This is the code i have for selectong a USB device
for /f "delims=" %%a in ('wmic logicaldisk where drivetype^=2 get deviceid ^| find ":"') do set "$List=!$List! %%a"
:USB
echo Avaiable Drive ==^> !$List!
set /p "$Drive=Enter the letter of the Backup drive : "
echo !$List! | find /i "%$Drive::=%:" && Echo drive OK || Echo drive do not exist && goto:USB
set backupcmd=robocopy
and this is the code that backup the folder
%backupcmd% "%cd%" "%$Drive%"
Give this one a go:
#echo off
setlocal enabledelayedexpansion
set num=0
for /f %%a in ('wmic logicaldisk where drivetype^=2 get drivetype^, deviceid ^| find ":"') do (
set /a num+=1
set cnt=!cnt!!num!
echo (!num!^) %%a
set "d!num!=%%a"
)
choice /c !cnt! /M "select a number to choose a drive: "
echo You've chosen drive !d%errorlevel%! you can do everything with it from here..
You will notice that I set delayedexpansion here because of the setting and using of variables inside of the parenthesised code block.
Choice is a good option here as it does not allow typing the wrong values.

Bat file to copy to device based on device name not drive letter

Not sure the best way to accomplish this and looking for a point in the right direction.
Currently use a Bat file to fetch Backup files and copy them to an external device.
For example:
XCOPY C:\MainFolder\Backups\*.* G:\Backupfolder\ /D /S /E /Y
I am trying to encourage users to backup to more than one device and to keep one device offsite.
The backup usually occurs overnight and is setup as a scheduled task.
Most of the users are computer illiterate.
As we know, you can plug in multiple USB devices and it will assign it a new drive letter.
Is there any way i can Copy to a device based on its name opposed to drive letter?
Its too complicated for users to go into Computer management and assign it the correct drive letter.
The only thing i can think of, is using Bat file to copy to a array of Drive letters, but with this i can see it backing up to things that it should not.
Assuming you know the Volume name of the removable disk:
#echo off
for /f %%i in ('wmic logicaldisk where "drivetype=2" get Name^, VolumeName ^| findstr /i "Name Of USB volume here"') do echo %%i
The above will echo the drive letter of the volume name.
Where the below will echo both Volumename and Drive letter:
#echo off
for /f "tokens=1,*" %%i in ('wmic logicaldisk where "drivetype=2" get Name^, VolumeName ^| findstr /i "Name Of USB volume here"') do echo %%i %%j
You can run wmic on its own to see other results:
wmic logicaldisk get Name, VolumeName, Caption, DriveType
As a side note. Removable drive types are 2
As an alternative, and possibly because wmic can be a little slow, you could try this from your batch-file:
#Echo Off
Set "Drv="
For /F "Tokens=*" %%A In (
'MountVol^|Find ":\"'
)Do Vol %%~dA 2>NUL|Find /I "YourLabel">NUL&&Set "Drv=%%~dA"
If Not Defined Drv Exit /B
Echo Your Drive Letter is %Drv%
Timeout 3 >NUL
Just change YourLabel, (on the fifth line above), to that you have set as your unique device label.
If you wanted to use wmic, because all USB devices are not seen as drive type 2, it may be safer not to rely on its DriveType value. I would therefore suggest this alternative:
#Echo Off
Set "Drv="
For /F Skip^=1 %%A In (
'WMIC LogicalDisk Where "VolumeName='YourLabel'" Get DeviceID 2^>NUL'
)Do For /F Tokens^=* %%B In ("%%A")Do Set "Drv=%%B"
If Not Defined Drv Exit /B
Echo Your Drive Letter is %Drv%
Timeout 3 >NUL
Once again changing YourLabel as necessary.
In both cases you'd obviously change the penultimate line to:
XCopy "C:\MainFolder\Backups" "%Drv%\Backupfolder\" /D /S /Y

Reading out put of previous command in bat command file

I am very new to batch programming, I am trying to write a batch file that is a fake virus. I need to obtain the IP address from the previous command IPCONFIG into the variable VarIP. Can you help me?
My code:
echo off
echo Trying to hack your computer
ipconfig
echo Now hacking your IP
ping -t VarIP
echo on
pause
It's pretty simple to extract part of the output from any console command by using find to eliminate the lines in the output that you do not want, then using the for command to extract a portion of the line found by find:
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2 delims=:" %%i in ('ipconfig ^| find /i "IPv4 Address"') do (set VarIP=%%i&set VarIP=!VarIP: =!)
ping -t !VarIP!
endlocal
Hopefully you are just creating a practical joke on a friend and aren't up to anything more nefarious.
Another Version without "Tokens" for NT :
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('ipconfig ^| find /i "IPv4 Address"') do (set VarIP=!%%a%!)
ping -t %VarIP%
This is a useful method to get IP info:
#echo off
for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get IPAddress /value | find "I" "') do echo IPv4 %%~a IPV6 %%~b
pause

Resources