Board logo

标题: [已结]给txt文件中的每行前面加一个固定的字母 [打印本页]

作者: cnlssj     时间: 2008-11-28 16:57    标题: [已结]给txt文件中的每行前面加一个固定的字母

大家好,小弟有事相求!小弟的问题如下: 给txt文件中的每行前面加一个固定的字母, 比如一个txt文件中的内容是: 1.windowsXP 2.windows2000 3.windows98 4.unix 5.linux 6.... 7... (尽可能更多……) 然后给每行前面加一个固定的字母 : <a> 让txt文本文件中的内容变成: <a> 1.windowsXP <a>2.windows2000 <a>3.windows98 <a>4.unix <a> 5.linux <a> 6.... <a> 7... (尽可能更多……) 小弟先谢谢大家了! [ Last edited by HAT on 2008-12-2 at 13:55 ]

作者: moniuming     时间: 2008-11-28 17:13
@echo off
set "str=^<a^>"&set "file=2222.txt"
for /f "delims=" %%a in (%file%) do (
  if not defined dos (
    set dos=cndos
    >%file% echo %str%%%a
  ) else (
    >>%file% echo %str%%%a
  )
)
start "" %file%

作者: wangff     时间: 2008-11-28 17:51
好象会删掉空行

作者: wangff     时间: 2008-11-28 18:19
最后要加上set dos=否则第二次运行变追加

作者: aiqian2008     时间: 2008-11-28 19:10
试试这个,注意"!"会跑没掉.
@echo off&setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%i in ('findstr /n "." test.txt') do (
    set /a n+=1
    if %%i gtr !n! call :loop %%i
    echo.^<a^>%%j>>"test111.txt"
)
exit

:loop
echo.>>"test111.txt"
set /a n+=1
if %1 gtr %n% goto loop
goto :eof

作者: cnlssj     时间: 2008-12-2 13:41    标题: 谢谢大家@!

谢谢你们的帮忙

作者: wangff     时间: 2008-12-4 10:41
@echo off
Setlocal Enabledelayedexpansion
set str=^<a^>&set dos=&set fn=9.txt
for /f "tokens=*" %%i in ('findstr /n "$" %fn%') do (
   set aa=%%i&set aa=!aa:*:=!
   if not defined dos (
      set dos=cndos
      if defined aa (echo !str!!aa!>%fn%) else echo.>%fn%
   ) else if defined aa (echo !str!!aa!>>%fn%) else echo.>>%fn%
)
拼的,不好意思.去掉了过渡文件