'Change file content using batch
So I have this going and I need a little help to improve it
@ECHO Off
setlocal ENABLEDELAYEDEXPANSION
cd /d "%~dp0"
:: Significant part of string
set "replacemain="appi": "
@For %%G In ("%~dp0Originals") Do Set "sourcedir=%%~fG"
@For %%G In ("%~dp0Ready") Do Set "destdir=%%~fG"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%sourcedir%\(*)*.txt"') DO (
rem calculate new destination directory
SET "newdest=%%~dpq"
SET "newdest=!newdest:%sourcedir%=%destdir%!"
SET "newdest=!newdest:~0,-1!"
MD "!newdest!" 2>nul
(
FOR /f "tokens=1 delims=()" %%j IN ("%%~nxq") DO (
rem %%j now has sequence number
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%%q"') do (
set "line=%%b"
if defined line IF "%%b" neq "!line:appi=!" CALL :subs %%j
echo(!line!
)
)
)>"!newdest!\%%~nxq"
)
GOTO :eof
:: substitute
:subs
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%replacemain%%%s=%replacemain%!"
IF "%original%" neq "%line%" goto subs
set "line=!line:%replacemain%=%replacemain%%1!"
GOTO :eof
This is designed to read anything in the filename in closed by ( )
Main Folder
|
+-\Orignal
|--| + - (100) Craft.txt
|--------------"appi": 4748483,
+-\Ready
|--| + - (100) Craft.txt
|--------------"appi": 100,
My script is based on 1 code editing and now I have been trying to change it to multiple code editing
As you can see here, this is set for one code I would like to change it to a type of dictionary
set "replacemain="appi": "
My goal
replace=replacemain = ("appi":
"code":
"gedt":)
set "replace"
if defined line IF "%%b" neq "!line:replace=!" CALL :subs %%j
echo(!line!
Could this be possible, and all codes will change to match the file name in the (?)
Also is there a way to increase the speed of the script
Thank you
This is how it looks inside my txt file
BEFORE
(100) filename.txt
|----"appi": 4748483,
"code": 415241,
"gedt": 458421,
AFTER
(100) filename.txt
|----"appi": 100,
"code": 100,
"gedt": 100,
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
