Board logo

标题: [文本处理] 【已解决】请教批处理文本替换的问题 [打印本页]

作者: 不知道是谁    时间: 2023-11-7 19:43     标题: 【已解决】请教批处理文本替换的问题

本帖最后由 不知道是谁 于 2023-11-8 13:52 编辑

需要将
user_pref("mail.server.server1.directory", "D:\\测试");
user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../测试");
替换为
user_pref("mail.server.server1.directory", "D:\\存档");
user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../存档");

必须是批处理,希望有大佬可以帮忙解决


文本部分内容如下:

user_pref("extensions.enabledAddons", "clipboard%40ankexinchuang.cn:2.0,extensionManager%40ankexinchuang.cn:2.0,fontSetting%40ankexinchuang.cn:2.0,groupaddressbook%40ankexinchuang.cn:2.0,mailArchiver%40ankexinchuang.cn:3.3.0e,mailboxQuota%40ankexinchuang.cn:2.0,mailrecall%40ankexinchuang.cn:2.0,mintotray%40ankexinchuang.cn:1.3.2e,remoteAddrBook%40ankexinchuang.cn:2.0,superArchive%40ankexinchuang.cn:2.0,winmaildat%40ankexinchuang.cn:1.3.04,%7Be2fda1a4-762b-4020-b5ad-a41df1933103%7D:.4.3.5.9,%7B972ce4c6-7e08-4474-a285-3208198ce6fd%7D:2.3.5.9");
user_pref("mail.identity.id1.doBcc", false);
user_pref("mail.identity.id1.drafts_folder_picker_mode", "0");
user_pref("mail.identity.id1.fcc", false);
user_pref("mail.identity.id1.fcc_folder_picker_mode", "0");
user_pref("mail.identity.id1.reply_on_top", 1);
user_pref("mail.identity.id1.smtpServer", "smtp1");
user_pref("mail.identity.id1.tmpl_folder_picker_mode", "0");
user_pref("mail.identity.id1.valid", true);
user_pref("mail.openMessageBehavior.version", 1);
user_pref("mail.root.imap-rel", "[ProfD]ImapMail");
user_pref("mail.root.none-rel", "[ProfD]Mail");
user_pref("mail.server.default.serverSaveSent", true);
user_pref("mail.server.server1.directory", "D:\\测试");
user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../测试");
user_pref("mail.server.server1.hostname", "Local Folders");
user_pref("mail.server.server1.login_at_startup", false);
user_pref("mail.server.server1.name", "本地文件夹");
user_pref("mail.server.server1.type", "none");
user_pref("mail.server.server1.userName", "nobody");
user_pref("mail.server.server2.ageLimit", 30);
user_pref("mail.server.server2.applyToFlaggedMessages", false);
user_pref("mail.server.server2.cacheCapa.acl", false);
user_pref("mail.server.server2.cacheCapa.quota", false);
user_pref("mail.server.server2.carddav.Changed", false);
user_pref("mail.server.server2.carddav.Error", false);
user_pref("mail.server.server2.check_new_mail", true);
user_pref("mail.server.server2.cleanupBodies", false);
user_pref("mail.server.server2.daysToKeepBodies", 30);
user_pref("mail.server.server2.daysToKeepHdrs", 30);
user_pref("mail.server.server2.downloadByDate", false);
user_pref("mailnews.tags.$label1.color", "#FF0000");
user_pref("mailnews.tags.$label1.tag", "重要");
user_pref("mailnews.tags.$label2.color", "#FF9900");
user_pref("mailnews.tags.$label2.tag", "工作");
user_pref("mailnews.tags.$label3.color", "#009900");
user_pref("mailnews.tags.$label3.tag", "个人信息");
user_pref("mailnews.tags.$label4.color", "#3333FF");
user_pref("mailnews.tags.$label4.tag", "将要做");
user_pref("mailnews.tags.$label5.color", "#993399");
user_pref("mailnews.tags.$label5.tag", "稍后");
作者: Five66    时间: 2023-11-7 23:00

本帖最后由 Five66 于 2023-11-8 01:04 编辑

输入文件的文件名aaaaaa.txt,编码ansi或gbk
输出文件的文件名bbbbbb.txt,编码ansi或gbk
文本某行太长可能会读取或比较不了出错(???)
  1. @echo off&pause
  2. set "file=aaaaaa.txt"
  3. goto :pre
  4. user_pref("mail.server.server1.directory", "D:\\测试");
  5. user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../测试");
  6. user_pref("mail.server.server1.directory", "D:\\存档");
  7. user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../存档");
  8. :pre
  9. for /f "skip=5 delims=" %%a in ('findstr /n .* "%~f0"') do (
  10. if not defined line1 ( set "line1=%%a"
  11. ) else if not defined line2 ( set "line2=%%a"
  12. ) else if not defined line3 ( set "line3=%%a"
  13. ) else if not defined line4 ( set "line4=%%a"
  14. ) else if not defined line5 ( set "line5=%%a"
  15. ) else goto :main
  16. )
  17. :main
  18. (
  19. for /f "delims=" %%a in ('findstr /n .* "%file%"') do (
  20. set "line=%%a"
  21. setlocal enabledelayedexpansion
  22. if "!line:*:=!" == "!line1:*:=!" ( echo,!line4:*:=!
  23. ) else if "!line:*:=!" == "!line2:*:=!" ( echo,!line5:*:=!
  24. ) else echo,!line:*:=!
  25. endlocal
  26. )
  27. )>bbbbbb.txt
  28. echo done&pause&exit/b
