[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[系统相关] [已解决]bat批处理文件,生成下面这个vbs代码文件。哪些地方,需要注意加上特殊符号?

本帖最后由 ygqiang 于 2014-12-17 00:26 编辑

[已解决]通过bat批处理文件,生成下面这个vbs代码文件。哪些地方,需要注意加上特殊符号?

比如echo i = i + ^1>>c:\diskpart.vbs
  1. Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
  2. Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
  3. For Each objDisk in colDisks
  4.     i = i + 1
  5.     s = s & "硬盘" & i & ":" & vbTab
  6.     s = s & "大小:" & GetSize(objDisk.Size) & vbTab
  7.     s = s & "型号:" & objDisk.Caption & vbCrLf
  8.     strDiskID = Replace(objDisk.DeviceID, "\", "\\")
  9.     Set colPartitions = objWMI.ExecQuery _
  10.     ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDiskID & """}" _
  11.     & " where AssocClass=Win32_DiskDriveToDiskPartition")
  12.     For Each objPartition in colPartitions
  13.         strPartId = objPartition.DeviceID
  14.         Set colLogicalDisks = objWMI.ExecQuery _
  15.         ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & strPartId & """}  where AssocClass=Win32_LogicalDiskToPartition")
  16.         For Each objLogicalDisk in colLogicalDisks
  17.             size = objLogicalDisk.Size
  18.             free = objLogicalDisk.Freespace
  19.             used = FormatNumber((size - free) / size * 100, 2, true) & "%"
  20.             s = s & "分区" & objLogicalDisk.DeviceID & vbTab
  21.             s = s & "大小:" & GetSize(size) & vbTab
  22.             s = s & "剩余:" & GetSize(free) & vbTab
  23.             s = s & "使用率:" & used & vbCrLf
  24.         Next
  25.     Next
  26.     s = s & vbCrLf
  27. Next
  28. Set fso = CreateObject("Scripting.FileSystemObject")
  29. fso.OpenTextFile("c:\diskpart.txt", 2, true).Write s
  30. Function GetSize(intSize)
  31.     If intSize/1024/1024 > 1024 Then
  32.         GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
  33.     Else
  34.         Getize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
  35.     End If
  36. End Function
复制代码
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

解决了。。。
  1. @echo off
  2. ———————————————————————————————————————
  3. del /q c:\diskinfo.* >nul 2>nul
  4. ———————————————————————————————————————
  5. echo Set objWMI = GetObject("winmgmts:\\.\root\cimv2")>>c:\diskinfo.vbs
  6. echo Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%%'")>>c:\diskinfo.vbs
  7. echo For Each objDisk in colDisks>>c:\diskinfo.vbs
  8. echo     i = i + ^1>>c:\diskinfo.vbs
  9. echo     s = s ^& "硬盘" ^& i ^& ":" ^& vbTab>>c:\diskinfo.vbs
  10. echo     s = s ^& "大小:" ^& GetSize(objDisk.Size) ^& vbTab>>c:\diskinfo.vbs
  11. echo     s = s ^& "型号:" ^& objDisk.Caption ^& vbCrLf>>c:\diskinfo.vbs
  12. echo     strDiskID = Replace(objDisk.DeviceID, "\", "\\")>>c:\diskinfo.vbs
  13. echo     Set colPartitions = objWMI.ExecQuery _>>c:\diskinfo.vbs
  14. echo     ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" ^& strDiskID ^& """}" _>>c:\diskinfo.vbs
  15. echo     ^& " where AssocClass=Win32_DiskDriveToDiskPartition")>>c:\diskinfo.vbs
  16. echo     For Each objPartition in colPartitions>>c:\diskinfo.vbs
  17. echo         strPartId = objPartition.DeviceID>>c:\diskinfo.vbs
  18. echo         Set colLogicalDisks = objWMI.ExecQuery _>>c:\diskinfo.vbs
  19. echo         ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" ^& strPartId ^& """}  where AssocClass=Win32_LogicalDiskToPartition")>>c:\diskinfo.vbs
  20. echo         For Each objLogicalDisk in colLogicalDisks>>c:\diskinfo.vbs
  21. echo             size = objLogicalDisk.Size>>c:\diskinfo.vbs
  22. echo             free = objLogicalDisk.Freespace>>c:\diskinfo.vbs
  23. echo             used = FormatNumber((size - free) / size * 100, 2, true) ^& "%%">>c:\diskinfo.vbs
  24. echo             s = s ^& "分区" ^& objLogicalDisk.DeviceID ^& vbTab>>c:\diskinfo.vbs
  25. echo             s = s ^& "大小:" ^& GetSize(size) ^& vbTab>>c:\diskinfo.vbs
  26. echo             s = s ^& "剩余:" ^& GetSize(free) ^& vbTab>>c:\diskinfo.vbs
  27. echo             s = s ^& "使用率:" ^& used ^& vbCrLf>>c:\diskinfo.vbs
  28. echo         Next>>c:\diskinfo.vbs
  29. echo     Next>>c:\diskinfo.vbs
  30. echo     s = s ^& vbCrLf>>c:\diskinfo.vbs
  31. echo Next>>c:\diskinfo.vbs
  32. echo Set fso = CreateObject("Scripting.FileSystemObject")>>c:\diskinfo.vbs
  33. echo fso.OpenTextFile("c:\diskinfo.txt", 2, true).Write s>>c:\diskinfo.vbs
  34. echo Function GetSize(intSize)>>c:\diskinfo.vbs
  35. echo     If intSize/1024/1024 ^> 1024 Then>>c:\diskinfo.vbs
  36. echo         GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) ^& "GB">>c:\diskinfo.vbs
  37. echo     Else>>c:\diskinfo.vbs
  38. echo         Getize = FormatNumber(intSize/1024/1024, 2, true) ^& "MB">>c:\diskinfo.vbs
  39. echo     End If>>c:\diskinfo.vbs
  40. echo End Function>>c:\diskinfo.vbs
  41. ping 127.0.0.1 -n 2 >nul 2>nul
  42. ———————————————————————————————————————
  43. start c:\diskinfo.vbs
  44. ping 127.0.0.1 -n 2 >nul 2>nul
  45. cls
  46. type c:\diskinfo.txt
  47. ping 127.0.0.1 -n 15 >nul 2>nul & del /q c:\diskinfo.* & exit/b
复制代码

TOP

我来提供一个不需要转义的方案
  1. @echo off
  2. for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (
  3.     more +%%i "%~f0" > new.vbs
  4. )
  5. goto :eof
  6. :DynamicScript
  7. Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
  8. Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
  9. For Each objDisk in colDisks
  10.     i = i + 1
  11.     s = s & "硬盘" & i & ":" & vbTab
  12.     s = s & "大小:" & GetSize(objDisk.Size) & vbTab
  13.     s = s & "型号:" & objDisk.Caption & vbCrLf
  14.     strDiskID = Replace(objDisk.DeviceID, "\", "\\")
  15.     Set colPartitions = objWMI.ExecQuery _
  16.     ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDiskID & """}" _
  17.     & " where AssocClass=Win32_DiskDriveToDiskPartition")
  18.     For Each objPartition in colPartitions
  19.         strPartId = objPartition.DeviceID
  20.         Set colLogicalDisks = objWMI.ExecQuery _
  21.         ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & strPartId & """}  where AssocClass=Win32_LogicalDiskToPartition")
  22.         For Each objLogicalDisk in colLogicalDisks
  23.             size = objLogicalDisk.Size
  24.             free = objLogicalDisk.Freespace
  25.             used = FormatNumber((size - free) / size * 100, 2, true) & "%"
  26.             s = s & "分区" & objLogicalDisk.DeviceID & vbTab
  27.             s = s & "大小:" & GetSize(size) & vbTab
  28.             s = s & "剩余:" & GetSize(free) & vbTab
  29.             s = s & "使用率:" & used & vbCrLf
  30.         Next
  31.     Next
  32.     s = s & vbCrLf
  33. Next
  34. Set fso = CreateObject("Scripting.FileSystemObject")
  35. fso.OpenTextFile("c:\diskpart.txt", 2, true).Write s
  36. Function GetSize(intSize)
  37.     If intSize/1024/1024 > 1024 Then
  38.         GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
  39.     Else
  40.         Getize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
  41.     End If
  42. End Function
复制代码
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表