中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --
联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
游客 | 登录 | 注册 | 会员 | 搜索 | 中国DOS联盟
中国DOS联盟论坛
现在时间是 2026-08-07 10:16
48,040 主题排行 / 350,125 发帖 / 今日 0 篇 / 48,252 会员排行
DOS批处理 & 脚本技术(批处理室) » 如何在文件头插入字符串?
可打印版本  3,083 / 8
第1楼 sercomm 发表于 2004-11-30 00:00
初级用户 发帖 3 积分 109
如何在文件头插入字符串?
请问如何在一个文件的头部插入一个字符串?要求插入的字符串不含回车换行.

我用echo发现它始终会加回车换行.比如echo "hello">file,file的大小为7个byte,在最后多了回车换行,请问如何解决这个问题?

第2楼 Climbing 发表于 2004-12-01 00:00
铂金会员 发帖 2,753 积分 6,962 来自 河北保定
用Horst的QECHO第三方工具。------------------------------------------------------------------------
QECHO Quote echo Ver 1.3 (c) 2000 Horst Schaeffer
------------------------------------------------------------------------QECHO supports output that is not possible with the normal ECHO command: + Strings in 'single' or "double" quotes marks
+ ASCII byte values (decimal)
+ Control codes with caret, like ^L (=ASCII 12)
+ Symbols: CR (13), LF (10), FF (12), ESC (27)
+ @nn - write position (extra s.below)No terminating CR/LF is output unless specified (!) thus: QECHO "something" CR LF
is same as: ECHO somethingStrings may include the other type of quotes, like: 'file "%1" done'
or "file '%1' done". The same type of quotes must be doubled if used
within a string, for example: "file ""%1"" done".Pipes and redirections as usual. Separators (blank, comma, semicolon)
allowed. If any invalid data occur, the rest of the line will be ignored
(errorlevel > 0).Some examples:++ Write to a file without CR+LF QECHO "SET X="> some.bat++ Pipe multiple lines or CR's QECHO CR,"N",CR | format A: /q/u /v:"" (FORMAT, no prompts)
QECHO "D100,200",CR,"Q",CR | debug PROG.COM (DUMP thru DEBUG)++ Send control codes to printer (without CR/LF) QECHO FF > LPT1 (form feed)
QECHO ESC "@", ESC "l" 10 > LPT1 (reset, margin /EPSON)
Extra
-----
With the @-sign you can set the write position (1...) for the following
data. This allows column aligned output, even if the length of the first
part is variable.Example: Qecho "%varname%" @20 "more text", CR,LF >>file.extAnother feature: A string can be input to QECHO by redirection, an then
be completed by writing (or overwriting) something at the specified
position. Please test to find out if you can use it...----
QECHO is freeware by Horst Schaeffer - no warranties of any kind
eMail: horst.schaeffer@gmx.net= 04 AUG 2002
用Google搜索下载地址。请注意写好帖子标题。
第3楼 sercomm 发表于 2004-12-01 00:00
初级用户 发帖 3 积分 109
多谢解答!请问可不可以只用dos原有的基本命令完成这个功能呢?
第4楼 willsort 发表于 2004-12-04 00:00
元老会员 发帖 1,512 积分 4,432
Re sercomm:
不用第三方工具的方法也有很多,比如下面的debug方法:
@echo off
:makeasd 生成debug脚本
for %%f in (~insert.*) do del %%f
for %%s in (e100"insert text"1a n~insert.txt rcx 10 w q) do echo %%s>>~insert.asd
:runscr 运行debug脚本生成 prefix.txt
debug < ~insert.asd >nul
:fixed
copy ~insert.txt+%0 %0.txt > nul
for %%f in (~insert.*) do del %%f
:end
第5楼 Climbing 发表于 2004-12-04 00:00
铂金会员 发帖 2,753 积分 6,962 来自 河北保定
Re willsort:对dubug我一直不太熟悉,请问上面程序中“e100"insert text"1a”中的1a表示什么意思?难道表示文件结尾?如果字符串很长(超过了16个字符),是不是要相应修改rcx后面的数字来确定写入文件的字符串的长度?
第6楼 willsort 发表于 2004-12-06 00:00
元老会员 发帖 1,512 积分 4,432
Re Climbing:  debug是一个很好的工具,因为在微软所有的操作系统中都包含它,而且可以用它访问硬件端口、内存系统区,甚至可以动态汇编一个小程序。数学运算和字符串操作也都可以用的上它。 1a在这里是用作文本文件结束符,主要供copy /a识别,也可以供type识别。这里之所以如此做,是为预防前缀字符串的长度不固定或无法确定。如果字符串很长当然需修改相应的数字,一般采用100作为限制长度,超过这样长的字符串,已经不适合用debug的e命令写入了。
第7楼 Climbing 发表于 2004-12-06 00:00
铂金会员 发帖 2,753 积分 6,962 来自 河北保定
1a是不是就是在DOS命令行下用Ctrl+z输入的字符?谢谢,我现在也发觉debug真的很好用,看来有必要重新复习一下汇编语言的知识。
第8楼 willsort 发表于 2004-12-06 00:00
元老会员 发帖 1,512 积分 4,432
Re Climbing: 是的,不仅仅是1a,ascii码表01~1a的所有特殊字符都可以用Ctrl+A~Ctrl+Z输入。在type(type con)和copy(copy con file)的文本环境中是直接输入的,在edit和其他大部分dos编辑文本环境中,还需要先按一下Ctrl+P。
第9楼 kcdsw 发表于 2006-06-14 12:37
中级用户 发帖 179 积分 404
先提取第一行的内容,然后设置成变量
然后用echo 写入前缀%变量%
然后用for /f "tokens=* skip=1" %%a in (旧文件) do echo %%a >>新文件

cmd @ xp
[ 联系联盟系统管理团队 - 中国DOS联盟 - 标准版 ]
Sponsored by ifanr Inc | © 2001–2023