中国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-09 06:43
47,811 topics / 349,895 posts / today 0 new / 48,253 members
DOS批处理 & 脚本技术(批处理室) » [Original] Batch Processing Code Post Coloring Script Batch2ubb (CMD & GAWK)
Printable Version  17,298 / 42
Floor1 无奈何 Posted 2006-07-03 20:16
荣誉版主 Posts 356 Credits 1,338
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


All files packaged for download are in the attachment:

[ Last edited by 无奈何 on 2006-7-30 at 23:53 ]

Attachments
Batch2ubb.zip (145.3 KiB)
Batch2ubb_V0.3.zip (145.46 KiB)
Floor2 220110 Posted 2006-07-04 09:47
荣誉版主 Posts 313 Credits 718
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 ]
Floor3 无奈何 Posted 2006-07-04 21:22
荣誉版主 Posts 356 Credits 1,338
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.
Floor4 fastslz Posted 2006-07-06 13:59
铂金会员 Posts 2,315 Credits 5,493 From 上海


  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?
Floor5 zhaxi Posted 2006-07-06 19:10
初级用户 Posts 40 Credits 112
I don't understand how to use it.
Floor6 doscc Posted 2006-07-06 21:10
中级用户 Posts 93 Credits 256 From 广东
Quite an expert. Time to study this carefully.
Floor7 无奈何 Posted 2006-07-08 16:48
荣誉版主 Posts 356 Credits 1,338
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 ]
Floor8 IceCrack Posted 2006-07-14 08:21
中级用户 Posts 168 Credits 332 From 天涯


  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 ]
Floor9 无奈何 Posted 2006-07-30 23:50
荣誉版主 Posts 356 Credits 1,338
Thanks for the suggestion, brother IceCrack. I updated Batch2ubb.cmd to version V0.3. Anyone interested can compare it with the old version.
Floor10 NaturalJ0 Posted 2006-11-02 02:50
银牌会员 Posts 533 Credits 1,181
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.
Floor11 yangzhiyi Posted 2006-11-02 06:47
中级用户 Posts 123 Credits 261
Awesome, I'll use this whenever I post from now on
CODE: [Copy to clipboard] how is this done?
Floor12 redtek Posted 2006-11-03 00:34
金牌会员 Posts 1,147 Credits 2,902
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~ : )
Floor13 lxmxn Posted 2006-11-27 10:17
版主 Posts 4,938 Credits 11,386

  Hehe. Impressive~~ Brother 无奈何 only makes good posts when he posts~ bump~
Floor14 vlq5299 Posted 2006-12-05 03:17
初级用户 Posts 59 Credits 136
I don't quite understand it
Floor15 scriptor Posted 2006-12-24 02:52
银牌会员 Posts 555 Credits 1,187
This solved another one of my nagging troubles

Big praise
1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023