China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-03 22:45
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » BAT to VBS Problem View 2,957 Replies 18
Original Poster Posted 2007-03-28 07:13 ·  中国 浙江 丽水 电信
初级用户
Credits 28
Posts 10
Joined 2006-11-22 10:02
19-year member
UID 71367
Gender Male
Status Offline
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
Floor 2 Posted 2007-03-28 07:15 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Floor 3 Posted 2007-03-28 07:23 ·  中国 浙江 丽水 电信
初级用户
Credits 28
Posts 10
Joined 2006-11-22 10:02
19-year member
UID 71367
Gender Male
Status Offline
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?
Floor 4 Posted 2007-03-28 13:32 ·  中国 黑龙江 哈尔滨 联通
社区乞丐
Credits -174
Posts 8
Joined 2007-03-01 14:28
19-year member
UID 80485
Gender Male
Status Offline
I also want to learn this kind of knowledge. Can you teach me?
Floor 5 Posted 2007-03-28 13:36 ·  中国 黑龙江 哈尔滨 联通
社区乞丐
Credits -174
Posts 8
Joined 2007-03-01 14:28
19-year member
UID 80485
Gender Male
Status Offline
I just forgot to mention it earlier, don't make it too esoteric! Otherwise I won't understand (I'm an alien!@!#$%@#$#)
Floor 6 Posted 2007-03-29 03:29 ·  中国 四川 成都 教育网
中级用户
★★
Credits 346
Posts 103
Joined 2004-04-06 00:00
22-year member
UID 21852
Gender Male
Status Offline
Physical labor is one of the essential introductory courses
Floor 7 Posted 2007-03-29 05:22 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
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
Floor 8 Posted 2007-03-29 05:32 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
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.
Floor 9 Posted 2007-03-29 05:35 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
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.
Floor 10 Posted 2007-03-29 06:40 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
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
Floor 11 Posted 2007-03-29 08:01 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
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++
Floor 12 Posted 2007-03-29 09:09 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Just use quotes by yourself
Floor 13 Posted 2007-03-29 22:37 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Finally solved your problem;

type 1.txt |shellesc >3.txt

shellesc is really great
Floor 14 Posted 2007-03-30 07:17 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
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 ]
Floor 15 Posted 2007-03-30 13:21 ·  中国 广东 广州 电信
中级用户
★★
脚本爱好者
Credits 238
Posts 93
Joined 2007-03-11 13:38
19-year member
UID 81417
Gender Male
From GZ
Status Offline
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 ]
Forum Jump: