Board logo

标题: [其他] 求助:各位大佬,能否帮忙写个添加文本的批处理 [打印本页]

作者: hfstar    时间: 2021-10-21 09:31     标题: 求助:各位大佬,能否帮忙写个添加文本的批处理

文件是.js格式的
{"name":"XdRs_VirtualButton","status":false,"description":"虚拟按键 <MV + MZ>","parameters":{"rockerX":"20","rockerY":"450","rockerDiam":"160","keyA":"{\"x\":\"590\",\"y\":\"540\",\"width\":\"86\",\"height\":\"56\",\"value\":\"90\",\"text\":\"A\"}","keyB":"{\"x\":\"700\",\"y\":\"540\",\"width\":\"86\",\"height\":\"56\",\"value\":\"88\",\"text\":\"B\"}","keyC":"","keyD":"","keyE":""}},
];

  如果在最后一行的【];】前没有发现
{"name":"Cheat_Menu","status":true,"description":"","parameters":{}},
{"name":"Drill_SpeedGear","status":true,"description":"[v1.2]        系统 - 变速齿轮","parameters":{"初始齿轮速度":"2.50","是否绑定齿轮到变量":"false","绑定的变量":"0","声音是否变速":"true","声音变速同步率":"0.25"}}
则添加
{"name":"Cheat_Menu","status":true,"description":"","parameters":{}},
{"name":"Drill_SpeedGear","status":true,"description":"[v1.2]        系统 - 变速齿轮","parameters":{"初始齿轮速度":"2.50","是否绑定齿轮到变量":"false","绑定的变量":"0","声音是否变速":"true","声音变速同步率":"0.25"}}
,如果有则不添加

添加后是这样的,
{"name":"XdRs_VirtualButton","status":false,"description":"虚拟按键 <MV + MZ>","parameters":{"rockerX":"20","rockerY":"450","rockerDiam":"160","keyA":"{\"x\":\"590\",\"y\":\"540\",\"width\":\"86\",\"height\":\"56\",\"value\":\"90\",\"text\":\"A\"}","keyB":"{\"x\":\"700\",\"y\":\"540\",\"width\":\"86\",\"height\":\"56\",\"value\":\"88\",\"text\":\"B\"}","keyC":"","keyD":"","keyE":""}},
{"name":"Cheat_Menu","status":true,"description":"","parameters":{}},
{"name":"Drill_SpeedGear","status":true,"description":"[v1.2]        系统 - 变速齿轮","parameters":{"初始齿轮速度":"2.50","是否绑定齿轮到变量":"false","绑定的变量":"0","声音是否变速":"true","声音变速同步率":"0.25"}}
];
  请各位大佬帮忙写个判断的批处理
作者: qixiaobin0715    时间: 2021-10-21 10:33

本帖最后由 qixiaobin0715 于 2021-10-21 10:35 编辑

是一个js文件还是多个?
只有最后一行是“];”吗?
作者: hfstar    时间: 2021-10-21 10:41

回复 2# qixiaobin0715


    js文件只是一个,【],】有很多个,但是,这个【],】是在最后一行,在末尾
作者: qixiaobin0715    时间: 2021-10-21 11:10

未测试
  1. @echo off
  2. set "var1={"name":"Cheat_Menu","status":true,"description":"","parameters":{}},"
  3. set "var2={"name":"Drill_SpeedGear","status":true,"description":"[v1.2]        系统 - 变速齿轮","parameters":{"初始齿轮速度":"2.50","是否绑定齿轮到变量":"false","绑定的变量":"0","声音是否变速":"true","声音变速同步率":"0.25"}}"
  4. setlocal enabledelayedexpansion
  5. findstr /l /c:"%var1%" /c:"%var2%" abc.js
  6. if not %errorlevel%==0 (
  7. (for /f "delims=" %%a in (abc.js) do (
  8. echo,!str!
  9. set "str=%%a"
  10. )
  11. echo,%var1%
  12. echo,%var2%
  13. echo,];)>new_abc.js
  14. )
  15. pause
复制代码

作者: hfstar    时间: 2021-10-21 11:12

回复 4# qixiaobin0715


    多谢多谢
作者: aloha20200628    时间: 2021-10-21 14:02

4楼代码似乎未处理源文件末尾的 ];
楼主可以试试如下代码:
@echo off
set jsFile="楼主设定.js"
set LastLine#1="{"name":"Cheat_Menu","status":true,"description":"","parameters":{}},"
set LastLine#2="{"name":"Drill_SpeedGear","status":true,"description":"[v1.2]        系统 - 变速齿轮","parameters":{"初始齿轮速度":"2.50","是否绑定齿轮到变

量":"false","绑定的变量":"0","声音是否变速":"true","声音变速同步率":"0.25"}}"
for /f %%n in ('find /v /c "" ^< %jsFile%') do set/a totalLines=%%n
setlocal enabledelayedexpansion
set/a n=0
(for /f "usebackq tokens=* delims=" %%s in (%jsFile%) do (
   set/a n+=1
   if !n! equ %totalLines% (
      if !lastline! neq !LastLine#2! (
         echo,!LastLine#1!
         echo,!LastLine#2!
      )
   )
   echo,%%s
   set lastline="%%s"
))>new.js
endlocal&exit

