找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 7786|回复: 22

[文本处理] 【已解决】批处理如何从第4行起删除每行的行尾数字

[复制链接]
发表于 2025-8-21 12:57:09 | 显示全部楼层 |阅读模式
这个问题的起因是使用 Orca 软件导出 表(.idt)后,从第 4 行起删除每行末尾的乱序数字,此数字长 1~5 位。表 示例如下:
  1. File        Component_        FileName        FileSize        Version        Language        Attributes        Sequence
  2. s72        s72        l255        i4        S72        S20        I2        i4
  3. File        File
  4. agnosticuilib.dll        Viewer        maoufxaj.dll|agnosticuilib.dll        1251424        3.0.0.2418        0        1536        4
  5. Activation.exe        Viewer        mgkjfdue.exe|Activation.exe        17197152        13.0.0.21632        0        1536        2
  6. AIPLabelfpi        AIPLabelfpi        AIPLabel.fpi        535648        13.0.0.21632        1033        1536        5
  7. adaldll        Viewer        adal.dll        1497720        2.0.0.19401        1033        1536        3
  8. Alfresco.fpi        Alfrescofpi        Alfresco.fpi        5657184        13.0.0.21632        1033        1536        6
  9. apimswincoreconsolel110.dll        Viewer        7g0h2qpu.dll|api-ms-win-core-console-l1-1-0.dll        18696        10.0.17134.12        1033        1536        7
  10. apimswincorefilel120.dll        Viewer        p05oa5h9.dll|api-ms-win-core-file-l1-2-0.dll        18184        10.0.17134.12        1033        1536        12
  11. apimswincoredatetimel110.dll        Viewer        zhc6g3dg.dll|api-ms-win-core-datetime-l1-1-0.dll        18184        10.0.17134.12        1033        1536        8
  12. apimswincoredebugl110.dll        Viewer        xcldxomg.dll|api-ms-win-core-debug-l1-1-0.dll        18184        10.0.17134.12        1033        1536        9
  13. apimswincorefilel210.dll        Viewer        nskzl4i-.dll|api-ms-win-core-file-l2-1-0.dll        18184        10.0.17134.12        1033        1536        13
  14. apimswincoreerrorhandlingl110.dll        Viewer        etxbuq2g.dll|api-ms-win-core-errorhandling-l1-1-0.dll        18184        10.0.17134.12        1033        1536        10
  15. apimswincoreheapl110.dll        Viewer        ifx97dtf.dll|api-ms-win-core-heap-l1-1-0.dll        18184        10.0.17134.12        1033        1536        15
  16. ActionWizard.fpi        ActionWizard        cozefaey.fpi|ActionWizard.fpi        654432        13.0.0.21632        1033        1536        1
  17. apimswincorefilel110.dll        Viewer        utuy1new.dll|api-ms-win-core-file-l1-1-0.dll        21768        10.0.17134.12        1033        1536        11
  18. apimswincorehandlel110.dll        Viewer        xkq0otea.dll|api-ms-win-core-handle-l1-1-0.dll        18184        10.0.17134.12        1033        1536        14
复制代码
求助:现需要使用批处理从第4行起删除每行末尾的乱序数字(长度 1~5)位。例如:1 ~ 65535,谢谢!

后续精简文件行后再【已解决】重新分配升序序号


未评分的回复请见谅,因为超出限制了。

评分

参与人数 1PB +2 收起 理由
Batcher + 2 感谢给帖子标题标注[已解决]字样

查看全部评分

发表于 2025-8-21 15:41:10 | 显示全部楼层
第3行可设置文件名或路径
第4行可设置行数
第5行可设置文件编码
不删除数字前的分割符 ,仅删除末尾数字
运行后会添加前缀并创建新文件

  1. @set @do_not_save_as_utf8=1;/*&echo off

  2. set "file=a.idt"
  3. set "line_del=4"
  4. set "encoding=gbk"

  5. cscript //nologo //e:jscript "%~f0" "%file%" "%line_del%" "%encoding%"
  6. echo,&echo done&pause&exit /b 0 */

  7. (function(argv,stdin,stdout,stderr,wscript,nil,not_nil){if(!argv.length)return;

  8. var p=argv(0).split(/\\|\/|\\\\/);
  9. var file_in=file_new(argv(0),'r',argv(2));
  10. var file_out=file_new(nil,'w',argv(2));

  11. if(!file_in){
  12. wscript.echo('open file '+argv(0)+' error');
  13. file_out.close();
  14. wscript.quit();
  15. }

  16. var str=file_in.readtext(8192);
  17. file_in.position=0;
  18. var l=str.match(/\r\n?|\n/);
  19. if(l){
  20. if('\n'==l[0])file_in.lineseparator=10;
  21. if('\r'==l[0])file_in.lineseparator=13;
  22. if('\r\n'==l[0])file_in.lineseparator=-1;
  23. }else{file_in.lineseparator=-1;}
  24. file_out.lineseparator=file_in.lineseparator;

  25. var i=0,line_del=argv(1)>0?argv(1)-1:0;
  26. while(!file_in.eos){
  27. str=file_in.readtext(-2);
  28. if(line_del){
  29. line_del--;
  30. file_out.writetext(str,1);
  31. }else{
  32. i++;
  33. file_out.writetext(str.replace(/\t\d+$/,'\t'),1);
  34. }}

  35. file_out.savetofile('@@new_'+p.pop(),2);
  36. file_in.close();
  37. file_out.close();

  38. function file_new(n,m,e){var t=2,r=GetObject('new:adodb.stream');try{if(e)r.charset=e;
  39. if(-1!=m.indexOf('b'))t=1;r.type=t;r.open();if(n&&''!=n)r.loadfromfile(n);}catch(l){r=nil}
  40. return r;}
  41. })(WScript.Arguments,WScript.StdIn,WScript.StdOut,WScript.StdErr,WScript,null);
复制代码

评分

参与人数 1技术 +1 收起 理由
chishingchan + 1 谢谢!

查看全部评分

发表于 2025-8-21 17:56:58 | 显示全部楼层
本帖最后由 aloha20200628 于 2025-8-21 18:03 编辑

回复 1# chishingchan

假设源文件 a.txt 从第4行开始每行中各字段不会包含空格,逗号,分号,等号这些系统默认分割符,可用以下代码

  1. @echo off &setlocal enabledelayedexpansion
  2. (for /f "tokens=1* delims=:" %%a in ('findstr /n .* a.txt') do if %%a lss 4 (echo.%%b) else (
  3.     set "_last=" &set "v="
  4.     for %%x in (%%b) do (
  5.        if defined _last (set "v=!v!        !_last!")
  6.        set "_last=%%x")
  7.     echo.!v:~1!) )>b.txt
  8. pause&exit/b
复制代码
如果源文件 a.txt 从第4行开始每行中各字段可能包含空格,逗号,分号,等号这些系统默认分割符,须用以下代码,但效率会有所降低...

  1. @echo off &setlocal enabledelayedexpansion
  2. (for /f "tokens=1* delims=:" %%a in ('findstr /n .* a.txt') do if %%a lss 4 (echo.%%b) else (
  3.    set "v=" &call :xxx "%%b" &echo.!v:~1!) )>b.txt
  4. pause&exit/b
  5. :xxx
  6.    for /f "tokens=1* delims=        " %%x in ("%~1") do if "%%~y"=="" (exit/b) else (set "v=!v!        %%x" &call :xxx "%%~y")               
  7.    exit/b
复制代码

评分

参与人数 1技术 +1 收起 理由
chishingchan + 1 谢谢!

查看全部评分

发表于 2025-8-21 19:24:00 | 显示全部楼层
本帖最后由 aloha20200628 于 2025-8-21 20:31 编辑

回复 1# chishingchan

再给一个批处调用 powershell 功能的版本,存为 test.bat 运行,与源文件 a.txt 同目录...

  1. @echo off &powershell "$a=gc 'a.txt';$a[0..2]|%%{$_};$a[3..($a.length-1)]|%%{$_.substring(0,$_.LastIndexOf('        '))}">b.txt
  2. pause&exit/b
复制代码
 楼主| 发表于 2025-8-21 20:14:43 | 显示全部楼层
回复 3# aloha20200628


    老大,可以帮我使用批处理一楼的示例文件后的结果如下:
  1. File\agnosticuilib.dll
  2. File\Activation.exe
  3. File\AIPLabelfpi
  4. File\adaldll
  5. File\Alfresco.fpi
  6. File\apimswincoreconsolel110.dll
  7. File\apimswincorefilel120.dll
  8. File\apimswincoredatetimel110.dll
  9. File\apimswincoredebugl110.dll
  10. File\apimswincorefilel210.dll
  11. File\apimswincoreerrorhandlingl110.dll
  12. File\apimswincoreheapl110.dll
  13. File\ActionWizard.fpi
  14. File\apimswincorefilel110.dll
  15. File\apimswincorehandlel110.dll
复制代码
发表于 2025-8-21 20:57:44 | 显示全部楼层
回复 5# chishingchan

