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

[文本处理] 批处理替换文本指定字符串问题

{"AlignCell":true,"AutoSort":false,"BlackTagIconList":[{"Type":"sysIcon","name":"呼叫服务.lnk"},{"Type":"sysIcon","name":"商品超市.lnk"}],"IconCellid":"","IconList":[{"Type":"sysIcon","bottom":440,"left":10,"name":"英雄联盟TGP.lnk","right":110,"top":340},{"Type":"sysIcon","bottom":240,"left":10,"name":"网吧专用.lnk","right":110,"top":140},{"Type":"sysIcon","bottom":340,"left":10,"name":"腾讯QQ.lnk","right":110,"top":240},{"Type":"sysIcon","bottom":140,"left":10,"name":"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}","right":110,"top":40},{"Type":"sysIcon","bottom":540,"left":10,"name":"侠盗列车5.lnk","right":110,"top":440},{"Type":"sysIcon","bottom":640,"left":10,"name":"挂机锁.lnk","right":110,"top":540},{"Type":"sysIcon","bottom":740,"left":10,"name":"用户中心.lnk","right":110,"top":640},{"Type":"sysIcon","bottom":840,"left":10,"name":"自助结账.lnk","right":110,"top":740}],"IconSize":2,"WallpaperType":0,"WhiteIconList":[{"Type":"sysIcon","name":"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"},{"Type":"sysIcon","name":"侠盗列车5.lnk"},{"Type":"sysIcon","name":"用户中心.lnk"},{"Type":"sysIcon","name":"网吧专用.lnk"},{"Type":"sysIcon","name":"自助结账.lnk"},{"Type":"sysIcon","name":"英雄联盟TGP.lnk"}]}
以上是批处理一个文本内容 我的目的就是提取"WallpaperType":0 后面的0是可变的 无论怎么变都是要替换成"WallpaperType":0

  1. @echo off
  2. ::依山居 1:54 2015/11/9
  3. ::fr修改WallpaperType
  4. ::http://www.bathome.net/thread-38056-1-1.html
  5. ::fr是一个完整支持正则表达式查找替换的命令行工具,
  6. ::作者网站:http://baiy.cn/utils/fr/index.htm
  7. ::去掉-stdout参数则直接修改原文件
  8. ::直接修改原文件
  9. fr -r:"\"WallpaperType\":\d+," -t:"\"WallpaperType\":1," -trc x.json
  10. ::修改的内容,原文件不修改,重定向输出到新文件。
  11. fr -r:"\"WallpaperType\":\d+," -t:"\"WallpaperType\":1," -stdout -trc x.json>test.txt
  12. pause
复制代码

TOP

既然是 JSON,那用 JS 最方便了的:
  1. <!-- :
  2. @echo off
  3. mshta "%~f0" <转换前.txt >转换后.txt
  4. exit
  5. -->
  6. <script src=http://www.bathome.net/lib/diy/hide.js></script>
  7. <script src=http://www.bathome.net/lib/diy/WSH.js></script>
  8. <script src=http://www.bathome.net/lib/json2.js></script>
  9. <script>
  10. var json = JSON.parse(WSH.StdIn.ReadAll())
  11. json.WallpaperType = 0
  12. WSH.Echo(JSON.stringify(json))
  13. </script>
复制代码

TOP

终于编好了,楼主测试吧
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "delims=" %%i in ('type "%~dp0\1.txt"') do (
  4. for %%j in (%%i) do (
  5.   for /f "tokens=1,* delims=:" %%k in ("%%j") do (
  6.    if /i "%%~k"=="WallpaperType" (
  7.     set "name=!name!,"%%~k":0"
  8.    ) else (
  9.     if defined name set "name=!name!,%%j"
  10.     if not defined name set "name=%%j"
  11.    )
  12.   )
  13. )
  14. )
  15. (echo=!name!)>2.txt
  16. pause
复制代码
这么好的论坛你上哪找,运行测试环境为6.1.7601-64
注:请遵守互联网信息安全,勿用于非法用途

TOP

  1. sed -r "s/(\"WallpaperType\":)[^,]+,/\10,/" 1.txt > 2.txt
复制代码
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表