复制代码

作者: 77七    时间: 2023-11-8 13:08

  1. @echo off
  2. set b=D:\\存档
  3. set "input=1.txt"
  4. set "output=2.txt"
  5. for /f "delims=" %%a in ("%b%") do set c=%%~nxa
  6. for /f "tokens=1* delims=[]" %%a in ('find /n "user_pref(""mail.server.server1.directory""" ^<"%input%"') do (
  7. set _%%a=1
  8. )
  9. for /f "tokens=1* delims=[]" %%a in ('find /n "user_pref(""mail.server.server1.directory-rel""" ^<"%input%"') do (
  10. set #%%a=1
  11. )
  12. (for /f "tokens=1* delims=[]" %%x in ('find /n /v "" ^<"%input%"') do (
  13. if defined _%%x (
  14. for /f tokens^=1-5delims^=^" %%a in ("%%y") do (
  15. echo %%a"%%b"%%c"%b%"%%e
  16. )
  17. ) else if defined #%%x (
  18. for /f tokens^=1-10delims^=^"^/ %%a in ("%%y") do (
  19. echo %%a"%%b"%%c"%%d/%%e/%%f/%%g/%%h/%c%"%%j
  20. )
  21. ) else (
  22. echo=%%y
  23. )
  24. ))>"%output%"
  25. pause
复制代码

作者: 不知道是谁    时间: 2023-11-8 13:51

回复 2# Five66

非常感谢大佬
因为下面这两行中的路径不是固定值

user_pref("mail.server.server1.directory", "D:\\测试");
user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../测试");

所以我简单修改了下,测试可以使用
  1. @echo off
  2. set "file=a.js"
  3. goto :pre
  4. user_pref("mail.server.server1.directory", "D:\\存档");
  5. user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../存档");
  6. :pre
  7. for /f "skip=5 delims=" %%a in ('findstr /n .* "%~f0"') do (
  8.     if not defined line1 (
  9.         set "line1=%%a"
  10.     ) else if not defined line2 (
  11.         set "line2=%%a"
  12.     ) else goto :main
  13. )
  14. :main
  15. chcp 65001
  16. (
  17. for /f "delims=" %%a in ('findstr /n .* "%file%"') do (
  18.     set "line=%%a"
  19.     setlocal enabledelayedexpansion
  20.     if "!line:server1.directory-rel=!" NEQ "!line!" (
  21.         echo,!line2:*:=!
  22.     ) else if "!line:server1.directory=!" NEQ "!line!" (
  23.         echo,!line1:*:=!
  24.     ) else echo,!line:*:=!
  25.     endlocal
  26. )
  27. )>b.js
  28. pause&exit/b
复制代码

作者: terse    时间: 2023-11-8 17:48

看楼主修改代码,目标行存在唯一关键词的话,这样也行
  1. @echo off
  2. set "file=a.js"
  3. set "user_pref("mail.server.server1.directory"=user_pref("mail.server.server1.directory", "D:\\存档");"
  4. set "user_pref("mail.server.server1.directory-rel"=user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../存档");"
  5. for /f "usebackq delims=" %%i in ("%file%") do (
  6.      set "line=%%i"
  7.      setlocal enabledelayedexpansion
  8.      for /f "delims=," %%a in ("%%i") do if defined %%a set "line=!%%a! "
  9.      echo !line!
  10.      endlocal
  11. )
  12. pause
复制代码

作者: 不知道是谁    时间: 2023-11-8 17:50

稍微再精简了一下
  1. @echo off
  2. set "file=a.js"
  3. set "line1=user_pref("mail.server.server1.directory", "D:\\存档");"
  4. set "line2=user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../存档");"
  5. chcp 65001
  6. (
  7.     for /f "delims=" %%a in ('findstr /n .* "%file%"') do (
  8.         set "line=%%a"
  9.         setlocal enabledelayedexpansion
  10.         if "!line:mail.server.server1.directory-rel=!" NEQ "!line!" (
  11.             echo,!line2!
  12.         ) else if "!line:mail.server.server1.directory=!" NEQ "!line!" (
  13.             echo,!line1!
  14.         ) else echo,!line:*:=!
  15.         endlocal
  16.     )
  17. )>b.js
  18. pause
复制代码

作者: 不知道是谁    时间: 2023-11-8 17:54

本帖最后由 不知道是谁 于 2023-11-8 17:57 编辑

回复 5# terse

确实更简练,我试试
不过文件是utf-8编码,chcp65001这个还是要的
作者: 不知道是谁    时间: 2023-11-8 20:32

看楼主修改代码,目标行存在唯一关键词的话,这样也行
terse 发表于 2023-11-8 17:48


需要保留空行,所以结合前面的修改了下
  1. @echo off
  2. set "file=p.js"
  3. set "user_pref("mail.server.server1.directory"=user_pref("mail.server.server1.directory", "D:\\存档");"
  4. set "user_pref("mail.server.server1.directory-rel"=user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../存档");"
  5. chcp 65001
  6. (
  7.     for /f "delims=" %%i in ('findstr /n .* "%file%"') do (
  8.         set "line=%%i"
  9.         setlocal enabledelayedexpansion
  10.         set "line=!line:*:=!"
  11.         for /f "delims=," %%j in ("!line!") do if defined %%j set "line=!%%j!"
  12.         echo,!line!
  13.         endlocal
  14.     )
  15. )>a.js
  16. pause
复制代码





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