用3-4楼各段代码测试一楼示例文件,其结果均会如期删除每个数据行 ...1536之后的数字,未出现5楼所示的结果...
 楼主| 发表于 2025-8-22 08:50:09 | 显示全部楼层
回复 6# aloha20200628


    我想要5楼的结果:获取第4行起每一行第一列的文件名,并在文件名前i添加 File\ 。
发表于 2025-8-22 09:26:49 | 显示全部楼层
回复 7# chishingchan

  1. @echo off &(for /f "skip=3 delims=         " %%a in (a.txt) do echo.File\%%a)>b.txt
  2. pause&exit/b
复制代码
 楼主| 发表于 2025-8-22 10:06:54 | 显示全部楼层
回复 8# aloha20200628


    谢谢!
发表于 2025-8-22 14:50:28 | 显示全部楼层
我数了下,都是8列,两个问题一次解决。
  1. @echo off
  2. cd /d "%~dp0"
  3. for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^<"1.idt"') do (
  4.         if %%a leq 3 (
  5.                 echo=%%b
  6.         ) else (
  7.                 for /f "tokens=1-8 delims=        " %%a in ("%%b") do (
  8.                         set /a n+=1
  9.                         set str=%%a        %%b        %%c        %%d        %%e        %%f        %%g
  10.                         setlocal enabledelayedexpansion
  11.                         echo !str!        !n!
  12.                         endlocal
  13.                 )
  14.         )
  15. )
  16. pause
复制代码
 楼主| 发表于 2025-8-22 19:19:44 | 显示全部楼层
本帖最后由 chishingchan 于 2025-8-22 23:24 编辑

回复 10# 77七


    测试了一下,只修改了 1.idt 为 File.idt,没效果!

根据老大们的代码,我修改如下,看看能否解决三个疑难问题:
  1. @echo off
  2. cd /d %~dp0

  3. type nul>Import_Table_File.idt
  4. type nul>File.txt

  5. for /f "tokens=1* delims=:" %%i in ('findstr /n .* File.idt') do (
  6.         if %%i lss 4 (
  7.                 echo %%j>>Import_Table_File.idt
  8.         ) else (
  9.         goto ExitFor
  10.         )
  11. )

  12. :ExitFor
  13. setlocal enabledelayedexpansion
  14. for /f "skip=3 tokens=1-8 delims=        " %%a in (File.idt) do (
  15.         set /a n+=1
  16.         echo %%a        %%b        %%c        %%d        %%e        %%f        %%g        !n!>>Import_Table_File.idt
  17.         echo File\%%a>>File.txt
  18. )
复制代码
经核对,生成 Import_Table_File.idt 的方法错误的!出现错误数据列!
老大们能否给我这段代码再优化一下,谢谢!
 楼主| 发表于 2025-8-22 23:41:16 | 显示全部楼层
回复 3# aloha20200628


    老大,以上问题都是采纳你的答案,能否将代码组合一下,使 删除序号、添加序号、生成文件列表 同时进行,节省时间。谢谢!
发表于 2025-8-23 00:10:16 | 显示全部楼层
回复 11# chishingchan


   
你改的没错。

  1. @echo off
  2. cd /d "%~dp0"
  3. (for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^<"1.idt"') do (
  4.         if %%a leq 3 (
  5.                 echo=%%b
  6.         ) else (
  7.                 for /f "tokens=1-8 delims=        " %%a in ("%%b") do (
  8.                         set /a n+=1
  9.                         set str=%%a        %%b        %%c        %%d        %%e        %%f        %%g
  10.                         >> "list.txt" echo File\%%a
  11.                         setlocal enabledelayedexpansion
  12.                         echo !str!        !n!
  13.                         endlocal
  14.                 )
  15.         )
  16. )) > "2.idt"
  17. pause
复制代码
 楼主| 发表于 2025-8-23 07:55:32 | 显示全部楼层
本帖最后由 chishingchan 于 2025-8-23 08:05 编辑

回复 13# 77七

