检测CMD可移动驱动器盘符
我试图写一个脚本,它可以检测我的所谓的UUIUSB可移动驱动器的盘符,然后在其上创建文件夹。我写的,当单独运行,工作CMD几个命令。然而,当我把它们放入一个bat文件,我总是得到一些错误。这里有一个bat文件的命令:
I'm trying to write a script, which will detect the letter of my USB Removable Drive called "UUI" and then create folder on it. I've written few commands for CMD which, when run separately, work. However when I put them into a bat file, I always get some errors. Here are the commands in a bat file:
for /F "tokens=1 delims= " %i in ('WMIC logicaldisk where "DriveType=2" list brief ^| c:\windows\system32\find.exe "UUI"') do (echo %i > drive.txt)
set /p RemovableDriveLetter2= < drive.txt
del /F /Q drive.txt
set RemovableDriveLetter=%RemovableDriveLetter2:~0,1%
%RemovableDriveLetter%:
md MyNewFolder
cd MyNewFolder
当我去和cmd.exe的通过调用myScript.bat或呼叫myScript.bat运行该文件,我得到一个错误:
When I go to cmd.exe and run the file by calling "myScript.bat" or "call myScript.bat", I get an error:
C:\\用户\\ UUI \\桌面> myScript.bat
C:\Users\UUI\Desktop>myScript.bat
\\ WINDOWS \\ SYSTEM32 \\ FIND.EXE在这个时候意外。
\windows\system32\find.exe was unexpected at this time.
C:\\用户\\ UUI \\桌面> FOR / F标记= 1 delims =\\ WINDOWS \\ SYSTEM32 \\ FIND.EXEUUI')做(回声I> drive.txt)
C:\Users\UUI\Desktop>for /F "tokens=1 delims= " \windows\system32\find.exe "UUI"') do (echo i > drive.txt)
C:\\用户\\ UUI \\桌面>
C:\Users\UUI\Desktop>
我可以看到MyNewFolder未创建。然而,当我复制的所有行和在CMD本身(在bat文件例如未)运行它们并且由一个运行它们之一,它是在cmd.exe的实例中充分发挥作用。
I can see that MyNewFolder was not created. However, when I copy all lines and run them in CMD as such (e.g. not in the .bat file) and run them one by one, it is fully functional within the cmd.exe instance.
如何创建一个BAT文件,将成功运行并检测我的可移动驱动器的驱动器号没有问题?或者我如何解决错误\\ WINDOWS \\ SYSTEM32 \\ FIND.EXE在这个时候意外的。
How can I create bat a file, which will successfully run and detects the drive letter of my removable drive without issues? Or how can I solve the error "\windows\system32\find.exe was unexpected at this time."?
您需要的双的用来标记%
迹象 FOR
在一个批处理脚本(循环控制变量的.bat
或的.cmd
),即使用 %%我
而不是%I
在纯命令行使用。
You need to double the %
sign used to mark a FOR
loop control variable in a batch script (.bat
or .cmd
), i.e. use %%i
instead of %i
used in pure CLI.
然而,还有另一种可能的方法如何,解析 WMIC
输出。
另见戴夫贝纳姆的的 WMIC
和 FOR / F
:一个尾随&LT修复; CR&GT;
问题
However, there is another possible approach how-to parse wmic
output.
See also Dave Benham's WMIC
and FOR /F
: A fix for the trailing <CR>
problem
@echo OFF
SETLOCAL enableextensions
set "USBCounter=0"
for /F "tokens=2 delims==" %%G in ('
WMIC logicaldisk where "DriveType=2" get DeviceID /value 2^>NUL ^| find "="
') do for /F "tokens=*" %%i in ("%%G") do (
set /A "USBCounter+=1"
echo %%i
rem your stuff here
)
echo USBCounter=%USBCounter%
rem more your stuff here
ENDLOCAL
goto :eof
下面的为
环路
-
%%摹
来检索的DeviceID
值; -
%%我
删除结束的回车的中返回的值:WMIC
行为:每个输出符合0x0D0D0A
结束>(CR + CR + LF
),而不是常见的0x0D0A
(CR + LF
)。
-
%%G
to retrieve theDeviceID
value; -
%%i
to remove the ending carriage return in the value returned:wmic
behaviour: each output line ends with0x0D0D0A
(CR+CR+LF
) instead of common0x0D0A
(CR+LF
).
一个可以使用标题
或名称
而不是的DeviceID
:
One could use Caption
or Name
instead of DeviceID
:
==>WMIC logicaldisk where "DriveType=2" get /value | find ":"
Caption=F:
DeviceID=F:
Name=F:
请注意可能存在的没有的或的更多的磁盘present有的DriveType = 2
:
Note there could be no or more disks present having DriveType=2
:
==>WMIC logicaldisk where "DriveType=2" get /value | find ":"
No Instance(s) Available.
==>WMIC logicaldisk where "DriveType=2" list brief
DeviceID DriveType FreeSpace ProviderName Size VolumeName
F: 2 2625454080 3918512128 HOMER
G: 2 999600128 1029734400 LOEWE
剧本输出作为的没有的,然后的有一个的,然后的两个的分别为USB驱动器(S):
Script output for no, then one and then two USB drive(s), respectively:
==>D:\bat\SO\31356732.bat
USBCounter=0
==>D:\bat\SO\31356732.bat
F:
USBCounter=1
==>D:\bat\SO\31356732.bat
F:
G:
USBCounter=2
==>