Board logo

标题: [文本处理] 批处理如何删除txt文本末尾最后的3个字符/几个字符 [打印本页]

作者: yujiaphf    时间: 2016-10-16 06:04     标题: 批处理如何删除txt文本末尾最后的3个字符/几个字符

本帖最后由 pcl_test 于 2017-3-19 04:42 编辑

如何删除txt文件中最后3个字符
作者: pcl_test    时间: 2016-10-16 06:48

  1. rem win7及以上系统运行
  2. powershell -c "[IO.File]::ReadAllText('测试.txt',[Text.Encoding]::Default) -replace '[^\r\n]{3}[\r\n]*$'"
  3. pause
复制代码

作者: yujiaphf    时间: 2016-10-16 08:37

XP系统的,好像不好用
作者: happy886rr    时间: 2016-10-16 09:48

回复 3# yujiaphf
兼容各种特殊字符,把%~f0改成你的文件名即可
  1. @echo off
  2. set FileName="%~f0"
  3. for /f "tokens=1-2 delims=:" %%a in ('findstr /n .* %FileName%') do (set i=%%a)
  4. (
  5. for /l %%j in (1,1,%i%) do (
  6. set "str="
  7. set/p str=
  8. setlocal enabledelayedexpansion
  9. if %%j equ %i% (set "str=!str:~0,-3!")
  10. echo;!str!
  11. endlocal
  12. )
  13. )<%FileName%
  14. set/p=
  15. 留意尾部要消失的!*^ ,#%!*^
复制代码

作者: bailong360    时间: 2016-10-16 10:53

  1. @mshta http://bathome.net/s/hta/ type('test.txt').replace(/...$/, '')|more >new.txt
复制代码

作者: CrLf    时间: 2016-10-17 09:37

回复 5# bailong360


    重定向不需要 more,可以直接 >new.txt




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2