标题: [系统相关] [已解决]bat批处理文件,生成下面这个vbs代码文件。哪些地方,需要注意加上特殊符号? [打印本页]
作者: ygqiang 时间: 2014-12-16 23:28 标题: [已解决]bat批处理文件,生成下面这个vbs代码文件。哪些地方,需要注意加上特殊符号?
本帖最后由 ygqiang 于 2014-12-17 00:26 编辑
[已解决]通过bat批处理文件,生成下面这个vbs代码文件。哪些地方,需要注意加上特殊符号?
比如echo i = i + ^1>>c:\diskpart.vbs- Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
- Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
-
- For Each objDisk in colDisks
- i = i + 1
- s = s & "硬盘" & i & ":" & vbTab
- s = s & "大小:" & GetSize(objDisk.Size) & vbTab
- s = s & "型号:" & objDisk.Caption & 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
-
- Set fso = CreateObject("Scripting.FileSystemObject")
- fso.OpenTextFile("c:\diskpart.txt", 2, true).Write s
-
- Function GetSize(intSize)
- If intSize/1024/1024 > 1024 Then
- GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
- Else
- Getize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
- End If
- End Function
复制代码
作者: ygqiang 时间: 2014-12-17 00:25
解决了。。。- @echo off
-
- ———————————————————————————————————————
- del /q c:\diskinfo.* >nul 2>nul
-
-
- ———————————————————————————————————————
- echo Set objWMI = GetObject("winmgmts:\\.\root\cimv2")>>c:\diskinfo.vbs
- echo Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%%'")>>c:\diskinfo.vbs
-
- echo For Each objDisk in colDisks>>c:\diskinfo.vbs
- echo i = i + ^1>>c:\diskinfo.vbs
- echo s = s ^& "硬盘" ^& i ^& ":" ^& vbTab>>c:\diskinfo.vbs
- echo s = s ^& "大小:" ^& GetSize(objDisk.Size) ^& vbTab>>c:\diskinfo.vbs
- echo s = s ^& "型号:" ^& objDisk.Caption ^& vbCrLf>>c:\diskinfo.vbs
- echo strDiskID = Replace(objDisk.DeviceID, "\", "\\")>>c:\diskinfo.vbs
-
- echo Set colPartitions = objWMI.ExecQuery _>>c:\diskinfo.vbs
- echo ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" ^& strDiskID ^& """}" _>>c:\diskinfo.vbs
- echo ^& " where AssocClass=Win32_DiskDriveToDiskPartition")>>c:\diskinfo.vbs
-
- echo For Each objPartition in colPartitions>>c:\diskinfo.vbs
- echo strPartId = objPartition.DeviceID>>c:\diskinfo.vbs
-
- echo Set colLogicalDisks = objWMI.ExecQuery _>>c:\diskinfo.vbs
- echo ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" ^& strPartId ^& """} where AssocClass=Win32_LogicalDiskToPartition")>>c:\diskinfo.vbs
-
- echo For Each objLogicalDisk in colLogicalDisks>>c:\diskinfo.vbs
- echo size = objLogicalDisk.Size>>c:\diskinfo.vbs
- echo free = objLogicalDisk.Freespace>>c:\diskinfo.vbs
- echo used = FormatNumber((size - free) / size * 100, 2, true) ^& "%%">>c:\diskinfo.vbs
- echo s = s ^& "分区" ^& objLogicalDisk.DeviceID ^& vbTab>>c:\diskinfo.vbs
- echo s = s ^& "大小:" ^& GetSize(size) ^& vbTab>>c:\diskinfo.vbs
- echo s = s ^& "剩余:" ^& GetSize(free) ^& vbTab>>c:\diskinfo.vbs
- echo s = s ^& "使用率:" ^& used ^& vbCrLf>>c:\diskinfo.vbs
- echo Next>>c:\diskinfo.vbs
- echo Next>>c:\diskinfo.vbs
- echo s = s ^& vbCrLf>>c:\diskinfo.vbs
- echo Next>>c:\diskinfo.vbs
-
- echo Set fso = CreateObject("Scripting.FileSystemObject")>>c:\diskinfo.vbs
- echo fso.OpenTextFile("c:\diskinfo.txt", 2, true).Write s>>c:\diskinfo.vbs
-
- echo Function GetSize(intSize)>>c:\diskinfo.vbs
- echo If intSize/1024/1024 ^> 1024 Then>>c:\diskinfo.vbs
- echo GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) ^& "GB">>c:\diskinfo.vbs
- echo Else>>c:\diskinfo.vbs
- echo Getize = FormatNumber(intSize/1024/1024, 2, true) ^& "MB">>c:\diskinfo.vbs
- echo End If>>c:\diskinfo.vbs
- echo End Function>>c:\diskinfo.vbs
-
-
- ping 127.0.0.1 -n 2 >nul 2>nul
-
-
- ———————————————————————————————————————
- start c:\diskinfo.vbs
- ping 127.0.0.1 -n 2 >nul 2>nul
-
- cls
- type c:\diskinfo.txt
-
- ping 127.0.0.1 -n 15 >nul 2>nul & del /q c:\diskinfo.* & exit/b
复制代码
作者: Batcher 时间: 2014-12-17 09:26
我来提供一个不需要转义的方案- @echo off
- for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (
- more +%%i "%~f0" > new.vbs
- )
- goto :eof
-
- :DynamicScript
- Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
- Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
-
- For Each objDisk in colDisks
- i = i + 1
- s = s & "硬盘" & i & ":" & vbTab
- s = s & "大小:" & GetSize(objDisk.Size) & vbTab
- s = s & "型号:" & objDisk.Caption & 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
-
- Set fso = CreateObject("Scripting.FileSystemObject")
- fso.OpenTextFile("c:\diskpart.txt", 2, true).Write s
-
- Function GetSize(intSize)
- If intSize/1024/1024 > 1024 Then
- GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
- Else
- Getize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
- End If
- End Function
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |