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-06-24 13:15
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Original] Batch Processing Code Post Coloring Script Batch2ubb (CMD & GAWK) DigestI View 16,771 Replies 42
Original Poster Posted 2006-07-03 20:16 ·  中国 辽宁 葫芦岛 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Batch2ubb, a batch code posting colorizer script (CMD & GAWK), updated on 7.30
This is something I made back when I was learning AWK. There were always some unresolved problems, so I never posted it. Recently I haven’t been able to get online for a while, so I rewrote it and added a calling batch file that supports parameters. The multi-parameter calling framework is the great work of Willsort; I copied it directly. For details, see: A brief discussion of batch parameter issues http://www.cn-dos.net/forum/viewthread.php?tid=17785.
The main functionality is handled by the GAWK script. Clipboard operations depend on winclip.exe, and the syntax file was simplified from the batch syntax file of EmEditor that I use. download link for the required executable files:
winclip http://www.dmst.aueb.gr/dds/sw/outwit/outwit-bin-1.26.zip
gawk http://www.klabaster.com/progs/gawk32.zip

As for how to use this batch file, let me explain several calling methods:
1、Run it directly by double-clicking.
2、Call it from the command line; use Batch2ubb /h to view the specific parameter usage.
3、Create a shortcut in sendto and call it from there.
4、Create a shortcut and assign a hotkey to call it.

For method 1, it will convert the code in the clipboard into UBB code and write it back to the clipboard.
For method 3, the usage is: Start, Run, enter sendto, create a shortcut in the sendto directory pointing to Batch2ubb.cmd, then right-click the batch file you want to convert and use Send To on the shortcut you created; the converted code will be stored in the clipboard. This method is suitable for converting existing batch files.
For method 4, the usage is: create a shortcut pointing to Batch2ubb.cmd on the desktop or in the Start menu and assign a hotkey to it (for the latter location, it takes effect after reboot, or on the desktop hold down F5 for ten seconds for it to take effect), or let software such as hoekey do it for you. Then copy the code you want to post, press the hotkey, and you can paste it into the posting window. And you can also choose whether to enable the /n parameter in the shortcut target.
Also, please modify the name in the timestamp statement in Batch2ubb.awk to your own name. Questions or suggestions are welcome in replies; I’ll do my best to improve it.

Batch2ubb.cmd


  1. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2. :: Batch2ubb.cmd -V0.30 -- batch to UBB code
  3. :: 无奈何@cn-dos.net - updated on: 2005-7-30 - CMD & GAWK
  4. :: Usage: Batch2ubb
  5. :: Supported files: - gawk.exe winclip.exe Batch2ubb.awk batch.esy
  6. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

  7. @echo off
  8. setlocal
  9. set path=%path%;%cd%;%~dp0
  10. set error=
  11. rem File integrity check.
  12. for %%i in (gawk.exe winclip.exe batch.esy batch2ubb.awk) do (
  13. @if "%%~$PATH:i" == "" (
  14. echo Error: required file "%%i" is missing.
  15. set error=Error: program files are incomplete.
  16. ) else ( set %%~ni="%%~$PATH:i" )
  17. )
  18. if defined error goto error

  19. rem Test clipboard data type.
  20. winclip -i |findstr /r ".*_.*TEXT" >nul
  21. if ERRORLEVEL 1 set error=Error: the clipboard is empty or contains non-text data!

  22. rem With no parameters, get clipboard data and copy the converted result to the clipboard.
  23. if "%~1" == "" (
  24. if defined error goto error
  25. winclip -p |gawk -v line=1 -f %Batch2ubb% |winclip -c
  26. goto end
  27. )
  28. rem If there is only one parameter, try handling it as an input file; if it does not exist, handle it as a parameter.
  29. if "%~2" == "" (
  30. if exist "%~1" (
  31. gawk -v line=1 -f %Batch2ubb% "%~1" |winclip -c
  32. goto end
  33. )
  34. )
  35. set line=1
  36. set input=
  37. set out=

  38. :ParseLoop
  39. if "%~1" == "" goto Start
  40. if "%~1" == "/?" goto SwitchH
  41. rem Process parameters and jump to the corresponding label.
  42. for %%s in (i I n N o O h H) do if "%~1"=="/%%s" goto Switch%%s
  43. set error=Error: incorrect parameter format - "%1"!
  44. goto error

  45. :SwitchI
  46. set "input=%~2"
  47. if not exist "%input%" set error=Warning: file "%input%" does not exist. & goto error
  48. goto Next2Arg

  49. :SwitchO
  50. set "out=%~2"
  51. if not defined out set error=Warning: please specify an output file. & goto error
  52. goto Next2Arg

  53. :SwitchN
  54. set line=0
  55. goto NextArg

  56. :Next2Arg
  57. shift
  58. :NextArg
  59. shift
  60. goto ParseLoop

  61. rem Handle different cases according to different parameters.
  62. :Start
  63. if defined input (
  64. if defined out (
  65. gawk -v line=%line% -f %Batch2ubb% "%input%" >"%out%"
  66. ) else (
  67. gawk -v line=%line% -f %Batch2ubb% "%input%" |winclip -c
  68. )
  69. ) else (
  70. if defined out (
  71. if defined error goto error
  72. winclip -p |gawk -v line=%line% -f %Batch2ubb% >"%out%"
  73. ) else (
  74. if defined error goto error
  75. winclip -p |gawk -v line=0 -f %Batch2ubb% |winclip -c
  76. )
  77. )
  78. goto end

  79. :error
  80. echo.%error%
  81. echo.
  82. :SwitchH
  83. echo.Batch to UBB code.
  84. echo.Batch2ubb
  85. echo.
  86. echo. /i Specify the file to convert; may include a path; by default input is taken from the clipboard.
  87. echo. /o Specify the output file; may include a path; by default output goes to the clipboard.
  88. echo. /n Generate UBB code without line numbers; by default it includes line numbers.
  89. echo. /h Show this brief help, equivalent to /?.
  90. echo. This program depends on the following files: gawk.exe winclip.exe Batch2ubb.awk batch.esy.
  91. echo.
  92. :end
Posted by 无奈何 on: 2006-07-30 23:44




Batch2ubb.awk

###########################################################
# Batch2ubb.awk -V0.2 -- batch to UBB code script
# 无奈何 - 2005-7-2 -- GAWK script
###########################################################
#Read the configuration file and get keywords
BEGIN {
IGNORECASE=1
while ((getline<"batch.esy") >0){
if ($0!~/^*;/ && $0!~/^$/) {
if ($0~/^*#LINECOMMENT=/){
LineComment=substr($0,index($0,"=")+1)
} else if ($0~/^*#VARIABLE=/){
Variable=substr($0,index($0,"=")+1)
} else if ($0~/^*#PARAMETER=/){
Parameter=substr($0,index($0,"=")+1)
} else if ($0~/^*#LABEL=/){
Label=substr($0,index($0,"=")+1)
} else if ($0~/^*#KEYWORD/){
color=substr($0,index($0,"=")+1)
} else {
keyword=color
}
}
}
print "
"
if (line) print "
    "
    }
    #Main program starts
    {
    before=""
    after=""
    #Handle comment lines and label lines
    if (($0~/^*::/) || ($0~/^*REM\>/)){
    gsub(/=.*]/,"=" LineComment "]",before)
    $0=before $0 after
    }else if ($0~/^*:+/){
    gsub(/=.*]/,"=" Label "]",before)
    $0=before $0 after
    } else {
    temp=$0
    #Split out keywords
    gsub(/+/,"□",temp)
    gsub(/(^□|□$)/,"",temp)
    len=split(temp,word,"□+")
    for (i=1;i<=len;i++){
    # The COLOR keyword overlaps with UBB color characters, handle it separately
    if (word~/COLOR/){
    if(!fcolor){
    gsub(/=.*]/,"=" keyword "]",before)
    gsub("\\<" word "\\>",before "&" after,$0)
    delete word
    fcolor=1
    } else
    delete word
    }
    #Delete duplicate keywords
    for (j=i+1;j<=len;j++){
    if (word==word)
    delete word
    }
    }

    for (i=1;i<=len;i++){
    for (aa in keyword){
    if (aa==toupper(word)){
    gsub(/=.*]/,"=" keyword "]",before)
    gsub("\\<" word "\\>",before "&" after,$0)
    }
    }
    }
    #Handle details such as variables and command parameters
    gsub(/=.*]/,"=" Variable "]",before)
    gsub (/%+*%?/,before "&" after,$0)
    gsub (/!+*!*/,before "&" after,$0)
    gsub(/=.*]/,"=" Parameter "]",before)
    gsub (/ \/+/,before "&" after,$0)
    gsub (/(\(|\))/,"&",$0)
    gsub (/(\")/,"&",$0)
    }
    if (line) print "
  1. " $0
    else print $0
    }
    END {
    if (line) printf "
"
#Add timestamp, modify the author name yourself
print "" strftime("\t无奈何发表于: %Y-%m-%d %H:%M") ""
print "
"
}


All files packaged for download are in the attachment:

[ Last edited by 无奈何 on 2006-7-30 at 23:53 ]
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
redtek +2 2006-11-03 00:30
hxuan999 +2 2006-11-28 06:45
ccwan +15 2007-02-09 03:30
Attachments
Batch2ubb.zip (145.3 KiB, Credits to download 1 pts, Downloads: 385)
Batch2ubb_V0.3.zip (145.46 KiB, Credits to download 1 pts, Downloads: 490)
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 2 Posted 2006-07-04 09:47 ·  中国 广东 广州 天河区 电信
荣誉版主
★★★
Credits 718
Posts 313
Joined 2005-09-26 00:00
20-year member
UID 42844
Gender Male
Status Offline
re 无奈何

Did Batch2ubb.cmd leave out endlocal?
No wonder your posts have been full of color these past two days. I was thinking, 无奈何, you wouldn't go so far as to play around like this out of helplessness, haha. So this was your killer move after all. Very creative! Bump..

[ Last edited by 220110 on 2006-7-4 at 09:49 ]
Floor 3 Posted 2006-07-04 21:22 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Re 220110
Haha, a little more color amid the helplessness. ^_^
endlocal wasn't omitted, because there is an implicit endlocal command at the end of a batch file, so endlocal can be left out. If there are multiple setlocal sections, then adding an end to the environment changes is necessary.
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 4 Posted 2006-07-06 13:59 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline


  1. @echo off
  2. set Version=VIRDEFVER
  3. find /i virusdb.cfg "%Version%"
  4. if not errorlevel 1 set Version=VersionNo
  5. for /F "tokens=1 delims=VIRDEFVER " %%A in ('find /i .\Rav\virusdb.cfg "VIRDEFVER"') do set No=%%A
  6. for /F "tokens=1 delims=VIRDEFUPDATEDATE " %%B in ('find /i .\Rav\virusdb.cfg "VIRDEFUPDATEDATE"') do set DATENO=%%B
  7. echo >.\001\VERSION.INF
  8. echo %Version%%NO%>>.\001\VERSION.INF
  9. echo UpdateDate%DATENO%>>.\001\VERSION.INF
DOS一跟葱 2006-07-06 13:46



Boss, why is the signature font white?
Floor 5 Posted 2006-07-06 19:10 ·  中国 广东 珠海 电信
初级用户
Credits 112
Posts 40
Joined 2006-06-23 19:52
20-year member
UID 57473
Gender Male
Status Offline
I don't understand how to use it.
Floor 6 Posted 2006-07-06 21:10 ·  中国 广东 广州 中移铁通
中级用户
★★
Credits 256
Posts 93
Joined 2006-03-26 22:12
20-year member
UID 52853
Gender Male
From 广东
Status Offline
Quite an expert. Time to study this carefully.
Floor 7 Posted 2006-07-08 16:48 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Re fastslz
I had two purposes for adding the timestamp:
1、to use some of the words in the mark as keywords for full-text search, so I could find my own posts containing code.
2、to simply add a time marker.

As for 1, using only a username as a marker like in post #1 is not a very wise approach, because it will turn up a lot of other posts too, so a more distinctive phrase works better. As for why I chose white, it was just because I wanted it to be a bit more subtle. Of course, you can change it to a color you like better.

Re zhaxi
If setting up a shortcut feels a bit complicated, you can call it directly from the command line.
An even simpler method is to copy the batch code you want to post to the clipboard, double-click Batch2ubb.cmd , and then you can paste the converted code.
I updated it a bit and also added some more explanations.

[ Last edited by 无奈何 on 2006-7-8 at 23:33 ]
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 8 Posted 2006-07-14 08:21 ·  中国 辽宁 大连 教育网
中级用户
★★
DOS之友
Credits 332
Posts 168
Joined 2005-10-06 00:00
20-year member
UID 43171
Gender Male
From 天涯
Status Offline


  1. @echo off
  2. :start

  3. setlocal ENABLEDELAYEDEXPANSION
  4. :loop_a
  5. del tmp*.txt 2>nul
  6. for /l %%i in (1,1,6) do (
  7. echo %random% >nul
  8. set /a s%%i=!random:~-1!
  9. echo %random% >nul
  10. set /a g%%i=!random:~-1!
  11. if "!s%%i!"=="0" (set /a _VAR_=!g%%i! / 3) else (set /a _VAR_=!s%%i!!g%%i! / 3)
  12. if "!_VAR_:~1,1!"=="" set _VAR_=0!_VAR_!

  13. findstr /m "!_VAR_!" tmp.txt 2>nul >nul && goto :loop_a
  14. if "!_VAR_!"=="00" (echo 33>>tmp.txt) else (echo !_VAR_!>>tmp.txt)
  15. )
  16. sort tmp.txt>tmp1.txt
  17. endlocal


  18. :loop_b

  19. setlocal ENABLEDELAYEDEXPANSION

  20. echo %random% >nul
  21. set /a s=!random:~-1!

  22. echo %random% >nul
  23. set /a g=!random:~-1!

  24. if "!s!"=="0" (set /a _VAR_=!g! / 6) else (set /a _VAR_=!s!!g! / 6)
  25. if "!_VAR_:~1,1!"=="" set _VAR_=0!_VAR_!

  26. echo ^|>>tmp1.txt
  27. if "!_VAR_!"=="00" (echo 16>>tmp1.txt) else (echo !_VAR_!>>tmp1.txt)

  28. endlocal



  29. :echo
  30. setlocal ENABLEDELAYEDEXPANSION
  31. for /f %%i in (tmp1.txt) do (
  32. set n=%%i
  33. set m=!m! !n!
  34. )
  35. del tmp*.txt
  36. cls
  37. echo.
  38. echo.¥1000000000000000000000000000000000000000000.00
  39. echo.
  40. echo. Hitme, you're guaranteed to win in Double Color Ball, the numbers are !m!
  41. echo.
  42. echo.¥1000000000000000000000000000000000000000000.00
  43. echo.



  44. :save
  45. set /p x=Save the numbers to num.txt
  46. if /i [%x%]== (echo !m!>>num.txt && endlocal && goto :start) else (endlocal && goto :start)
IceCrack发表于: 2006-07-14 08:13

Hehe, the effect is pretty good. After playing with it more carefully, I think it should work like this: when there is no data in the clipboard and there are no parameters, it should jump to the help file. And under the DOS window it should support /h, /?, and also jump to the help file when there are no parameters. That's what I think.

[ Last edited by IceCrack on 2006-7-14 at 08:32 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +5 2007-02-06 22:44
Floor 9 Posted 2006-07-30 23:50 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Thanks for the suggestion, brother IceCrack. I updated Batch2ubb.cmd to version V0.3. Anyone interested can compare it with the old version.
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 10 Posted 2006-11-02 02:50 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
This program is excellent, really practical. Not only does it look good, it also makes the structure much clearer when trying to understand code written by others.
Floor 11 Posted 2006-11-02 06:47 ·  中国 广东 茂名 电信
中级用户
★★
Credits 261
Posts 123
Joined 2006-06-06 19:23
20-year member
UID 56648
Status Offline
Awesome, I'll use this whenever I post from now on
CODE: [Copy to clipboard] how is this done?
Floor 12 Posted 2006-11-03 00:34 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
After downloading it and using it once, I found it unbelievably convenient!!!
This automatic line-numbering function for batch files is really interesting. If you're discussing some statement on a certain line or in a certain section, you can just refer to the line number~ : )
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 13 Posted 2006-11-27 10:17 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Hehe. Impressive~~ Brother 无奈何 only makes good posts when he posts~ bump~
Floor 14 Posted 2006-12-05 03:17 ·  IANA 局域网IP(Private-Use)
初级用户
★★
Credits 136
Posts 59
Joined 2006-06-02 16:05
20-year member
UID 56438
Status Offline
I don't quite understand it
Floor 15 Posted 2006-12-24 02:52 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
This solved another one of my nagging troubles

Big praise
Forum Jump: