标题: [其他] 求助:各位大佬,能否帮忙写个添加文本的批处理 [打印本页]
作者: 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
未测试- @echo off
- set "var1={"name":"Cheat_Menu","status":true,"description":"","parameters":{}},"
- set "var2={"name":"Drill_SpeedGear","status":true,"description":"[v1.2] 系统 - 变速齿轮","parameters":{"初始齿轮速度":"2.50","是否绑定齿轮到变量":"false","绑定的变量":"0","声音是否变速":"true","声音变速同步率":"0.25"}}"
- setlocal enabledelayedexpansion
- findstr /l /c:"%var1%" /c:"%var2%" abc.js
- if not %errorlevel%==0 (
- (for /f "delims=" %%a in (abc.js) do (
- echo,!str!
- set "str=%%a"
- )
- echo,%var1%
- echo,%var2%
- echo,];)>new_abc.js
- )
- 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编码- #&@cls&cd /d "%~dp0" & powershell -c "Get-Content '%~0' | Select-Object -Skip 1 | Out-String | Invoke-Expression" & pause&exit
- #文件
- $src_js = 'plugins.js'
- #要比较的数据
- $const1 = '{"name":"Cheat_Menu","status":true,"description":"","parameters":{}},'
- $const2 = '{"name":"Drill_SpeedGear","status":true,"description":"[v1.2] 系统 - 变速齿轮","parameters":{"初始齿轮速度":"2.50","是否绑定齿轮到变量":"false","绑定的变量":"0","声音是否变速":"true","声音变速同步率":"0.25"}}'
- #读取
- [string[]]$txt_list = Get-Content $src_js -Encoding UTF8 | Where-Object { $_ -notmatch '^\s+$'}
- #比较并输出
- &{
- $txt_list[0..($txt_list.Length - 2)]
- if($txt_list[$txt_list.Length - 3] -ne $const1 -and $txt_list[$txt_list.Length - 2] -ne $const2){
- $const1
- $const2
- }
- $txt_list[$txt_list.Length - 1]
- } | 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
复制替换- #&@cls&cd /d "%~dp0" & powershell -c "$cur_path='%~dp0';Get-Content '%~0' | Select-Object -Skip 1 | Out-String | Invoke-Expression" & pause&exit
- #文件
- $src_js = $cur_path + 'www\js\plugins.js'
- if(![System.IO.File]::Exists($src_js)){
- $src_js = $cur_path + 'js\plugins.js'
- if(![System.IO.File]::Exists($src_js)){
- Write-Host '没有发现 plugins.js'
- exit
- }
- }
- #要比较的数据
复制代码
作者: hfstar 时间: 2021-10-23 19:05
回复 15# went
好使,谢谢谢谢
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |