Board logo

标题: [系统相关] [已解决]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
  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
复制代码

作者: ygqiang    时间: 2014-12-17 00:25

解决了。。。
  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
复制代码

作者: Batcher    时间: 2014-12-17 09:26

我来提供一个不需要转义的方案
  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
复制代码





欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2