老大,这个批处理不对劲。因为有些行其中有 3 个跳表符存在,例如以下文件数据的最后一行:
File.idt
  1. File        Component_        FileName        FileSize        Version        Language        Attributes        Sequence
  2. s72        s72        l255        i4        S72        S20        I2        i4
  3. File        File
  4. AIPLabelfpi        AIPLabelfpi        AIPLabel.fpi        535648        13.0.0.21632        1033        1536        5
  5. APIMSWincorexstatel210.dll        Viewer        pjzw0ihk.dll|API-MS-Win-core-xstate-l2-1-0.dll        2560        10.0.17134.12        1033        1536        32
  6. ActionWizard.fpi        ActionWizard        cozefaey.fpi|ActionWizard.fpi        654432        13.0.0.21632        1033        1536        1
  7. Activation.exe        Viewer        mgkjfdue.exe|Activation.exe        17197152        13.0.0.21632        0        1536        2
  8. Alfresco.fpi        Alfrescofpi        Alfresco.fpi        5657184        13.0.0.21632        1033        1536        6
  9. BitMailAgent        BitMailAgent        9xvzvips.exe|64BitMailAgent.exe        512096        1.0.8.1228        0        1536        48
  10. Bookmark        Bookmark        Bookmark.fpi        1590880        13.0.0.21632        2052        1536        49
  11. BoxPlugin.fpi        BoxPlugin        veo3zuir.fpi|BoxPlugin.fpi        4972640        13.0.0.21632        0        1536        50
  12. Calculator        Calculator        w4waceym.fpi|Calculator.fpi        3839584        13.0.0.21632        2052        1536        51
  13. CheckLicenceDll.dll        CheckLicenceDll        phc.dll        722944        13.0.0.21632        0        1536        52
  14. CheckLicenceX64_Dll.dll        CheckLicenceDll2        phc64.dll        788992        13.0.0.21632        0        1536        53
  15. CheckLicenceX86_Dll.dll        CheckLicenceDll2        phc.dll        722944        13.0.0.21632        0        1536        54
  16. CommentsSummary.fpi        CommentsSummary        66iuskyu.fpi|CommentsSummary.fpi        602720        13.0.0.21632        0        1536        55
  17. ComparePDF        ComparePDF        6v9mf7bn.fpi|ComparePDF.fpi        517728        13.0.0.21632        0        1536        56
  18. ConnectedPDFLogin        ConnectedPDFLogin        eks9tbgk.dll|ConnectedPDFLogin.dll        2826848        13.0.0.21632        1033        1536        58
  19. ConnectedPDFLogin64        ConnectedPDFLogin        vpv4twiv.dll|ConnectedPDFLogin64.dll        3743328        13.0.0.21632        1033        1536        59
  20. ContentSuite.fpi        ContentSuitefpi        zbqplez4.fpi|ContentSuite.fpi        2064480        13.0.0.21632        1033        1536        60
  21. ConvertToPDF.dll        ConvertToPDFDll        zwid-adl.dll|ConvertToPDF_x86.dll        19192416        13.0.0.15725        0        1536        61
  22. ConvertToPDF3.dll        ConvertToPDFDll2        ndvmxdsc.dll|ConvertToPDF_x64.dll        23299168        13.0.0.15725        0        1536        62
  23. CrossReferenceLinks        CrossReferenceLinks        mpuhzevn.fpi|CrossReferenceLinks.fpi        436832        13.0.0.21632        1033        1536        63
  24. DMSforLegal.fpi        DmsForLegal        gbjcbk0o.fpi|DMSforLegal.fpi        2182240        13.0.0.21632        1033        1536        65
  25. DefaultPluginsList.xml        DefaultPluginsList        bssyikxc.xml|DefaultPluginsList.xml        28573                        1536        64
复制代码
错误的处理结果:
  1. DefaultPluginsList.xml        DefaultPluginsList        bssyikxc.xml|DefaultPluginsList.xml        28573        1536        64                22
复制代码
最后正确结果应该是这样才对:
  1. DefaultPluginsList.xml        DefaultPluginsList        bssyikxc.xml|DefaultPluginsList.xml        28573                        1536        22
复制代码
麻烦老大修正一下,谢谢!
发表于 2025-8-23 09:24:40 | 显示全部楼层
回复 14# chishingchan


  
  1. @echo off
  2. cd /d "%~dp0"
  3. (for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^<"1.idt"') do (
  4.         if %%a leq 3 (
  5.                 echo=%%b
  6.         ) else (
  7.                 set str=%%b@#@#
  8.                 set /a n+=1
  9.                 setlocal enabledelayedexpansion
  10.                 for /f "tokens=1-2* delims=" %%c in ("!n!\!str:        =\!") do (
  11.                         echo !str:%%~nxe=%%c!
  12.                         endlocal
  13.                         >> "list.txt" echo %%d
  14.                 )
  15.         )
  16. )) > "2.idt"
  17. pause
复制代码

评分

参与人数 1技术 +1 收起 理由
chishingchan + 1 list.txt 要初始化

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 07:05 , Processed in 0.038838 second(s), 15 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表