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

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

文件是.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"}}
];
  请各位大佬帮忙写个判断的批处理

回复 15# went


    好使,谢谢谢谢

TOP

回复 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. #要比较的数据
复制代码
1

评分人数

TOP

回复 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
)

那么,在您的这个脚本中,要实现上面批处理的判断,该怎么写呢?

TOP

:lol 回复 12# aloha20200628

TOP

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

TOP

回复 11# went


    牛!搞定了,多谢多谢

TOP

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读取不了
1

评分人数

TOP

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

回复 9# aloha20200628


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

https://wwi.lanzoui.com/iFhtmvlnvhi

TOP

试了试自己的代码,更正如下:
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楼的代码未试成功...

TOP

回复 6# aloha20200628


    今天忙,还来不及测试呢,谢谢各位的指点

TOP

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 获取文件行数...

TOP

回复 4# qixiaobin0715


    多谢多谢

TOP

未测试
  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
复制代码
1

评分人数

TOP

回复 2# qixiaobin0715


    js文件只是一个,【],】有很多个,但是,这个【],】是在最后一行,在末尾

TOP

返回列表