Board logo

标题: 如何在文件头插入字符串? [打印本页]

作者: sercomm     时间: 2004-11-30 00:00    标题: 如何在文件头插入字符串?

请问如何在一个文件的头部插入一个字符串?要求插入的字符串不含回车换行.

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


作者: Climbing     时间: 2004-12-1 00:00
用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搜索下载地址。请注意写好帖子标题。
作者: sercomm     时间: 2004-12-1 00:00
多谢解答!请问可不可以只用dos原有的基本命令完成这个功能呢?
作者: willsort     时间: 2004-12-4 00:00
Re sercomm:
    不用第三方工具的方法也有很多,比如下面的debug方法:

  Quote:
@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


作者: Climbing     时间: 2004-12-4 00:00
Re willsort:对dubug我一直不太熟悉,请问上面程序中“e100"insert text"1a”中的1a表示什么意思?难道表示文件结尾?如果字符串很长(超过了16个字符),是不是要相应修改rcx后面的数字来确定写入文件的字符串的长度?
作者: willsort     时间: 2004-12-6 00:00
Re Climbing:  debug是一个很好的工具,因为在微软所有的操作系统中都包含它,而且可以用它访问硬件端口、内存系统区,甚至可以动态汇编一个小程序。数学运算和字符串操作也都可以用的上它。    1a在这里是用作文本文件结束符,主要供copy /a识别,也可以供type识别。这里之所以如此做,是为预防前缀字符串的长度不固定或无法确定。如果字符串很长当然需修改相应的数字,一般采用100作为限制长度,超过这样长的字符串,已经不适合用debug的e命令写入了。
作者: Climbing     时间: 2004-12-6 00:00
1a是不是就是在DOS命令行下用Ctrl+z输入的字符?谢谢,我现在也发觉debug真的很好用,看来有必要重新复习一下汇编语言的知识。
作者: willsort     时间: 2004-12-6 00:00
Re Climbing:    是的,不仅仅是1a,ascii码表01~1a的所有特殊字符都可以用Ctrl+A~Ctrl+Z输入。在type(type con)和copy(copy con file)的文本环境中是直接输入的,在edit和其他大部分dos编辑文本环境中,还需要先按一下Ctrl+P。
作者: kcdsw     时间: 2006-6-14 12:37
先提取第一行的内容,然后设置成变量
然后用echo 写入前缀%变量%
然后用for /f   "tokens=* skip=1"  %%a in (旧文件) do echo %%a >>新文件

cmd @ xp