标题: [数值计算] 不支持32位以上的字符,批处理计算磁盘空间怎样以GB为单位? [打印本页]
作者: 范君健 时间: 2019-10-11 13:18 标题: 不支持32位以上的字符,批处理计算磁盘空间怎样以GB为单位?
- @echo off
- rem write to log
- set filename=磁盘空间日志.log
- echo -------------------这是一条没有感情的分割线------------------------- >>%filename%
- echo Start >>%filename%
- echo (%date% %time%) >>%filename%
-
- net use O: \192.168.0.127\127d Kovn2020 /user:administrator
- net use P: \192.168.0.128\128d Kovn2020 /user:administrator
- net use Q: \192.168.0.130\130d Kovn2020 /user:administrator
-
- setlocal enabledelayedexpansion
- :Main
- cls
- set /a num2=1024*1024
- for %%i 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 %%i: (
- set num3_str=
- set num4=
- for /f "tokens=3" %%j in ('dir /-c %%i:') do set num1=%%j
- if not "!num1!"=="0" (
- call :loop
- for /f "delims=0 tokens=" %%k in ("!num3_str!") do set num3_str=%%k
- echo.
- echo %%i 盘剩余空间为 !num3_str! MB
- echo %%i !num3_str! MB >>%filename%
- echo.
- )
- )
- )
- )
- net use O: /delete /y
- net use P: /delete /y
- net use Q: /delete /y
- pause
- goto Main
- :loop
- :: 求商
- set /a num3=%num4%%num1:~0,1%/%num2%
- :: 求商序列
- set num3_str=%num3_str%%num3%
- :: 求余
- set /a num4=%num4%%num1:~0,1%%%%num2%
- if %num4% equ 0 set num4=
- set num1=%num1:~1%
- if not "%num1%"=="" goto loop
- goto :eof
-
- echo End >>%filename%
- echo (%date% %time%) >>%filename%
复制代码
怎么才能使导出的文件单位是G呢? 不支持32位以上的字符,能不能曲线救国?通过其他办法解决一下
作者: Batcher 时间: 2019-10-11 14:13
推荐试试PowerShell
http://bbs.bathome.net/thread-26200-1-1.html
作者: terse 时间: 2019-10-11 15:36
本帖最后由 terse 于 2019-10-11 15:42 编辑
CALL出来影响效率 直接FOR里计算吧- set md=1048576
- for %%i 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 %%i: (
- set num3_str=
- set num4=
- for /f "tokens=3" %%j in ('dir /-c %%i:') do set num1=%%j00
- set len=0
- set s=!num1!
- for %%j in (16 8 4 2 1) do if "!s:~%%j!" neq "" set/a len+=%%j&set "s=!s:~%%j!"
- set/a "r=!num1:~,8!%%md,m=!num1:~,8!/md"
- for /l %%j in (8 1 !len!) do (
- set/a "q=!r!!num1:~%%j,1!/md,r=!r!!num1:~%%j,1!%%md"
- set "m=!m!!q!"
- )
- set /a g=m/1024
- echo %%i 盘剩余空间为 !g:~,-2!.!g:~-2! G
- echo %%i 盘剩余空间为 !m:~,-2!.!m:~-2! MB
- echo.
- )
- )
-
-
- PAUSE
复制代码
作者: 小渣飞 时间: 2019-10-12 09:25
回复 3# terse
要加setlocal enabledelayedexpansion
作者: ygqiang 时间: 2019-10-12 10:23
- //&cls&cscript -nologo -e:jscript "%~f0"&ping 127.0.0.1 -n 15 >nul 2>nul&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+' 字节';
- s=s+new Array(10-s.length).join(" ");
- 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 时间: 2019-10-12 10:24
- @echo off & setlocal enabledelayedexpansion
- echo 各分区剩余空间:
- 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!
- )
- )
- )
- ping 127.0.0.1 -n 15 >nul 2>nul&exit
复制代码
作者: ygqiang 时间: 2019-10-12 10:24
- @echo off
- echo ——————————————————————————————————————— >nul 2>nul
- echo 判断硬盘是MBR分区还是GTP分区
-
- 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%'")
- For Each objDisk in colDisks
- 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
- Next
- WScript.Echo s
- ' 把显示的内容保存在TXT文件
- ' WScript.Echo s替换为下面的代码
- ' CreateObject("Scripting.filesystemobject").CreateTextFile("结果.txt",2).Write 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"
- If InStrRev(sText, "*")>0 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
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |