标题: [文本处理] 【已解决】请教批处理文本替换的问题 [打印本页]
作者: 不知道是谁 时间: 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
文本某行太长可能会读取或比较不了出错(???)- @echo off&pause
-
- set "file=aaaaaa.txt"
- goto :pre
-
- 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]../../../../../存档");
-
- :pre
- for /f "skip=5 delims=" %%a in ('findstr /n .* "%~f0"') do (
- if not defined line1 ( set "line1=%%a"
- ) else if not defined line2 ( set "line2=%%a"
- ) else if not defined line3 ( set "line3=%%a"
- ) else if not defined line4 ( set "line4=%%a"
- ) else if not defined line5 ( set "line5=%%a"
- ) else goto :main
- )
-
- :main
- (
- for /f "delims=" %%a in ('findstr /n .* "%file%"') do (
- set "line=%%a"
- setlocal enabledelayedexpansion
- if "!line:*:=!" == "!line1:*:=!" ( echo,!line4:*:=!
- ) else if "!line:*:=!" == "!line2:*:=!" ( echo,!line5:*:=!
- ) else echo,!line:*:=!
- endlocal
- )
- )>bbbbbb.txt
-
- echo done&pause&exit/b
复制代码
作者: 77七 时间: 2023-11-8 13:08
- @echo off
- set b=D:\\存档
- set "input=1.txt"
- set "output=2.txt"
- for /f "delims=" %%a in ("%b%") do set c=%%~nxa
- for /f "tokens=1* delims=[]" %%a in ('find /n "user_pref(""mail.server.server1.directory""" ^<"%input%"') do (
- set _%%a=1
- )
- for /f "tokens=1* delims=[]" %%a in ('find /n "user_pref(""mail.server.server1.directory-rel""" ^<"%input%"') do (
- set #%%a=1
- )
- (for /f "tokens=1* delims=[]" %%x in ('find /n /v "" ^<"%input%"') do (
- if defined _%%x (
- for /f tokens^=1-5delims^=^" %%a in ("%%y") do (
- echo %%a"%%b"%%c"%b%"%%e
- )
- ) else if defined #%%x (
- for /f tokens^=1-10delims^=^"^/ %%a in ("%%y") do (
- echo %%a"%%b"%%c"%%d/%%e/%%f/%%g/%%h/%c%"%%j
- )
- ) else (
- echo=%%y
- )
- ))>"%output%"
- pause
复制代码
作者: 不知道是谁 时间: 2023-11-8 13:51
回复 2# Five66
非常感谢大佬
因为下面这两行中的路径不是固定值
user_pref("mail.server.server1.directory", "D:\\测试");
user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../测试");
所以我简单修改了下,测试可以使用- @echo off
-
- set "file=a.js"
- goto :pre
-
- user_pref("mail.server.server1.directory", "D:\\存档");
- user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../存档");
-
- :pre
- for /f "skip=5 delims=" %%a in ('findstr /n .* "%~f0"') do (
- if not defined line1 (
- set "line1=%%a"
- ) else if not defined line2 (
- set "line2=%%a"
- ) else goto :main
- )
-
- :main
- chcp 65001
- (
- for /f "delims=" %%a in ('findstr /n .* "%file%"') do (
- set "line=%%a"
- setlocal enabledelayedexpansion
- if "!line:server1.directory-rel=!" NEQ "!line!" (
- echo,!line2:*:=!
- ) else if "!line:server1.directory=!" NEQ "!line!" (
- echo,!line1:*:=!
- ) else echo,!line:*:=!
- endlocal
- )
- )>b.js
-
- pause&exit/b
复制代码
作者: terse 时间: 2023-11-8 17:48
看楼主修改代码,目标行存在唯一关键词的话,这样也行- @echo off
- set "file=a.js"
- set "user_pref("mail.server.server1.directory"=user_pref("mail.server.server1.directory", "D:\\存档");"
- set "user_pref("mail.server.server1.directory-rel"=user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../存档");"
- for /f "usebackq delims=" %%i in ("%file%") do (
- set "line=%%i"
- setlocal enabledelayedexpansion
- for /f "delims=," %%a in ("%%i") do if defined %%a set "line=!%%a! "
- echo !line!
- endlocal
- )
- pause
复制代码
作者: 不知道是谁 时间: 2023-11-8 17:50
稍微再精简了一下- @echo off
-
- set "file=a.js"
-
- set "line1=user_pref("mail.server.server1.directory", "D:\\存档");"
- set "line2=user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../存档");"
-
- chcp 65001
- (
- for /f "delims=" %%a in ('findstr /n .* "%file%"') do (
- set "line=%%a"
- setlocal enabledelayedexpansion
- if "!line:mail.server.server1.directory-rel=!" NEQ "!line!" (
- echo,!line2!
- ) else if "!line:mail.server.server1.directory=!" NEQ "!line!" (
- echo,!line1!
- ) else echo,!line:*:=!
- endlocal
- )
- )>b.js
-
- 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
需要保留空行,所以结合前面的修改了下- @echo off
-
- set "file=p.js"
-
- set "user_pref("mail.server.server1.directory"=user_pref("mail.server.server1.directory", "D:\\存档");"
- set "user_pref("mail.server.server1.directory-rel"=user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../存档");"
-
- chcp 65001
- (
- for /f "delims=" %%i in ('findstr /n .* "%file%"') do (
- set "line=%%i"
- setlocal enabledelayedexpansion
- set "line=!line:*:=!"
- for /f "delims=," %%j in ("!line!") do if defined %%j set "line=!%%j!"
- echo,!line!
- endlocal
- )
- )>a.js
-
- pause
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |