[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

批处理把一文本的首行和尾行并接为一行

   一个文本文件内容如下,把它们的第一行和最后一行并接成一行显示在屏幕上

  1. 12345
  2. dw5v
  3. ti7
  4. dfg3
  5. ds120
  6. ds437
  7. dw025
  8. 32123
  9. aseth
  10. ep09
  11. s36
  12. 567
  13. 54dc汉
  14. d36国
  15. 33s
  16. 人中
  17. d2
  18. 6789
复制代码
不大可能的事也许今天实现,根本不可能的事也许明天会实现

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "delims=" %%i in (pjfile.txt) do (
  4. if not defined firstrow (set firstrow=%%i) else (set lastrow=%%i)
  5. )
  6. echo !firstrow!!lastrow!
复制代码

TOP

python
  1. f=open("a.txt","r")
  2. txt=f.readlines()
  3. f.close()
  4. l=txt[0].rstrip()+txt[-1]
  5. print(l)
复制代码

TOP

  1. @echo off
  2. set /p a=<2.txt
  3. for /f %%I in (2.txt) do (
  4.     set b=%%I
  5. )
  6. echo %a%%b%
复制代码

TOP

1.
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%a in (1.txt) do (
  3. set s=%%a
  4. set /a n+=1
  5. if !n!==1 set /p=%%a<nul
  6. echo;%%a>nul
  7. )
  8. echo;!s!
  9. pause
复制代码
2.
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%a in (1.txt) do set /p n=<1.txt&set s=%%a
  3. echo;!n!!s!
  4. pause
复制代码
3.
  1. @echo off&setlocal enabledelayedexpansion
  2. (for /l %%a in (1,1,20) do set /p n%%a=)<1.txt
  3. echo;!n1!!n18!
  4. pause
复制代码

TOP

@echo off&setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=:" %%i in ('type 1.txt^|findstr /n .') do (
        set/a n+=1
        set h!n!=%%j
)
set/a m=%n%-(%n%-1)
echo:!h%m%! !h%n%!

TOP

  1. @echo off&setlocal enabledelayedexpansion
  2. set /p a=<a.txt
  3. for /f %%n in (a.txt) do (
  4.     set b=%%n
  5.   
  6. )
  7. echo %a%!b!
  8. pause
复制代码

TOP

本帖最后由 shelluserwlb 于 2014-11-9 16:08 编辑
  1. @echo off
  2. for /f %%i in (a.txt) do (
  3.     if %%i neq "" (set "str1=%%i" & goto :next)
  4. )
  5. :next
  6. for /f %%i in (a.txt) do (
  7.     if %%i neq "" set "str2=%%i"
  8. )
  9. echo %str1%%str2%
  10. pause>nul
复制代码

TOP

  1. @echo off
  2. cd..\..
  3. cd /d f:\.&type nul>1.txt
  4. @echo off
  5. set str=12345dw5vti7dfg3ds120ds437dw02532123asethep09s3656754dc汉d36国33s人中d26789
  6. echo var=%str:~0,5%
  7. echo %str:~0,5%>f:\1.txt
  8. pause
  9. @echo off
  10. cd..\..
  11. cd /d f:\.&type nul>2.txt
  12. @echo off
  13. set str=12345dw5vti7dfg3ds120ds437dw02532123asethep09s3656754dc汉d36国33s人中d26789
  14. set var=%str:~-4%
  15. echo %str:~-4%
  16. echo %str:~-4%>>f:\2.txt
  17. pause
  18. @echo off
  19. cd..\..
  20. cd /d f:\.&type nul>3.txt&type 1.txt>3.txt%type 2.txt>3.txt
  21. start "" f:\3.txt
  22. pause
复制代码

TOP

@echo off
for /f %%i in (a.txt) do (set a=%%i)

for /f %%i in (a.txt) do (set b=%%i&&goto :b)

:b
echo %b%%a%
pause

TOP

回复 8# battab


    你不如直接写个
  1. echo 123456789
复制代码

TOP

回复 1# ieutk
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f %%i in (1.txt) do (
  3. set str=!str!%%i)
  4. echo %str:~,5%%str:~-4%
  5. pause>nul
复制代码
还不通用,只是个思路,仅符合题例,有待完善。
学习中

TOP

我也试试

其实主要是文本中,第一行和最后一行不能使空的!空行会跳过的!
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "delims= " %%a in (a.txt) do (set/a num+=1&set !num!=%%a)
  4. echo !1!!%num%!
复制代码

[ 本帖最后由 sjzong 于 2009-4-25 18:22 编辑 ]
2

评分人数

TOP

  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%i in (a.txt) do (
  3.     set a=%%i
  4.     set /a n+=1
  5.     if !n!==1 set o=!a!
  6. )
  7. echo %o%%a%&pause>nul
复制代码
(*^_^*)

TOP

@echo off
set /p a=<abc.txt
setlocal EnableDelayedExpansion
for /f %%i in ('findstr /n .* abc.txt') do (set hhh=%%i)
set "hhh=!hhh:*:=%a%!"
echo !hhh!
pause>nul

但首行和尾行不能都为空(空格也算空)否则仅仅会输出echo的状态。等同于一个单独的echo.

TOP

返回列表