问题
I am trying to copy files from my USB drive to my PC automatically by using a batch file (.bat)
I know I can do something like that
xcopy "C:\xxxx\xxxx\xxxx.xxx" "C:\xxxx\xxx\xxxx\xxx.exe" /E /H /I
But I faced a little problem...
What if I don't know the exact letter of the drive, where the USB is inserted? How am I able to copy those files automatically? If this is possible, can someone please post a simple "xcopy" command from USB to PC.
Thanks!
回答1:
Can you add a generic file to the root of the USB drive so you can test for it to discover the drive letter of the USB drive? For instance, if you created a file called USBDrive1
in the root of the USB drive, you could do something like this:
for %%i in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist %%i:\USBDrive1 set USBDrive=%%i
Then you can use %USBDrive%
for the drive letter in the rest of your batch file. Like this:
xcopy %USBDrive%:\*.* C:\Some\Path ...
回答2:
Place your batch file in your USB drive using this syntax:
xcopy %~d0\folder\*.* c:\target\ /E /H /I
回答3:
This should work:
set "USBDrive="
for /F "tokens=1,2" %%a in ('wmic logicaldisk get DeviceID^,DriveType') do if %%b equ 2 set USBDrive=%%a
if not defined USBDrive (
echo USB not connected
) else (
echo USB on drive %USBDrive%
)
来源:https://stackoverflow.com/questions/22277180/not-knowing-the-destination-of-usb-in-cmd