批处理生成几乎任何字符,包括Tab

所属分类: 脚本专栏 / DOS/BAT 阅读数: 1899
收藏 0 赞 0 分享

One of my early posts on DosTips was a :chr routine that could convert a number into the corresponding character. The final code that appears near the end of the thread works really well, and is fast, but nearly all the characters must be embedded in the script. The TAB character in particular can be problematic, especially when trying to post the code on a forum site.

There is the undocumented %=ExitCodeAscii% dynamic variable that converts the most recent error code (from EXIT, not EXIT /B), but it is limited to codes between 32 and 126.

I had never seen a native method to generate a TAB character that works on all Windows versions - until now

I recently ran across this FORFILES documentation gem at SS64.COM:

复制代码 代码如下:

To include special characters in the command line, use the hex code for the character in 0xHH format (ex. 0x09 is theTAB character, 0x22 is the double quote " character.)

Eureeka :idea: - The following simple printHex routine can generate any byte code value except 0x00 (nul), 0x0A (newline), and 0x0D (carriage return). 8)

@echo off
setlocal

::Define a Linefeed variable
set LF=^


::above 2 blank lines are critical - do not remove.

::Create a TAB variable
call :hexprint "0x09" TAB

::Print a string with encoded TABs
call :hexprint "A0x09B0x09C"

::Create a string variable with encoded TABs
call :hexprint "A0x09B0x09C" var
set var

exit /b

:hexPrint string [rtnVar]
 for /f eol^=^%LF%%LF%^ delims^= %%A in (
  'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(%~1"'
 ) do if "%~2" neq "" (set %~2=%%A) else echo(%%A
exit /b

I'll be playing Native American style flutes at a retreat in the Blue Ridge Mountains of Virginia for the next 4 days :D , without computer access :|
I wanted to post this before I left.

Enjoy the code, while I enjoy the crisp fall air. :)

Dave Benham

更多精彩内容其他人还在看

for 语句从入门到精通

在批处理中,for是最为强大的命令语句,它的出现,使得解析文本内容、遍历文件路径、数值递增/递减等操作成为可能
收藏 0 赞 0 分享

dos 目录跳转 cd

当我们需要处理不同路径下的文件的时候,很可能需要切换目录,这个时候,可以考虑使用目录跳转命令cd。
收藏 0 赞 0 分享

tree 以树形格式罗列文件

tree ,在英语中的基本含义是“树”,在cmd中,tree命令的功能是以树形格式罗列文件。
收藏 0 赞 0 分享

dos 内容重定向

当我们在cmd窗口中查询某条命令的帮助信息的时候,帮助信息是显示在命令行窗口中的,命令行窗口关闭后,这些帮助信息就看不到了,如果下次还想看,又得在命令行窗口中输入查询命令,比较繁琐。
收藏 0 赞 0 分享

执行批处理bat程序中的条件处理

一直用bat实现部分功能,对于我们仍需要控制是否满意我们的要求,下面是if帮助文档,方便查询
收藏 0 赞 0 分享

不错的批处理脚本 第一部分

非常不错的批处理脚本代码,功能比较多,用到了,很多的批处理机器
收藏 0 赞 0 分享

不错的批处理脚本实例代码 第二部分

不错的批处脚本实例代码,用到了批处理中的很多技巧与知识点,不懂得可以逐一查找相关资料
收藏 0 赞 0 分享

开机更新桌面主题的批处理代码

更新桌面主题的批处理代码
收藏 0 赞 0 分享

非常好的for 教程, 当时我就是看这个学习for 的第1/2页

批处理for命令详解 FOR这条命令基本上都被用来处理文本,但还有其他一些好用的功能! 看看他的基本格式(这里我引用的是批处理中的格式,直接在命令行只需要一个%号)
收藏 0 赞 0 分享

批处理的"循环"效果脚本

曾经在回答一个问题时 无意中想到的方法 今天又看到类似的问题 个人认为是非常实用的 于是 想把这种方法推荐给大家
收藏 0 赞 0 分享
查看更多