说明》代码中用 find.exe 获取文件行数...
作者: hfstar    时间: 2021-10-21 16:36

回复 6# aloha20200628


    今天忙,还来不及测试呢,谢谢各位的指点
作者: aloha20200628    时间: 2021-10-21 17:43

试了试自己的代码,更正如下:
set/p jsFile="拖入一个测试文件:"
::set jsFile="楼主设定.js"
set "LastLine#1={"name":"Cheat_Menu","status":true,"description":"","parameters":{}},"
set "LastLine#2={"name":"Drill_SpeedGear","status":true,"description":"[v1.2]        系统 - 变速齿轮","parameters":{"初始齿轮速度":"2.50","是否绑定齿轮到变

量":"false","绑定的变量":"0","声音是否变速":"true","声音变速同步率":"0.25"}}"
for /f %%n in ('find /v /c "" ^< %jsFile%') do set/a totalLines=%%n
setlocal enabledelayedexpansion
set/a n=0
(for /f "usebackq tokens=* delims=" %%s in (%jsFile%) do (
   set/a n+=1
   if !n! equ %totalLines% (
      if "!lastline!" neq "!LastLine#2!" (
         echo,!LastLine#1!
         echo,!LastLine#2!
      )
   )
   echo,%%s
   set "lastline=%%s"
))>new.js
endlocal&exit

4楼的代码未试成功...
作者: hfstar    时间: 2021-10-21 18:41

本帖最后由 hfstar 于 2021-10-21 18:45 编辑

回复 9# aloha20200628


    刚刚测试了.没有成功,这样吧,我把文件发上来

https://wwi.lanzoui.com/iFhtmvlnvhi
作者: went    时间: 2021-10-21 20:24

test.bat, ansi编码
  1. #&@cls&cd /d "%~dp0" & powershell -c "Get-Content '%~0' | Select-Object -Skip 1 | Out-String | Invoke-Expression" & pause&exit
  2. #文件
  3. $src_js = 'plugins.js'
  4. #要比较的数据
  5. $const1 = '{"name":"Cheat_Menu","status":true,"description":"","parameters":{}},'
  6. $const2 = '{"name":"Drill_SpeedGear","status":true,"description":"[v1.2]        系统 - 变速齿轮","parameters":{"初始齿轮速度":"2.50","是否绑定齿轮到变量":"false","绑定的变量":"0","声音是否变速":"true","声音变速同步率":"0.25"}}'
  7. #读取
  8. [string[]]$txt_list = Get-Content $src_js -Encoding UTF8 | Where-Object { $_ -notmatch '^\s+$'}
  9. #比较并输出
  10. &{
  11.     $txt_list[0..($txt_list.Length - 2)]
  12.     if($txt_list[$txt_list.Length - 3] -ne $const1 -and $txt_list[$txt_list.Length - 2] -ne $const2){
  13.         $const1
  14.         $const2
  15.     }
  16.     $txt_list[$txt_list.Length - 1]
  17. } | Out-File $src_js -Encoding utf8
复制代码
你的js文件其中有一行5万多个字符,纯bat读取不了
作者: hfstar    时间: 2021-10-21 20:33

回复 11# went


    牛!搞定了,多谢多谢
作者: aloha20200628    时间: 2021-10-22 11:38

今早拾贴才见楼主的js脚本中真有超过8K文本行字符量的'大坑',而且还是utf-8编码文件。这二者都算是碰了纯P玩法的'雷池'。若仅是前者,还可请'文本处理三剑客'赐福纯P代码。若是这二者联手,就难为纯P代码了...也许只能移步 vbs/PS/Py 任一脚本主场...
   谨记。
作者: hfstar    时间: 2021-10-22 11:53

:lol 回复 12# aloha20200628
作者: hfstar    时间: 2021-10-23 16:34

回复 10# went

   脚本很好用,再次向你求助一下,如果路径加判断该怎么写呢,一般的批处理,这样写即可
if exist "www\js\plugins.js" (
    set "$src_js=www\js\plugins.js"
) else if exist "js\plugins.js" (
    set "$src_js=js\plugins.js"
) else (
echo       没有发现 plugins.js & echo.
echo       请按任意键退出...& pause>nul & exit
)

那么,在您的这个脚本中,要实现上面批处理的判断,该怎么写呢?
作者: went    时间: 2021-10-23 18:59

回复 14# hfstar


    复制替换
  1. #&@cls&cd /d "%~dp0" & powershell -c "$cur_path='%~dp0';Get-Content '%~0' | Select-Object -Skip 1 | Out-String | Invoke-Expression" & pause&exit
  2. #文件
  3. $src_js = $cur_path + 'www\js\plugins.js'
  4. if(![System.IO.File]::Exists($src_js)){
  5. $src_js = $cur_path + 'js\plugins.js'
  6. if(![System.IO.File]::Exists($src_js)){
  7. Write-Host '没有发现 plugins.js'
  8. exit
  9. }
  10. }
  11. #要比较的数据
复制代码

作者: hfstar    时间: 2021-10-23 19:05

回复 15# went


    好使,谢谢谢谢




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