Reputation: 145
Hello guys I need a bit of help
I am trying to edit a txt file but it has no data in front of it so I am having a hard tle
Here is my script
@ECHO Off
setlocal ENABLEDELAYEDEXPANSION
cd /d "%~dp0"
set "Mfind= %Random%"
@For %%G In ("%~dp03Ready\NSB") Do Set "sourcedir=%%~fG"
@For %%G In ("%~dp03Ready\NSBEdited") Do Set "destdir=%%~fG"
Set /p "FPats=Enter Your Amount:
FOR /f "dedestination directory
SET "newdest=%%~dpq"
SET "newdest=!newdest:%sourcedir%=%destdir%!"
SET "newdest=!newdest:~0,-1!"
MD "!newdest!" 2>nul
(
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%%q"') do (
set "line=%%b"
if defined line IF "%%b" neq
)
)>"!newdest!\%%~nxq"
)
GOTO :NSBEditing
:nidC1
SET "line=%line:-=%"
SET "original=line%" goto nidC1
set "line=!line:%Mfind%=%Mfind%%FPats%!"
GOTO :eof
Here is my txt file The text file cna reacll part of it<b "upnn": 0, "upfb": true, "upcu": { "adi "isnc": false, "erpm": 0 } }, 58, < ---------------- Trying to edit these numbers { "upma":ue, "upcu": { "adid": "lphaSixoSSC "adtc": [ { "kfti": 1000.0, "kfva": 0.321 }
Upvotes: 0
Views: 101
Reputation: 67296
Ok. Here we go (again)...
First step: (trying to) identify the lines to be edited:
@echo off
setlocal EnableDelayedExpansion
set "last=1"
set "lines=:"
for /F "delims=:" %%a in ('findstr /N "{ } ]" (09)Fus.txt') do (
set /A "prev=%%a-2, line=%%a-1"
if !prev! equ !last! set "lines=!lines!!line!:"
set "last=%%a"
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" (09)Fus.txt') do (
if "!lines::%%a:=!" neq "%lines%" (
echo %%b ^< - This line will be edited
) else (
echo %%b
)
)
Output example:
"cpup": [
{
"upma": "id",
"upnn": 0,
"upfb": true,
"upcu": {
"adid": "Dge_ChngerSTemon_2018_nit2",
"ulid": "TT_UPADE_NROU03",
"upty": "NOUS",
"isnc": false,
"erpm": 0
}
},
58, < - This line will be edited
{
"upma": "id",
"upnn": 0,
"upfb": true,
"upcu": {
"adid": "lphaSixoSSCrewcycled_016_tur1",
"ulid": "TT_RADE_TUO02",
"upty": "BO",
"uplv": 1,
"erpm": 0,
"adtc": [
{
"kfti": 1000.0,
"kfva": 0.321
}
]
}
},
70 < - This line will be edited
],
Upvotes: 0
Reputation: 2961
The below function can be used to modify lines containing only whitespace numbers and ',' indicates a manditory argument, argument positions are fixed.
It uses a flag to mark line as being a number only - a For /f loop with delims overides the flag if the line is not empty after removing those delims.
@Echo off
:ReplaceNum <inputfilepath.ext> <outputfilepath.ext>
Set "ScriptName=%~0"
For /f "tokens=1,2* Delims=:" %%G in (":.%ScriptName%")Do If "%%G"=="." Set "ScriptName=Call :%%H"
If "%~1"=="" (
Echo(%ScriptName% ^<inputfilepath.ext^> ^<outputfilepath.ext^>
Exit /B 0
)
%= Error 1; No Source =% If not Exist "%~1" Exit /B 1
%= Error 2; Filepaths Same =% If "%~1"=="%~2" Exit /B 2
Set "Class="
1>"%~2" (
For /f "usebackq delims=" %%G in ("%~1")Do (
Set "Line=%%G"
Setlocal EnableDelayedExpansion
Set "isnum=true"
For /f "delims=0123456789, " %%i in ("!Line!")Do Set "isnum=false"
If "!isnum!"=="true" (
Set "nLine=!Line: =!"
Set "nLine=!nLine:,=!"
Mode 120,40
1> Con Call :GetNum Replace number:'!nLine!' for Class: !Class!
For /f "tokens=1,2" %%i in ("!nLine! !Num!")Do Echo(!Line:%%i=%%j!
Set "Class="
)Else (
Set ^"cLine=!Line:"=!"
For /f "tokens=1,2 delims=:," %%u in ("!Line!")Do If not "%%~b"=="" Set "Class=!Class! %%u"
For %%c in ("{" "}" "[" "]") Do Set "Class=!Class:%%~c=!"
For /L %%i in (1 1 4)Do Set "Class=!Class: = !"
Echo(!Line!
)
For /F "Delims=" %%c in ("!Class!")Do Endlocal & Set "Class=%%c"
)
)
Exit /B 0
:GetNum
( Set LF=^
%= Linefeed Definition - Do not modify =%)
Set "Num="
Set /P "Num=%*!LF!>:"
2> nul Set /A "Num+=0","1/Num" || Goto:GetNum
Exit /B 0
Upvotes: 0