我需要一點幫助
我有一個無法制作的腳本,我確實嘗試過以不同的方式重寫它
這是我的腳本
@echo off
set "replace="code1": ,"
set "replaced="code1": 99999999,"
set "source=File.txt"
set "target=result.txt"
setlocal enableDelayedExpansion
(
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
set "line=%%b"
if defined line set "line=!line:%replace%=%replaced%!"
echo(!line!
)
) > %target%
endlocal
我有一個 12KB 大小的文本檔案,里面有我想用其他資料替換的代碼
我的目標是一個可以用新行替換整行的腳本 我有很多代碼,所以我需要幫助才能讀取輸入的每個代碼,我擁有的代碼數量是未知的 我
在這里構建的代碼是我的例子
file.txt
---content inside the file
"code1": 123456789,
"code2": 123123123,
"code3": 456456456,
New Codes to replace old codes
"code1": 9999999,
"code2": 9999999,
"code3": 9999999,
是否可以制作這樣的腳本請注意這個較低的腳本只是一個例子
@echo off
set "source=File.txt"
set "target=result.txt"
setlocal enableDelayedExpansion
(
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
set "line=%%b"
if defined line set "line=!line:%replace%=%replaced%!"
echo(!line!
set "find=" - if the script can find "code1": and replace it with New "code1":
"code1":
"code2":
"code3":
set "replace="
"code1": 99999999,
"code2": 99999999,
"code3": 99999999,
)
) > %target%
endlocal
我已經制作了 5 個其他腳本,但我無法用它來替換整行
我還想洗掉 %target% 并讓它編輯當前的 file.txt
我有一個想法這樣做
) > %target% to ) >> %source%
不確定是否需要其他任何東西來洗掉目標
Any help will be great
uj5u.com熱心網友回復:
根據我的理解,以下腳本應該執行您想要的操作:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~dp0file.txt" & rem // (path to file to be edited; `%~dp0` is script parent)
set "_CODE=%~dp0code.txt" & rem // (path to file containing new codes to become applied)
set "_TMPF=%~dp0file.tmp" & rem // (path to temporary file)
rem // Write to temporary file:
> "%_TMPF%" (
rem // Loop through lines of file, each one preceded with line number plus `:`:
for /F "delims=" %%L in ('findstr /N "^" "%_FILE%"') do (
rem // Store current line (including line number prefix):
set "LINE=%%L"
rem // Try to extract text portion between prefix and next `:`:
for /F "tokens=2 delims=:" %%K in ("%%L") do (
rem // Check whether text portion is `"` plus anything plus `"`:
cmd /V /D /C echo(!LINE:*:^=!| findstr "^\"[^^^":][^^^":]*\"" > nul && (
rem // Text portion complies with pattern, hence store such:
set "CODE=%%K"
rem // Toggle delayed expansion to avoid troubles with `!` and `^`:
setlocal EnableDelayedExpansion
rem // Escape `findstr` meta-characters `\` and `"` by `\\` and `\"`:
for %%J in ("\" ""^") do set "CODE=!CODE:%%~J=\%%~J!"
rem // Return line beginning with same text portion from other file:
findstr /B /C:"!CODE!:" "!_CODE!"
endlocal
) || (
rem // Text portion does not comply, hence return original line:
setlocal EnableDelayedExpansion
echo(!LINE:*:=!
endlocal
)
)
)
)
rem // Replace original file:
move /Y "%_TMPF%" "%_FILE%"
endlocal
exit /B
您需要在腳本頂部指定一些檔案路徑:
變數
_FILE必須指向包含原始代碼的檔案:"code1": 123456789, "code2": 123123123, "code3": 456456456,變數
_CODE必須指向包含要使用的新代碼的檔案:"code1": 9999999, "code2": 9999999, "code3": 9999999,變數
_TMPF只是指向一個臨時檔案;
uj5u.com熱心網友回復:
可悲的是,我們不知道該檔案的確切外觀,但這應該可以作業:
@echo off
set "source=File.txt"
set "target=result.txt"
setlocal enableDelayedExpansion
(
for /F "tokens=1,2,* delims=:" %%a in ('findstr /N "^" "%source%"') do (
echo %%b|findstr /b "\"code1\" \"code3\"" >nul && (
echo %%b: 999999999,
) || (
if "%%c" == "" (echo(%%b) else echo %%b:%%c
)
)
)> "%target%"
endlocal
type "%target%"
REM move /y "%source%" "%target%"
只需將您的 15 個代碼放入findstr /b搜索掩碼中。
注意:這應該適用于您的 15 個代碼,但搜索findstr模式是有限的,因此當代碼太多(或太長)時,您可能會遇到問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/410389.html
標籤:
