I have this script that checks for a certain USB and then opens a file on it. However, now I need it to check only for the drive letter of the USB inserted so. However I don't really know how to do that and searches online returned nothing that could help me.
This is the script I have right now:
#echo off
:loop
if exist D:\ (goto Load) else (goto loop)
:Load
D:
start Loader1.exe
goto Finish
:Finish
exit
Is something like this that you are looking?
This will take over the task only on the Removable Disk and will be given the associated logical name of each for check if your file Loader1.exe is there for take next action.
See this link: How To List Drive Letters:
Value Meaning for drivetype in WMIC logicaldisk
0 Unknown
1 No root directory
2 Removable disk
3 Local disk
4 Network drive
5 Compact disk
6 RAM disk
wmic logicaldisk get caption,drivetype|find "2"
This Result Removable disk :
D: 2
E: 2
F: 2
G: 2
Caption = D:
DriveType = Removable disk == 2
After, and make sure the Loader1.exe file exist to start/run your command:
Driver:\Loader1.exe
The last part, time out (1 minute) to perform the next loop:
timeout 60 >nul && goto :loop
#echo off
:loop
for /f %%i in ('"wmic logicaldisk get caption,drivetype|find "2""')do if exist "%%~i\Loader1.exe" (
cd /d %%~i\ & start .\Loader1.exe && goto :Finish )
timeout 60 >nul & goto :loop)
:Finish
:: Or ::
#echo off
set "_cd=%cd%" && title <nul
:loop
for /f %%i in ('"wmic logicaldisk where drivetype=2 get name|find /v "Name""
')do if exist "%%~i\Loader1.exe" cd /d %%~i\ & start .\Loader1.exe & cd /d "%_cd%" & exit /b
timeout 60 >nul & goto :loop
Here's how to get the USB drive letter from any drive:
#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. )
)
Answer is not a duplicate of #scientist_7 I got it from Find USB Drive letter.
Related
#echo on
set choice=
:D
if exist D: (set choice=D:) if exist (choice=D:) goto copyG else goto E
:E
if exist E: (set choice=E:) if exist (choice=E:) goto copyG else goto F
:F
if exist F: (set choice=F:) if exist (choice=F:) goto copyG else goto H
:H
if exist H: (set choice=H:) if exist (choice=H:) goto copyG
:copyG
xcopy /e /y G:\2019-Year10\*.* "%choice%"
pause
:removed
echo removed
This is my current code.
But is does not copy the files to the USB from the G drive.
Is there a way to fix this?
Edit:
Thanks got it working in the end i used this
#echo off
echo.
echo Finding Drive to copy work from "G:\2019-Year10"
echo.
for %%i in (D: E: F: H:) do (
if exist %%i set drive=%%i
)
xcopy /e /y "G:\2019-Year10\*.*" "%drive%\2019-Year10\"
echo.
echo Files coped to "%drive%"
echo.
echo.
echo Remove %drive% Drive
:removle_of_drive
if exist %drive% (goto removle_of_drive) else exit
It knows when you take out the USB and closes the batch file, and gives you more info on what it is doing
thanks for the help: Gerhard Barnard
So here we can use wmic to determine the disk type. Note!!! If you have more than one USB attached, this will not do as intended:
#echo off
for /F "tokens=1-4" %%a in ('wmic logicaldisk get caption^,description^,drivetype 2^>nul') do (
if %%l equ 2 (
echo xcopy /e /y "G:\2019-Year10\*.*" "%%d"
)
)
pause
Also Note I added echo to the xcopy line to simulate the command, only remove echo to perform the ACTUAL xcopy command once you are happy that it will do what you want it to.
But to basically show you how the choice one should work (as per your example):
CHOICE /C DEFH /M "Choose driveletter "
if errorlevel 4 set "Drive=H:"
if errorlevel 3 set "Drive=F:"
if errorlevel 2 set "Drive=E:"
if errorlevel 1 set "Drive=D:"
echo xcopy /e /y "G:\2019-Year10\*.*" "%Drive%\"
Or to automate it based on your know drive letters, instead of detecting it like the first example (as per your example)
#echo off
for %%i in (D: E: F: H:) do (
if exist %%i echo xcopy /e /y "G:\2019-Year10\*.*" "%%i\"
)
I'm trying to write a script that parses a text file to identify removable drives by Label. The goal is to create a copy script based on the found drives.
I'm able to parse the file but I can't seem to get the second pair of IF conditions to be true.
I may be misunderstanding how variables are evaluated in each loop of the FOR.
Thoughts appreciated.
EDIT: Expanding on the original request for clarity(it was late when I asked) The expanded goal is to identify one or many target drives to copy data from an SD card. Since the drives letter allocations vary from PC to PC and depend on which is plugged in first my idea was to use the labels as somewhat identifiable values and create a simple copy string along the lines of
XCOPY SourceDrive\*.jpg DestinationDrive1\<GetDate>\
XCOPY SourceDrive\*.jpg DestinationDrive2\<GetDate>\
End Edit
#echo off
cls
title "Detecting device drive letters..."
setlocal enabledelayedexpansion
set DestinationDrive1Found=
set DestinationDrive2Found=
set SourceDriveFound=
set SourceDrive=
set DestinationDrive1=
set DestinationDrive2=
echo Detecting device drive letters...
echo list volume > %systemdrive%\ListDrives.tmp
diskpart /s %systemdrive%\ListDrives.tmp > %systemdrive%\CurrentDrives.tmp
echo Checking drive IDs
FOR /F "skip=7 tokens=2-4" %%a IN (%systemdrive%\CurrentDrives.tmp) DO (
set "matchvar=%%c"
set "comparevar=!matchvar:DRIVE=!"
IF /I NOT "!comparevar!" == "%%c" IF "!DestinationDrive1Found!" NEQ 1 (
echo Drive %%b was found to be %%c and will be DESTINATION1
set DestinationDrive1Found=1
set DestinationDrive1=%%b )
IF /I NOT "!comparevar!" == "%%c" IF "!DestinationDrive1Found!" EQU 1 ( echo Drive %%b was found to be %%c and will be DESTINATION2
set DestinationDrive2Found=1
set DestinationDrive2=%%b )
)
File I'm parsing
Microsoft DiskPart version 10.0.10586
Copyright (C) 1999-2013 Microsoft Corporation.
On computer: NEWGLOOMWIN10
Volume ### Ltr Label Fs Type Size Status Info
---------- --- ----------- ----- ---------- ------- --------- --------
Volume 0 D DVD-ROM 0 B No Media
Volume 1 E Macintosh H HFS Partition 231 GB Healthy
Volume 2 C BOOTCAMP NTFS Partition 721 GB Healthy System
Volume 3 F TEAMXCAMRAX FAT Removable 1937 MB Healthy
Volume 4 G TEAMXDRIVEX NTFS Partition 465 GB Healthy
Volume 5 H TEAMYDRIVEY NTFS Partition 931 GB Healthy
Thanks to all who responded. Had #Mofi provided an answer I would have marked that response as the answer. In the end the removal of a four quote marks and the addition of an ELSE got me the answer I was looking for.
The script below gives me exactly what I'm after.
#echo off
cls
title "Detecting device drive letters..."
setlocal enabledelayedexpansion
set DestinationDrive1Found=
set DestinationDrive2Found=
set SourceDriveFound=
set SourceDrive=
set DestinationDrive1=
set DestinationDrive2=
echo Detecting device drive letters...
echo list volume > %systemdrive%\ListDrives.tmp
diskpart /s %systemdrive%\ListDrives.tmp > %systemdrive%\CurrentDrives.tmp
echo Checking drive IDs
FOR /F "skip=7 tokens=2-4" %%a IN (%systemdrive%\CurrentDrives.tmp) DO (
set "matchvar=%%c"
set "comparevar=!matchvar:CAMRA=!"
IF /I NOT "!comparevar!" == "%%c" (#echo Drive %%b was found to be %%c and will be SOURCE
set SourceDriveFound=1
set SourceDrive=%%b)
)
echo Checking drive IDs
FOR /F "skip=7 tokens=2-4" %%a IN (%systemdrive%\CurrentDrives.tmp) DO (
set "matchvar=%%c"
set "comparevar=!matchvar:DRIVE=!"
IF /I NOT "!comparevar!" == "%%c" IF !DestinationDrive1Found! NEQ 1 (
echo Drive %%b was found to be %%c and will be DESTINATION1
set DestinationDrive1Found=1
set DestinationDrive1=%%b
) ELSE (
IF /I NOT "!comparevar!" == "%%c" IF !DestinationDrive1Found! EQU 1 ( echo Drive %%b was found to be %%c and will be DESTINATION2
set DestinationDrive2Found=1
set DestinationDrive2=%%b ))
)
If you just want to set some variables to the drives allocated to any removable drive with the string DRIVE in it's 'label' then the following may provide you with an alternative not requiring elevation.
#Echo Off
For /F "UseBackQ Skip=1 Delims=" %%A In (`WMIC LogicalDisk Where^
"DriveType='2' And VolumeName Like '%%DRIVE%%'" Get Name`
) Do For /F %%B In ("%%A") Do Set/A "i+=1"&Call Set "_drv%%i%%=%%B"
Set _drv
Timeout -1
[Edits] - an example correctly stipulating a fixed drive
#Echo Off
For /F "UseBackQ Skip=1 Delims=" %%A In (`WMIC LogicalDisk Where^
"DriveType='3' And VolumeName Like '%%DRIVE%%'" Get Name`
) Do For /F %%B In ("%%A") Do Set/A "i+=1"&Call Set "_drv%%i%%=%%B"
Set _drv
Timeout -1
An example without stipulating the type of drive, (recommended based on comments).
#Echo Off
For /F "UseBackQ Skip=1 Delims=" %%A In (
`WMIC LogicalDisk Where "VolumeName Like '%%DRIVE%%'" Get Name`
) Do For /F %%B In ("%%A") Do Set/A "i+=1"&Call Set "_drv%%i%%=%%B"
Set _drv
Timeout -1
I need to select a disk in WinPE using DISKPART by label. I found this but I think it's outdated:
#Echo %dbg%Off
::
:: Find External drive and set it active
:: Lists Disk Information using Diskpart
::
SetLocal EnableDelayedExpansion
:: Type the frst 11 characters of the label of the drive here.
Set _FindLabel=Recovery
Call :_InitVars "%_FindLabel%"
"%SystemRoot%\system32\FSUTIL.exe">Nul 2>&1||Goto _NotAdmin
Echo.
Echo.Please wait, gathering info on the installed drives
Echo.
>"%_Dscr1%" Echo.List disk
For /F "Tokens=2" %%I In ('Diskpart /S "%_Dscr1%"^|Findstr /I /R /C:"Disk [0-9]"') Do (
(Echo.Select Disk %%I
Echo.Detail Disk)>>"%_OFile1%"
)
For /F "Tokens=1,2,3*" %%I In ('Diskpart /S "%_OFile1%"^|Findstr /I /R /C:"Disk [0-9]" /C:"Volume [0-9]"') Do (
If /I %%I==Disk (
Set _Tmp=%%J:
) Else (
Set _Label=%%L
Set _Label=!_Label:~,11!
>>"%_OFile2%" Echo.!_Tmp!%%J:!_Label!
))
If Exist "%_OFile1%" Del "%_OFile1%"
For /F "Usebackq Tokens=1-3 Delims=:" %%I In ("%_OFile2%") Do (
Set _Label=%%K
Set _Label=!_Label:~,11!
If "!_Label!"=="%_FindLabel%" (Set _Disk=%%I) & (Set _Label=%%K) & Goto _FDisk
>>"%_OFile1%" Echo.Volume %%J on Disk %%I has the Label %%K
)
Echo.
Echo.There is no drive connected that has the label of "%_FindLabel%"
Echo.These are the currently connected volumes:
Type "%_OFile1%"
:_Exit
Echo.
Pause
Goto _Cleanup
:_FDisk
(Echo.Select disk %_Disk%
Echo.Select Partition 1
Echo.Active)>"%_Dscr1%"
Diskpart /S "%_Dscr1%"
:_Cleanup
For %%I In ("%_Dscr1%" "%_OFile1%" "%_OFile2%") Do Del %%I>Nul 2>&1
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutines
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_NotAdmin
Ver|Findstr /I /C:"Version 5">Nul
If %Errorlevel%==0 (Set _Tmp1=5) & (Set _Tmp=a Computer Administrator account)
Ver|Findstr /I /C:"Version 6">Nul
If %Errorlevel%==0 (Set _Tmp1=6) & (Set _Tmp=an Elevated Command Prompt)
Echo.
Echo.This program must be run from %_Tmp%.
If %_Tmp1%==6 Echo.Please Right click the file, then click Run as Administrator
Echo.Exiting program
Goto _Exit
:_InitVars
For /F "Tokens=1 Delims==" %%I In ('Set _ 2^>Nul') Do Set %%I=
Set _Dscr1=%temp%\dpscr1.txt
Set _OFile1=%temp%\_OFile1.txt
Set _OFile2=%temp%\_OFile2.txt
Set _FindLabel=%~1
Set _FindLabel=%_FindLabel:~,20%
Call :_Cleanup
When I run it, it returns:
Please wait, gathering info on the installed drives
There is no drive connected that has the label of "Recovery"
These are the currently connected volumes:
Volume 1 on Disk 0 has the Label NTFS Part
Volume 2 on Disk 0 has the Label NTFS Part
Volume 3 on Disk 1 has the Label WINPE
It's showing the Fs (filesystem), not the label.
Diskpart output (list volume)
Volume ### Ltr Label Fs Type Size Status Info
---------- --- ----------- ----- ---------- ------- --------- --------
Volume 0 D USBBOOT UDF DVD-ROM 329 MB Healthy
Volume 1 Windows NTFS Partition 40 GB Healthy
Volume 2 Recovery NTFS Partition 19 GB Healthy
Volume 3 Z WINPE FAT32 Removable 7632 MB Healthy
Not sure how to modify this to get it to select the Label column.
As there is missing valid drive letter in some lines of the Diskpart output (list volume, column ltr), then tokens in line #19
For /F "Tokens=1,2,3*" %%I In ('Diskpart /S "%_OFile1%"^|Findstr /I /R /C:"Disk [0-9]" /C:"Volume [0-9]"') Do (
display as follows (and we have our culprit now):
%%I %%J %%K %%L
Volume 0 D USBBOOT UDF DVD-ROM 329 MB ...
Volume 1 Windows NTFS Partition 40 GB Healthy
Volume 2 Recovery NTFS Partition 19 GB Healthy
Volume 3 Z WINPE FAT32 Removable 7632 MB ...
Therefore you could use next snippet code (instead of lines 19..26) in your script:
For /F "Tokens=1*" %%I In ('Diskpart /S "%_OFile1%"^|Findstr /I /R /C:"Disk [0-9]" /C:"Volume [0-9]"') Do (
If /I %%I==Disk (
Set _Tmp=%%J:
) Else (
Set _Label=%%J
Set _Label=!_Label:~10,11!
>>"%_OFile2%" Echo.!_Tmp!%%J:!_Label!
))
Care this change interferes _Tmp variable!
I have a small side project I've been working on and i want to add an option to scan the computer and find which drives are connected and what they are(USB, Hard drive, Disc drive, SSD, etc.)
this is what i have so far.
echo off
:start
color 0a
cls
title Search
echo To search enter the drive and term you wish to search below.
echo ---------------------------------------------------------------------------
set inputdrive=
set /p inputdrive=Drive:
set input=
set /p input=Search:
dir %inputdrive%:\%input% /s /b
pause
echo search again?
set inputsearch=
set /p inputsearch=(Y/N)
if %inputsearch%==Y goto start
if %inputsearch%==N exit
I plan on adding an option to scan for connected drives at the beginning of the script.
You can use WMIC to get the info without the need for administrator rights.
Here is code that gives a nice listing of all available drives, including removable media drives that do not have any media installed:
#echo off
setlocal
set ^"driveTypes=^
0Unknown :^
1No Root Directory:^
2Removable Media :^
3Local Disk :^
4Network Drive :^
5CD/DVD :^
6RAM Disk :^"
echo Available Drives:
echo(
echo ID Drive Type Volume Name
echo -- ----------------- -----------
for /f "skip=1 tokens=1-3" %%A in (
'"wmic logicalDisk get DeviceID, DriveType, VolumeName"'
) do if "%%B" neq "" (
setlocal enableDelayedExpansion
for /f "delims=:" %%D in ("!driveTypes:*%%B=!") do (
endlocal
echo %%A %%D %%C
)
)
Just a bit more code will restrict the list to only drives that are available with media:
#echo off
setlocal
set ^"driveTypes=^
0Unknown :^
1No Root Directory:^
2Removable Media :^
3Local Disk :^
4Network Drive :^
5CD/DVD :^
6RAM Disk :^"
echo Available Drives:
echo(
echo ID Drive Type Volume Name
echo -- ----------------- -----------
for /f "skip=1 tokens=1-3" %%A in (
'"wmic logicalDisk get DeviceID, DriveType, VolumeName"'
) do if "%%B" neq "" dir %%A >nul 2>nul && (
setlocal enableDelayedExpansion
for /f "delims=:" %%D in ("!driveTypes:*%%B=!") do (
endlocal
echo %%A %%D %%C
)
)
fsutil fsinfo drives
fsutil fsinfo drivetype c:
But you need to have administrator rights to use it
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