中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-03 23:46
48,038 topics / 350,123 posts / today 0 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » BAT to VBS Problem
Printable Version  2,961 / 18
Floor1 djhui Posted 2007-03-28 07:13
初级用户 Posts 10 Credits 28
echo strComputer = "." >> tmp.vbs
echo Set objWMIService = GetObject("winmgmts:\\" ^& strComputer ^"\root\cimv2") >> tmp.vbs
echo Set colItems = objWMIService.ExecQuery _ >> tmp.vbs
echo ("Select * From Win32_DisplayConfiguration") >> tmp.vbs
echo For Each objItem in colItems >> tmp.vbs
echo Wscript.Echo "显示芯片: " ^& objItem.DeviceName ^& vbcrlf ^& "色深: " ^& objItem.BitsPerPel ^& " 位" ^& vbcrlf ^& "分辨率: " ^& objItem.PelsWidth ^& "*" ^& objItem.PelsHeight >> tmp.vbs
echo >> tmp.vbs
echo Next >> tmp.vbs
echo >> tmp.vbs
start tmp.vbs
Just take this as an example, because some characters need to add a "^" in front of them to be written into VBS, but it's too troublesome to add this symbol manually. Can anyone help me make a batch script to add ^ in front of a DOS special character
Floor2 bjsh Posted 2007-03-28 07:15
银牌会员 Posts 621 Credits 2,000
Floor3 djhui Posted 2007-03-28 07:23
初级用户 Posts 10 Credits 28
Manual is too troublesome, and I don't know how to compile a P myself! So that's why I'm posting to ask others for help! Otherwise, why would I post?
Floor4 KANXM Posted 2007-03-28 13:32
社区乞丐 Posts 8 Credits -174
I also want to learn this kind of knowledge. Can you teach me?
Floor5 KANXM Posted 2007-03-28 13:36
社区乞丐 Posts 8 Credits -174
I just forgot to mention it earlier, don't make it too esoteric! Otherwise I won't understand (I'm an alien!@!#$%@#$#)
Floor6 estar Posted 2007-03-29 03:29
中级用户 Posts 103 Credits 346
Physical labor is one of the essential introductory courses
Floor7 bjsh Posted 2007-03-29 05:22
银牌会员 Posts 621 Credits 2,000
Your this question really stumped me;

Any problem is easy to solve;

Just a quotation mark; I've been struggling for 12 hours; this time is still continuing......

Change the title;...... Let everyone write about it
Floor8 flyinspace Posted 2007-03-29 05:32
银牌会员 Posts 517 Credits 1,206
Oh. Then you list all the special characters.. Write them in a notepad. Then judge one by one. If this file is very long. It is estimated that the waiting time can drive you crazy.
Floor9 flyinspace Posted 2007-03-29 05:35
银牌会员 Posts 517 Credits 1,206
So, it's better to write one with C++. These judgments won't take too long to write. And the execution will be much faster.
Floor10 bjsh Posted 2007-03-29 06:40
银牌会员 Posts 621 Credits 2,000
Originally posted by flyinspace at 2007-3-28 04:32 PM:
Oh. Then you list all the special characters. Write them in a notepad.
Then judge one by one.
If this file is very long.
I estimate that the waiting time can drive you crazy.


It's not such a simple problem;

Special symbols are not easy to take out, if you don't believe it, try it yourself
Floor11 flyinspace Posted 2007-03-29 08:01
银牌会员 Posts 517 Credits 1,206
It can be taken out..
Tried several special symbols and got them out..
As for others, no time to test..
Don't want to test either..It's better to write with C++
Floor12 bjsh Posted 2007-03-29 09:09
银牌会员 Posts 621 Credits 2,000
Just use quotes by yourself
Floor13 bjsh Posted 2007-03-29 22:37
银牌会员 Posts 621 Credits 2,000
Finally solved your problem;

type 1.txt |shellesc >3.txt

shellesc is really great
Floor14 bjsh Posted 2007-03-30 07:17
银牌会员 Posts 621 Credits 2,000
Code without third-party tools



  1. @echo off
  2. if exist tmp.txt del tmp.txt
  3. for /f "tokens=1 delims=" %%a in (test.txt) do set "var=%%a" & call :change
  4. goto exit
  5. :change
  6. set "var=%var:^=^^%"
  7. set "var=%var:>=^>%"
  8. set "var=%var:<=^<%"
  9. set "var=%var:&=^&%"
  10. set "var=%var:|=^|%"
  11. set /p "=%var%" <nul >>tmp.txt
  12. echo. >>tmp.txt
  13. :exit
BJSH posted on: 2007-03-29 18:08


Want to talk about lines eleven and twelve;
Because almost every line contains special symbols
Although each line has added a ^ symbol, but when writing to other text with echo, the ^ will be removed
So that the content of the resulting tmp.txt is exactly the same as the original text;
If you use ^^ for echo, it will be executed as if the ^ does not exist
For example, echo ^^& and echo & are the same and will both report an error
Thought about using set var >>tmp.txt, but each line will have var=... added
Also thought about using for "tokens=2 delims=var=" but how to output the following; use echo? Still the previous problem.

Finally found that set var"= " <nu >>tmp.txtl has a good feature;
Generally, I use its purpose to not wrap lines; this time using it to write to text is an innovation
Then add echo.
Finally solved the problem....

Summarize my gains
echo for the problem of writing special characters to text;
Use set /p ''= " <nul >> & echo.
Solved

[ Last edited by bjsh on 2007-3-29 at 06:30 PM ]
Floor15 axi Posted 2007-03-30 13:21
中级用户 Posts 93 Credits 238 From GZ
If you want to create multiple VBS in a batch without considering special character issues, you can add markers after each line of VBS code, and then use the find method to extract the content with the markers to create VBS, as follows:

@echo off
findstr "'%skip%VBS1" "%~f0" >killvbs.vbs
findstr "'%skip%VBS2" "%~f0" >tmp.vbs
start tmp.vbs
exit

strComputer = "." 'VBS1
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 'VBS1
Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'wscript.exe'") 'VBS1
For Each objProcess in colProcessList 'VBS1
objProcess.Terminate() 'VBS1
Next 'VBS1


strComputer = "." 'VBS2
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 'VBS2
Set colItems = objWMIService.ExecQuery ("Select * From Win32_DisplayConfiguration") 'VBS2
For Each objItem in colItems 'VBS2
Wscript.Echo "Display chip: " & objItem.DeviceName&+vbcrlf&"Color depth: " & objItem.BitsPerPel & " bits"&+vbcrlf&"Resolution: " & objItem.PelsWidth & "*" & objItem.PelsHeight 'VBS2
Next 'VBS2

The above content refers to the "find method" in "Mixed Programming of Batch and Other Languages".

The method of automatically adding markers after each line of text:
@echo off
for /f "tokens=*" %%a in (tmp.txt) do echo %%a 'VBS2>>t.txt

You can copy the content in t.txt to the batch that creates VBS.

You can also use the more method to complete the building V scheme of the building owner:

@echo off
more +2 %0>>tmp.vbs&&start tmp.vbs&exit
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_DisplayConfiguration")
For Each objItem in colItems
Wscript.Echo "Display chip: " & objItem.DeviceName&+vbcrlf&"Color depth: " & objItem.BitsPerPel & " bits"&+vbcrlf&"Resolution: " & objItem.PelsWidth & "*" & objItem.PelsHeight

Next

[ Last edited by axi on 2007-3-30 at 11:16 AM ]
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023