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-22 12:02
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to insert a string at the file header? View 2,995 Replies 8
Original Poster Posted 2004-11-30 00:00 ·  中国 江苏 苏州 电信
初级用户
Credits 109
Posts 3
Joined 2004-11-23 00:00
21-year member
UID 33939
Gender Male
Status Offline
How to insert a string at the beginning of a file? The requirement is that the inserted string does not contain carriage return and line feed.

I found that using echo always adds carriage return and line feed. For example, echo "hello" > file, the size of file is 7 bytes, and there is an extra carriage return and line feed at the end. How to solve this problem?
Floor 2 Posted 2004-12-01 00:00 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Use Horst's QECHO third-party tool.
------------------------------------------------------------------------
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
Use Google to search for the download address. Please write a good post title.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 3 Posted 2004-12-01 00:00 ·  中国 江苏 苏州 电信
初级用户
Credits 109
Posts 3
Joined 2004-11-23 00:00
21-year member
UID 33939
Gender Male
Status Offline
Thanks for the answer! May I ask if this function can be completed only using the original basic commands of DOS?
Floor 4 Posted 2004-12-04 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
关于sercomm:
不用第三方工具的方法也有很多,比如下面的debug方法:
@echo off
:makeasd Generate debug script
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 Run debug script to generate prefix.txt
debug < ~insert.asd >nul
:fixed
copy ~insert.txt+%0 %0.txt > nul
for %%f in (~insert.*) do del %%f
:end
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 5 Posted 2004-12-04 00:00 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Re regarding sort: I've always been not very familiar with dubug. What does "1a" in "e100"insert text"1a" in the above program mean? Does it mean the end of the file? If the string is very long (exceeding 16 characters), do I need to modify the number after rcx accordingly to determine the length of the string written to the file?
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 6 Posted 2004-12-06 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re Climbing:  debug is a very good tool because it is included in all Microsoft operating systems, and it can be used to access hardware ports, memory system areas, and even dynamically assemble a small program. Mathematical operations and string operations can also be used with it. The 1a here is used as the end-of-text file character, mainly for copy /a to recognize, and can also be recognized by type. The reason here is to prevent the prefix string from being of an indefinite or undeterminable length. If the string is very long, the corresponding number needs to be modified. Generally, 100 is used as the length limit. Strings longer than this are no longer suitable for writing with the debug e command.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 7 Posted 2004-12-06 00:00 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Is 1a just the character entered with Ctrl+z under the DOS command line? Thanks, I now also find that debug is really useful. It seems necessary to review the knowledge of assembly language again.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 8 Posted 2004-12-06 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re Climbing: Yes, not just 1a, all special characters from ASCII code 01 to 1a can be entered using Ctrl+A to Ctrl+Z. In the text environments of type (type con) and copy (copy con file), they are entered directly. In edit and most other DOS text editing environments, you also need to press Ctrl+P first.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 9 Posted 2006-06-14 12:37 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
First, extract the content of the first line and set it as a variable. Then use echo to write the prefix %variable%. Then use for /f "tokens=* skip=1" %%a in (old file) do echo %%a >> new file.

cmd @ xp
Forum Jump: