批处理获取U盘盘符和可用容量
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
批处理获取U盘盘符和可用容量
批处理获取U盘盘符和可用容量
@echo off&color b
for /f "skip=1" %%a in ('wmic logicaldisk where "drivetype='2'" get DeviceID 2^>nul')
do set p=%%a
if "%p%"=="" echo.&echo 未找到可移动磁盘
&echo.&pause&goto :eof
for /f "tokens=2 delims==" %%i in ('wmic logicaldisk where caption^=^"%p%^" get
freespace /value 2^>nul') do set m=%%i
set m3=%m:~0,7%
set m=%m:~7%
call :loop "1048576"
:loop1
if %m2% LSS 1024 echo.&cls&echo.&echo.&echo. %p% 盘剩余空间为 %m2% MB&pause>nul&goto :eof
set m=%m2%
set m3=%m:~0,3%
set/a m=%m:~3%*1000
for %%i in (m1 m2) do set %%i=
call :loop "1024"
for /f "delims=0 tokens=*" %%i in ("%m2%") do set m=%%i
set g=%m:~0,-3%.
set g=%g%%m:~-3,3% G
cls
:m
echo.
echo.
echo. %p% 盘剩余空间为 %g%
echo.
pause>nul
goto :eof
:loop
set /a m1=%m3%%m:~0,1%/%~1
set m2=%m2%%m1%
set /a m3=%m3%%m:~0,1%%%%~1
if %m3% equ 0 set m3=
set m=%m:~1%
if not "%m%"=="" goto loop
goto :eof
用批处理写了个工具,对U盘拷贝文件(写满磁盘)然后比对文件,验证拷进去的资料是否正确。
现在问题是不能自动判断U盘的容量,就不知道要写满U盘的话要拷多大的文件进去,现在只能固定死测试512M的,另外不同电脑插入U盘后显示的盘符也不一样,所以我做了很多个批处理文件,每个对应处理一个盘符。
我现在需要解决上面的问题,就是自动获取移动磁盘盘符和容量,然后赋值给2个变量,再去做相应处理,请高手帮忙。
@echo off
fsutil >nul 2>nul
if "%errorlevel%"=="1" echo 非管理员身份运行&ping/n 5 127.1>nul&exit
if "%errorlevel%"=="9009" echo 运行环境必须是 Windows XP Professional 以上系统&ping/n 5 127.1>nul&exit
for /f "skip=2 tokens=2,3 delims=," %%i in ('WMIC LogicalDisk Where "DriveType=2 and SupportsDiskQuotas=FALSE" Get DeviceID^,FreeSpace /format:csv 2^>nul') do call :xxx %%i %%j
if not defined freesize echo 未找到可移动磁盘
pause&exit
:xxx
set freesize=%2
if not defined freesize goto:eof
if exist "%1\test.txt" del /q "%1\test.txt"
echo 正在写入%freesize%字节大小的测试文件到%~1盘...
fsutil file createnew "%1\test.txt" %freesize% >nul&&echo %1 盘文件拷贝测试成功!||echo %1 盘文件拷贝测试失败
del "%1\test.txt" 2>nul
echo.
goto:eof 该代码希望大家能收藏备用,它的价值在于能判断U盘厂商是否在flash芯片上动了手脚(奸商行为能将U盘容量加倍)去电脑城买U盘时带上这个代码肯定不让你吃亏!
以下代码适用繁体中文系统。
rem 獲取磁盤容量
@echo off
for /f "skip=1 delims= " %%a in ('"Wmic LogicalDisk Where DriveType="2" Get DeviceID|find /v "N:""') do call :xxx "%%a"
pause
:xxx
if "%~1"=="" goto:eof
if exist "%~1\test.txt" del /q "%~1\test.txt"
for /f "tokens=3" %%b in ('dir/-c "%~1\"^|find "可用字節"') do set freesize=%%b echo 正在寫入%freesize%字節大小的測試文件到%~1盤...
fsutil file createnew "%~1\test.txt" %freesize% >nul&&echo %~1 盤文件拷貝
測試成功!||echo 盤文件拷貝測試失敗
del "%~1\test.txt" 2>nul
echo.
goto:eof
繁体系统第二种代码:
@echo off
for /f "skip=1 delims= " %%a in ('"Wmic LogicalDisk Where DriveType="2" Get DeviceID|find /v "A:""') do call :xxx "%%a"
pause
:xxx
if "%~1"=="" goto:eof
if exist "%~1\test.txt" del /q "%~1\test.txt"
for /f "tokens=3" %%b in ('dir/-c "%~1\"^|find " 位元組可用 "') do set freesize=%%b echo 正在写入%freesize%字节大小的测试文件到%~1盘...
fsutil file createnew "%~1\test.txt" %freesize% >nul&&echo %~1 盘文件拷贝测试成功!||echo 盘文件拷贝测试失败
del "%~1\test.txt" 2>nul
echo.
goto:eof
Powered by 无名小鬼 。