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

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.

Related

Batch Script to Search for Softwares by Vendor to a CSV file

I want a batch script to search for software's on a computer by a specific vendor and save the results to a CSV file.... I am tried it with the following script, but it always returns NULL values, not able to figure out the issue.
#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
:Sub
Set "serial=%*"
Set "LogFile=%serial%.CSV"
If Exist "%LogFile%" Del "%LogFile%"
wmic /Append:"%LogFile%" product where "Name like '%Microsoft%'" get Name, Version /Format:CSV
Start "" "%LogFile%"
exit
NB : % is needed to be used as wildcard
In this case you should add another percent character % to %Microsfot% to escape the wilcard of WMIC in your batch and become like %%Microsoft%%
#echo off
Title Batch Script to Search for Softwares by Vendor to a CSV file
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
:Sub
echo(
echo( Please wait a while ... Searching for installed softwares ....
Set "serial=%*"
Set "LogFile=%serial%.CSV"
If Exist "%LogFile%" Del "%LogFile%"
wmic /Append:"%LogFile%" product where "Name like '%%Microsoft%%'" get Name, Version /Format:CSV
Start "" "%LogFile%"
Exit

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

Batch - check if variable matches any item in a list

I've got a simple script that grabs a PC's manufacturer and sets a variable:
for /f "usebackq tokens=2 delims==" %%A IN (`wmic csproduct get vendor /value`) DO SET VENDOR=%%A
What I need to do next is to check if my new VENDOR variable matches anything in a list:
Acer
Gateway
Packard
Alienware
ASUS
Dell Inc.
Fujitsu
HP
Compaq
Lenovo
IBM
Samsung
Sony
Toshiba
and then DO COMMAND1 if found, and DOCOMMANDB if not found.
I think that piping the variable into findstr might work, but I only know findstr usage when you're feeding it a text file. I'm really bad at regex so I would need some guidance on that if it's the best option.
How can I accomplish the above?
If you pipe the Vendor to findstr /I /G:Vendorlist.txt and use conditional execution && for success and || for fail :
#Echo off
for /f "tokens=2 delims==" %%A IN (
'wmic csproduct get VENDOR /value'
) DO Set VENDOR=%%A
Echo:%VENDOR%|Findstr /I /G:VENDORList.txt >NUL 2>&1 &&(
Echo found %VENDOR% in List
)||(
Echo %VENDOR% not found in list
)
Slightly different approach. You could also put all the vendors in a list and read the list with a FOR /F.
#echo off
for /f "usebackq tokens=2 delims==" %%A IN (`wmic csproduct get vendor /value`) DO SET VENDOR=%%A
FOR %%G IN ("Acer"
"Gateway"
"Packard"
"Alienware"
"ASUS"
"Dell Inc."
"Fujitsu"
"HP"
"Compaq"
"Lenovo"
"IBM"
"Samsung"
"Sony"
"Toshiba") DO (
IF /I "%vendor%"=="%%~G" GOTO MATCH
)
:NOMATCH
echo Does not match
pause
GOTO :EOF
:MATCH
echo Does match
pause
GOTO :EOF
If you want to read the list where each entry is on its own line.
FOR /F "usebackq delims=" %%G IN ("filelist.txt") DO IF.......

Batch Script to run a computer test

I am not really a programmer. I am a newbie.
Can you guys please help me with a windows batch file that will run a computer test?
Detect any hard drives then run a chkdisk on them
Detect any usb port
Detect any flash drive plug in the pc
Check the display pixels
I have started with this to detect the USB but somehow getting some errors
#echo off
setlocal EnableDelayedExpansion
set PNPDeviceID=4002FDCCE0E4D094
set Q='wmic diskdrive where "interfacetype='USB' and PNPDeviceID like '%%%PNPDeviceID%%%'" assoc /assocclass:Win32_DiskDriveToDiskPartition'
echo %Q%
for /f "tokens=2,3,4,5 delims=,= " %%a in (%Q%) do (
set hd=%%a %%b, %%c %%d
call :_LIST_LETTER !hd!)
goto :_END
:_LIST_LETTER
(echo %1 |find "Disk ") >nul|| goto :_EOF
for /f "tokens=3 delims==" %%a in ('WMIC Path Win32_LogicalDiskToPartition ^|find %1') do set TMP_letter=%%a
set Part_letter=%TMP_letter:~1,2%
echo %Part_letter% %1
goto :_EOF
:_END
:_EOF
:: *** end
pause
Here is a batch script that does almost everything you need.
Checkdisk will run against any local fixed drives. You can have it run in a new window (chkdskAsync=true) or have it run inside the program (chkdskAsync=false). Additionally, specify the chkdsk flags using the chkdskLaunchArguments= variable.
#ECHO OFF
CLS
SETLOCAL ENABLEDELAYEDEXPANSION
:: Use these lines to specify how CHKDSK and Notepad will run
:: ===============================================
SET chkdskLaunchArguments=/F /R
SET chkdskAsync=true
SET launchNotepad=true
:: ===============================================
COPY /Y NUL C:\Users\public\data.txt >nul 2>&1
ECHO Getting drive(s) information
FOR /F "tokens=*" %%A IN ('wmic logicaldisk get caption^, description^, providername^, volumename') DO (
ECHO.%%A>>C:\Users\public\data.txt
)
FOR /F "tokens=1,2 skip=1" %%A IN ('wmic logicaldisk get caption^, drivetype') DO (
IF %%B EQU 3 (
IF "!chkdskAsync!" EQU "true" (
START CMD /C CHKDSK %%A %chkdskLaunchArguments%
) ELSE (
CHKDSK %%A %chkdskLaunchArguments%
)
)
)
ECHO Getting resolution information
FOR /F "tokens=*" %%A IN ('wmic path Win32_VideoController get CurrentHorizontalResolution^,CurrentVerticalResolution') DO (
ECHO.%%A>>C:\Users\public\data.txt
)
ECHO Information gathered ... saved to C:\Users\public\data.txt
ECHO Showing results.
IF "!launchNotepad!" EQU "true" (
START /WAIT notepad.exe /A C:\Users\public\data.txt
)
ENDLOCAL
EXIT
When it is complete it saves everything to C:\Users\Public\data.txt. It will also launch the file in notepad (if launchNotepad=true is set to ... true).
The output looks like so:
Caption Description ProviderName VolumeName
A: Network Connection \\somefileserver.domain\ New Volume
C: Local Fixed Disk
E: Removable Disk someuser Drive
H: Network Connection \\somefileserver.domain\users$\someuser Data Share and User Home Dirs
P: Network Connection \\somefileserver.domain\Data Data Share and User Home Dirs
S: Network Connection \\somefileserver.domain\share
CurrentHorizontalResolution CurrentVerticalResolution
1920 1080
NOTE: Make sure to run this as an administrator. If you want chkdsk to run
Unfortunately, there isn't a way to grab all USB devices other than storage. Hope this helps!
I didn't realize this question is 4 years old. Going to leave my answer for posterity and anyone digging for answers.
I would recommend trying
chkdsk volume=C:\ D:\ E:\ F:\
to check the disks each as a separate volume

Resources