标题: [数值计算] [已解决]求列举磁盘各分区剩余空间的批处理脚本(帮修改也可以) [打印本页]
作者: habulu 时间: 2015-10-17 22:57 标题: [已解决]求列举磁盘各分区剩余空间的批处理脚本(帮修改也可以)
本帖最后由 habulu 于 2015-10-19 21:02 编辑
有个磁盘空间检查脚本是这样的:- echo **********************************%date%********************************>C:\mail2.txt
- echo.>>C:\mail2.txt
- echo.>>C:\mail2.txt
- echo ══════════════════════════════>>C:\mail2.txt
- echo 磁盘分区 剩余空间(GB) >>C:\mail2.txt
- rem echo ________________________________________________ >>C:\mail2.tx
- setlocal enabledelayedexpansion
- for %%a in (C 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 %%a:\nul (
- for /f "tokens=3" %%b in ('dir /-c %%a:\^|find "可用字节"') do (
- set freesize=%%b
- set /a freesize=!freesize:~0,-3!/1049/1024>nul 2>nul
- set freesize= !freesize!
- echo %%a: !freesize:~-17! >>C:\mail2.txt
- )
- )
- )
- echo ═════════════════════════════>>C:\mail2.txt
复制代码
但是当D盘空间超过2.5T后,就没转换为G了(C盘还是有转换为G的,但D盘大约2.5T,就没转换了)
══════════════════════════════
磁盘分区 剩余空间(GB)
C: 75
D: 2572068761600
═════════════════════════════
求修改脚本,或给个类似脚本
作者: pcl_test 时间: 2015-10-18 00:38
拒绝回复不安版规发帖的求助
作者: habulu 时间: 2015-10-18 01:15
额 标题不符么,改了
作者: CrLf 时间: 2015-10-18 01:49
回复 3# habulu
代码未放 code 里,这回帮你改了,下次发代码的时候记得戳一下文本框上方倒数第三个按钮
不过这是小问题,发帖时标题和内容清晰明确最重要
作者: DAIC 时间: 2015-10-18 14:03
set /a 命令无法进行大数计算,参考 set 教程第6页:
http://bbs.bathome.net/thread-31727-1-1.html
作者: pcl_test 时间: 2015-10-18 16:43
- //&cls&cscript -nologo -e:jscript "%~f0"&pause&exit
- function GetSize(val)
- {
- var s ='';
- if(val >= 1125899906842624)
- s = parseInt((val/1125899906842624)*100)/100+' PB';
- else if(val >= 1099511627776)
- s = parseInt((val/1099511627776)*100)/100+' TB';
- else if (val >= 1073741824)
- s = parseInt((val/1073741824)*100)/100+' GB';
- else if (val >= 1048576)
- s = parseInt((val/1048576)*100)/100+' MB';
- else if (val >= 1024)
- s = parseInt((val/1024)*100)/100+' KB';
- else s = val+' 字节';
- return s;
- }
-
- var fso = new ActiveXObject('Scripting.FileSystemObject');
- var enu = new Enumerator(fso.Drives);
- var str = '';
- var head = '盘符 类型 总容量 已用容量 剩余容量 使用率 文件系统\r\n'+
- '--------------------------------------------------------------';
- for (; !enu.atEnd(); enu.moveNext())
- {
- var drive = enu.item();
- if (drive.isReady)
- {
- var type = '';
- var UsedSpace = drive.TotalSize-drive.FreeSpace;
- switch(drive.DriveType)
- {
- case 0:type = 'Unknown';break;
- case 1:type = 'Removable';break;
- case 2:type = 'Fixed';break;
- case 3:type = 'Network';break;
- case 4:type = 'CD-ROM';break;
- case 5:type = 'RAM disk';break;
- default:type = 'Unknown';
- }
- str += drive.DriveLetter+' '+type+' '+
- GetSize(drive.TotalSize)+' '+
- GetSize(UsedSpace)+' '+
- GetSize(drive.FreeSpace)+' '+
- parseInt(UsedSpace/drive.TotalSize*100)+'% '+
- drive.FileSystem+'\r\n';
- }
- }
- WSH.echo(head+'\r\n'+str);
复制代码
作者: ygqiang 时间: 2015-10-18 17:27
回复 6# pcl_test
结果正确。。但是显示的结果,上下能否对齐呢?多谢。。
作者: CrLf 时间: 2015-10-18 18:14
本帖最后由 CrLf 于 2015-10-18 18:19 编辑
回复 7# ygqiang
可自行在代码中加入tab字符左对齐,就是键盘最左边那个tab,不懂vbs没关系,靠感觉多试几次就出来了
作者: ygqiang 时间: 2015-10-20 07:30
回复 6# pcl_test
显示的结果,上下还是不对齐?麻烦了。。
作者: ygqiang 时间: 2015-10-20 07:33
回复 8# CrLf
不同磁盘/容量情况不一样。。
要实现上下对齐,好像比较难吧?
作者: 依山居 时间: 2015-10-23 22:25
神手党一辈子啊。
作者: bailong360 时间: 2015-10-25 19:06
回复 8# CrLf
可是pcl写的是js
作者: CrLf 时间: 2015-10-25 22:44
回复 10# ygqiang
你试过 tab 了吗?
作者: ygqiang 时间: 2015-10-26 08:39
回复 13# CrLf
下面是另一个代码。。- @echo off
-
- echo ——————————————————————————————————————— >nul 2>nul
- if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
-
- for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (
- more +%%i "%~f0" > c:\diskinfo.vbs
- )
-
- ping 127.0.0.1 -n 2 >nul 2>nul
-
- cls
- cscript /nologo c:\diskinfo.vbs
- ping 127.0.0.1 -n 2 >nul 2>nul
-
- CScript /NoLogo c:\diskinfo.vbs>c:\diskinfo.txt
- ping 127.0.0.1 -n 2 >nul 2>nul
-
- cls
- Type c:\diskinfo.txt
-
- ping 127.0.0.1 -n 5 >nul 2>nul & if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
-
-
-
- echo ——————————————————————————————————————— >nul 2>nul
- if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
-
- set "vbs=c:\diskinfo.vbs"
- set "log=c:\diskinfo.log"
- for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (more +%%i "%~f0" > "%vbs%")
- CScript //NoLogo "%vbs%" > "%log%"
-
- cls
- type "%log%"
-
- ping 127.0.0.1 -n 5 >nul 2>nul & if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul) & exit/b
-
- goto :eof
-
-
-
- echo ——————————————————————————————————————— >nul 2>nul
- :DynamicScript
- Main
- Sub Main()
- Dim objWMI, colDisks
- Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
- Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
- ' 获取硬盘最大序号
- nIndexMax = 0
- For Each objDisk in colDisks
- If Cint(objDisk.Index) > nIndexMax Then nIndexMax = Cint(objDisk.Index)
- Next
- ' 按序号取信息
- For i = 0 To nIndexMax
- For Each objDisk in colDisks
- ' 只取该序号信息
- If objDisk.Index = i Then
- s = s & "硬盘" & objDisk.Index & ":" & vbTab
- s = s & "大小:" & GetSize(objDisk.Size) & vbTab
- s = s & "型号:" & objDisk.Caption & vbTab
- s = s & "分区表:" & IsGPT(objDisk.Index) & vbCrLf
- strDiskID = Replace(objDisk.DeviceID, "\", "\\")
- Set colPartitions = objWMI.ExecQuery _
- ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDiskID & """}" _
- & " where AssocClass=Win32_DiskDriveToDiskPartition")
- For Each objPartition in colPartitions
- strPartId = objPartition.DeviceID
- Set colLogicalDisks = objWMI.ExecQuery _
- ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & strPartId _
- & """} where AssocClass=Win32_LogicalDiskToPartition")
- For Each objLogicalDisk in colLogicalDisks
- size = objLogicalDisk.Size
- free = objLogicalDisk.Freespace
- used = FormatNumber((size - free) / size * 100, 2, true) & "%"
- s = s & "分区" & objLogicalDisk.DeviceID & vbTab
- s = s & "大小:" & GetSize(size) & vbTab
- s = s & "剩余:" & GetSize(free) & vbTab
- s = s & "使用率:" & used & vbCrLf
- Next
- Next
- s = s & vbCrLf
- End If
- Next
- Next
-
- WScript.Echo s
- End Sub
-
- ' 格式化
- Function GetSize(intSize)
- If intSize/1024/1024 > 1024 Then
- GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
- Else
- GetSize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
- End If
- End Function
-
- ' 获取指定硬盘的分区表类型(GPT/MBR)
- Function IsGPT(ByVal nDiskIndex)
- IsGPT = "MBR"
- Dim wso, sLogFile, sText
- Set wso = CreateObject("WScript.Shell")
- sLogFile = wso.ExpandenVironmentStrings("%temp%\diskpart.log")
- wso.Run "cmd /c ""chcp 437 & cls & (echo list disk | diskpart | find /i ""Disk " & nDiskIndex & """) >""" & sLogFile & """ "" ", 0, False
- Call TxtFile(sLogFile, 1, -2, sText)
- If Trim(Right(sText,3)) = "*" Then IsGPT = "GPT"
- End Function
-
- ' 对文本指定编码进行读写操作2
- 'nRW: 1只读, 2只写, 8追加 'nCharset: -2(系统), -1(Unicode), 0(ASCII)
- Sub TxtFile(ByVal FileName, ByVal nRW, ByVal nCharset, ByRef sText)
- Dim fso : Set fso = CreateObject("Scripting.filesystemobject")
- If sText <> "" And (nRW = 2 Or nRW = 8) Then
- fso.OpenTextFile(FileName, nRW, True, nCharset).Write sText
- ElseIf fso.FileExists(FileName) And nRW = 1 Then
- If fso.GetFile(FileName).Size > 0 Then _
- sText = fso.OpenTextFile(FileName, nRW, False, nCharset).ReadAll
- End If
- End Sub
-
- ' 以管理员身份运行
- Sub RunAsAdmin()
- Dim objItems, objItem, strVer, nVer
- Set objItems = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
- For Each objItem In objItems
- strVer = objItem.Version
- Next
- nVer = Split(strVer, ".")(0) & Split(strVer, ".")(1)
- If nVer >= 60 Then
- Dim oShell, oArg, strArgs
- Set oShell = CreateObject("Shell.Application")
- If Not WScript.Arguments.Named.Exists("ADMIN") Then
- For Each oArg In WScript.Arguments
- strArgs = strArgs & " """ & oArg & """"
- Next
- strArgs = strArgs & " /ADMIN:1"
- Call oShell.ShellExecute("WScript.exe", """" & WScript.ScriptFullName & """" & strArgs, "", "runas", 1)
- Set oShell = Nothing
- WScript.Quit(0)
- End If
- Set oShell = Nothing
- End If
- End Sub
复制代码
作者: terse 时间: 2015-10-26 10:50
修改下一楼的- @echo off & setlocal enabledelayedexpansion
- set md=1048576
- for %%a in (C 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 %%a:\nul (
- for /f "tokens=3" %%b in ('dir /-c %%a:\^|find "可用字节"') do (
- set "fsize=%%b00"
- set s=!fsize!&set len=0
- if "!s:~16!" neq "" set len=16&set "s=!s:~16!"
- set s=!s!FEDCBA9876543210
- set/a "len+=0x!s:~16,1!"
- set/a "r=!fsize:~,8!%%md,t=!fsize:~,8!/md"
- for /l %%j in (8 1 !len!) do (
- set/a "q=!r!!fsize:~%%j,1!/md,r=!r!!fsize:~%%j,1!%%md"
- set "t=!t!!q!"
- )
- set /a t/=1024
- if "!t:~,-2!" == "" (set g=0&set t=00!t!) else set "g=!t:~,-2!"
- set g=!g!.!t:~-2!
- echo %%a: !g!
- )
- )
- )
- pause
复制代码
作者: 樵夫 时间: 2018-4-2 16:12
回复 6# pcl_test
怎么输入到文本呢?
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |