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.
Related
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.
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
#echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype
2^>NUL`) do (
if %%l equ 2 (
echo %%i is a USB drive.
)
)
I used above commands in batch file and able to get usb drive letter from-
Find USB Drive letter
but please someone help me, how to use that letter in same batch file to create folders and files in the same drive, and it should work even if use as path for any specific command
With the above script, the output should be something like X: is a USB drive.
So with that one knows that in %%i X: is stored. Knowing this, one can set the letter to a variable like this: set "driveLetter=%%~i" for later use. This could be creating a directory for example:
#echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype
2^>NUL`) do (
if %%l equ 2 (
echo %%i is a USB drive.
set "driveLetter=%%~i"
)
)
md "%driveLetter%\myFolder"
I am trying to create a batch file that will return a file location, including partition (or drive) so I can use it for more manipulation in that batch file.
The file name is known but the partition and directory are not known. Is that possible?
You can try something like that :
#echo off
Title Searching for file by name
Mode con cols=75 lines=3
cls & color 0A
Set SearchResult=SearchResult.txt
If Exist %SearchResult% Del %SearchResult%
echo(
set /P "FileName=Type the file name for looking for = "
Setlocal EnableDelayedExpansion
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=3" ^|find /i ":"') do (
Set MyDrive=%%i
echo( & cls
echo( & echo Please Wait for moment .... Searching for "%FileName%" on "!MyDrive!\"
#where /r !MyDrive!\ "%FileName%" >> %SearchResult%
)
Start %SearchResult%
The following script -- let us call it findfile.bat -- searches for a file with the name given as a command line argument (for instance, findfile.bat "lostfile.txt") on local disks and network drives as an example:
#echo off
for /F "skip=1" %%I in (
'wmic LOGICALDISK WHERE ^( ^
DriveType^=3 OR^
DriveType^=4^
^) GET DeviceID'
) do (
for /F "delims=" %%J in ("%%I") do (
2> nul where /R %%J\ "%~1"
)
)
The wmic command is used to retrieve the drives available on the current system. In the example there are two DriveType values allowed: 3, meaning local disks, and 4, meaning network drives. You can adapt the filter as you like -- reference site Win32_LogicalDisk class for all the possible values. To not filter any drive types, simply remove the entire WHERE clause and use the remaining wmic command line wmic LOGICALDISK GET DeviceID instead.
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