How can i get this text from multiple line in batch file - batch-file

I have a text file (Myfile.txt), line by line, which is very long and centered as shown below
...","ItemPrice":17000.0,"MustPay":17000.0,"PaywithCash":17000.0,"etc...
...","ItemPrice":900.0,"MustPay":900.0,"PaywithCash":900.0,"etc...
...","ItemPrice":1400.0,"MustPay":1400.0,"PaywithCash":1400.0,"etc...
so I want to get the number after the word "PayWithCash":, for example the first line is the number 17000 and so on to the next line, and save it to a new text file "result.txt"
1700
900
1400
I have tried several codes, including as below
echo off
SETLOCAL EnableDelayedExpansion
for /f "delims=" %%a in ('type Myfile.txt^|find "PayWithCash:"') do (
set "line=%%a"
set "line=!line:*PayWithCash =!
set /a "last=!line:~1!" 2>nul
)
echo %last% >> result.txt
And yes, I still can't get the desired result, can you help me?
I use google translate, I hope you understand

Correct PayWithCash to the correct case in the data, PaywithCash or add the /i switch to find to make the match case-insensitive.
Remove the colon as suggested by #Compo.
Remove the Space after PayWithCash in the set statement as there is no such Space in the data.
Since the result in line will now begin ": you need to use line:~2
Test code I used:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q64838297.txt"
SET "outfile=%destdir%\outfile.txt"
(
for /f "delims=" %%a in ('type "%filename1%"^|find /i "PayWithCash"') do (
set "line=%%a"
set "line=!line:*PayWithCash=!"
set /a "last=!line:~2!" 2>nul
echo !last!
)
)>"%outfile%"
TYPE "%outfile%"
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances. The listing uses a setting that suits my system.
I used a file named q64838297.txt containing your data for my testing.
Produces the file defined as %outfile%
Result:
17000
900
1400

Related

Batch removing X number of lines from file from Nth line

I am trying to remove a tag from a set of html files and I can't seem to find the right commands to do the job.
I am able to find the line number of the tag; the tag is a total of 10 lines that need to be deleted. Once I find this line, how do I got and delete said line & next 10 lines?
Here's what I have (a first for loop to collect files, a second for loop that collects all line numbers.) thank you.
::start by creating a list of html files
set i=0
for /r %%G in (*.html) do (
set /A i+=1
set array[!i!]=%%G
)
set n=%i%
::second nested loop collects all lines for bottom secion to be deleted.
set j = 0
for /L %%i in (1,1,%n%) do (
for /f "delims=" %%a in ('findstr /n /c:"u-backlink u-clearfix u-grey 80" !array[%%i]!') do (
set var=%%a
set /A j+=1
set array[!j!]=!var:~0,3!
)
)
set m=%j%
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
for /r "%sourcedir%" %%G in (*.html) do (
SET "linecount="
FOR /f "delims=:" %%e IN ('findstr /n /c:"u-backlink u-clearfix u-grey 80" "%%G"') DO SET /a linecount=%%e
IF DEFINED linecount (
FOR /f "usebackqdelims=" %%b IN ("%%G") DO (
SET /a linecount-=1
IF !linecount! gtr 0 ECHO %%b
IF !linecount! lss -10 ECHO %%b
)
)>"%destdir%\tempfile"& MOVE /y "%destdir%\tempfile" "%%G" >nul
)
GOTO :EOF
Start a recursive directory list of the target files from sourcedir.
Initialise linecount to nothing and use findstr to locate the target string. %%e will be set to the number of the line found, set this into linecount.
If the string was found, linecount will now contain a value, so read each line and count down. If linecount is between 0 and -10, we don't echo the line to the output file.
I originally put all the generated files in the one directory. Yours to change at will.
Revised to generate a temporary file (quite where would be irrelevant) and then move that file over the original.
Always verify against a test directory before applying to real data.

Is there a way to use findstr to search for a value and return the line that value is on within a text file?

I want to find a value within a text file that has 120+ lines of values, then set the line that the value was found on as a variable so i can go back and replace the value in the text file with another value.
Using this line
for /F "delims=" %%a in ('findstr /c:/-59/12/ ScreenData.txt') do set
var=%%a
i can find the value (coordinate) /-59/12/ within the text (ScreenData.txt) and then assign it to the variable (var).
I can then echo %var% and get the output /-59/12/, but there is no way to then replace /-59/12/ with (the letter H for example).
I have tried...
for %%g in (findstr /n "/-58/12/" ScreenData.txt) do set var=%%g
the text doc has /-58/12/ as the 2nd line, however the output was ScreenData.txt not 2.
This is the code leading up to the replacement...
REM --- ROW a ---
SET /A rowa1x=%xaxis%-59
SET /A rowa1y=%yaxis%+12
SET rowa1=/%rowa1x%/%rowa1y%/
SET /A rowa2x=%xaxis%-58
SET /A rowa2y=%yaxis%+12
SET rowa2=/%rowa2x%/%rowa2y%/
SET /A rowa60x=%xaxis%
SET /A rowa60y=%yaxis%+12
SET rowa60=/%rowa60x%/%rowa60y%/
REM --- Export Values ---
(
echo %rowa1%
echo %rowa2%
echo %rowa60%
) >ScreenData.txt ---
pause
REM --- View Generator
for %%g in (findstr /n "/-58/12/" ScreenData.txt) do set var=%%g
echo %var%
pause
I'm sure i'm just going about this in the wrong way.
so the logic is...
find a value equal to /-58/12/ in the text file.
what line is it on?
it is on the 3rd line.
replace the 3rd line with "H".
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q55875551.txt"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "delims=" %%a IN ('type "%filename1%"') DO (
IF "%%a"=="/-59/12/" (
ECHO H
) ELSE (
ECHO %%a
)
)
)>"%outfile%"
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used a file named q55875551.txt containing some dummy data for my testing.
Produces the file defined as %outfile%
Empty data lines will be removed.
This simply changes any line which is exactly "/-59/12/" to "H". You would need to provide examples of other transformations you might want.
The layout I have used is deliberately verbose for versatility.

Batch column search / replace user input

I need to get a batch file running, which helps our employees to change text files without trouble.
As you can see, there are a lot of whit space in this file and they need to be there, otherwise it could not be imported in other programs.
My question: Is it possible to search for a specific column and replace this value with an user input?
I've tried to get something done with help of google and this was the result:
#echo off
for /f "skip=5 delims=" %%a in (Muster.dat) do set "var=%%a"&goto :done
:done
set "R_sph=%var:~126,5%"
echo R_sph: %R_sph%
set /p R_sph_new=Enter new sph:
echo R_sph neu: %R_sph_new%
set "search=R_sph"
set "replace=R_sph_new"
set "textfile=Muster.dat"
set "newfile=Muster2.dat"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
del %textfile%
rename %newfile% %textfile%
pause
PS: My batch experience is worse and it would be great to explain every step in the code you send me.
Edit: We generate a lot of files like this every day and only some of them need so be edited. The construction of this files is always the same but with other values so it's important so search via column.
Change the main loop in your batch file so that:
it only changes the 6th line;
it only changes the 5 characters from position 126 to 130.
And just to make the batch script more robust, you can also validate user input to ensure that they've entered exactly 5 characters as the replacement string.
Here's the whole file with suggested changes:
#echo off
setlocal enabledelayedexpansion
for /f "skip=5 delims=" %%a in (Muster.dat) do set "var=%%a"&goto :done
:done
set "R_sph=%var:~126,5%"
echo R_sph: %R_sph%
set /p R_sph_new=Enter new sph:
echo R_sph neu: %R_sph_new%
rem Validate user input - ensure it's exactly 5 characters
set "replace=%R_sph_new% "
set "replace=%replace:~0,5%"
if not "%R_sph_new%" == "%replace%" (
echo Don't be silly.
exit /b 1
)
set "textfile=Muster.dat"
set "newfile=Muster2.dat"
set /a lineNumber = 0
(for /f "delims=" %%i in (%textfile%) do (
set /a lineNumber += 1
set "line=%%i"
if !lineNumber! == 6 set "line=!line:~0,126!%replace%!line:~131!"
echo(!line!
))>"%newfile%"
rem It might be safer to rename %textfile% to %textfile%.BAK instead of deleting it.
del %textfile%
rename %newfile% %textfile%
endlocal
pause
set "search=R_sph"
set "replace=R_sph_new"
should
set "search=%R_sph%"
set "replace=%R_sph_new%"
that is, set the values of search and replace to the contents of the variables. Your code sets search to the string R_sph.
? Are you sure that the value in R_sph will be unique in your file?
Thank you very much Klitos Kyriacou, that's exactly what I was looking for!
But unfortunately this batch does not change anything in this text file, the whole code looks like this:
#echo off
:R_sph
for /f "skip=5 delims=" %%a in (Muster.dat) do set "var=%%a"&goto :done
:done
set "R_sph=%var:~49,5%"
echo R_sph: %R_sph%
set /p R_sph_new=Enter new sph:
set "replace=%R_sph_new%"
set "textfile=Muster.dat"
set "newfile=Muster2.dat"
setlocal enabledelayedexpansion
set /a lineNumber = 0
(for /f "delims=" %%i in (%textfile%) do (
set /a lineNumber += 1
set "line=%%i"
if !lineNumber! == 6 set "line=!line:~0,49!%replace%!line:~54!"
echo(!line!
))>"%newfile%"
endlocal
:replace file
del %textfile%
rename %newfile% %textfile%
pause
Edit: I was in hurry and dind't get how to set it up. Now I changed the code and it's working!

Edit names of image database with batch files

I have a folder with 460 images with 23 per person in the format: image_0001.jpg to image_0460.jpg.
What is the batch command to rename them in the form 01-01.jpg to 01-23.jpg for a person and thus the entire database upto 20-23.jpg?
[EDIT]
I came across:
#echo off & setlocal EnableDelayedExpansion
set a=1
for /f "delims=" %%i in ('dir /b *') do (
if not "%%~nxi"=="%~nx0" (
ren "%%i" "!a!"
set /a a+=1
)
)
I could not find a way to use loop variables to do the same. Is there a way to use loop variables or is there another way?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET /a filenum=10000
FOR /L %%a IN (1,1,20) DO (
FOR /L %%b IN (1,1,23) DO (
SET /a filenum+=1
SET /a newnum=10000+%%b+(%%a*100^)
ECHO(REN "%sourcedir%\image_!filenum:~-4!.jpg" "!newnum:~1,2!-!newnum:~-2!".jpg
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
The issue is one of leading zeroes, hence invoke delayedexpansion and calculate using 10000+a significant number, then substring.
The rest is simply a matter of mathematics.
This method use the % (modulus or remainder) operator to count in groups of 23: the image=(image+1)%23 part vary image variable from 0 to 22 and repeat this count. imgaux=101+image vary imgaux from 101 to 123 and just the last two digits are used in the ren command. Finally, person+=!image increment person variable each time that image is zero.
#echo off
setlocal EnableDelayedExpansion
set /A person=100, image=-1
for %%a in (*.jpg) do (
set /A "image=(image+1)%%23, imgaux=101+image, person+=^!image"
ECHO ren "%%a" "!person:~1!-!imgaux:~1!.jpg"
)
Note that in this method the number of persons doesn't need to be known in advance.

Remove a number of delimiters from each line of file

I have a file which I need to load into a database. It has a delimiter of pipe (|) however each line contains different number of pipes. Using a batch script, how can I remove pipes from each line so the same number of pipes are on each line?
Example of file:
1|2|3||||||
4|5|6|||
7|8||||||
Let's say I'd like 5 pipes on each line only so it looks like:
1|2|3|||
4|5|6|||
7|8||||
Update See second solution and limitation updates.
Example file.txt contents
A|B|C|D|E|F|G
1|2|3|4|5|6|7
!|#|#|$|%|^|&
]1|]2|]3|]4|]5|]6|]7
|Two||Four||||Eight
!#$%^&%^*(){}|[]';/.,<>/|
Lonely||||||||||||||||||
Sep|er|ate| From| Th|e |W||orld | |
First Solution
Here is a simple way to do what you want. It should not have any problems with special characters.
Limitations
It only supports up to 24 25 columns as it is currently written. %%A to %%Y
The first value may not begin with ]. Replaced for /F "tokens=1,* delims=]" %%Y in ('type file.txt ^| find /v /n ""') do ( with for /F "delims=" %%Z in ('type file.txt') do (.
"Empty" fields may only appear at the end of every line. See second solution.
Does not preserve blank lines in the file. (This can be fixed if desired.)
Just specify how many and which columns you want to keep. For example tokens=3-5,12,48-50 will select only columns 3,4,5,12,48,49,50. Make sure you add on or remove the variables to match the output you want. echo %%A^|%%B^|%%D^|%%C^|%%G^|%%E^|%%F. Note that the columns can be reordered as well in the echo statement.
#echo off
setlocal DisableDelayedExpansion
for /F "delims=" %%Z in ('type file.txt') do (
for /F "tokens=1-5 delims=|" %%A in ("%%Z") do (
echo %%A^|%%B^|%%C^|%%D^|%%E
)
)
endlocal
pause >nul
You can either redirect the output of the .bat file into a new file Script.bat>output.txt or output the echo command to a file by appending >>output.txt to the echo line.
Example Output:
A|B|C|D|E
1|2|3|4|5
!|#|#|$|%
]1|]2|]3|]4|]5
Two|Four|Eight|| <-- Note that this line exhibits limit 3.
!#$%^&%^*(){}|[]';/.,<>/|||
Lonely||||
Sep|er|ate| From| Th
Second Solution
Shares only limitations 1 and 4. Currently adds spaces into existing blank columns to preserve all columns. They can be removed with a further code change, but will not add unless desired by the OP.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "delims=" %%Z in ('type file.txt') do (
set "xLine=|%%Z"
call :Parse xLine
)
endlocal
pause >nul
goto :eof
:Parse
call set "xLine=%%%~1:||=| |%%"
for /F "tokens=1-5 delims=|" %%A in ("%xLine%") do (
echo %%A^|%%B^|%%C^|%%D^|%%E
)
goto :eof
Example Output:
A|B|C|D|E
1|2|3|4|5
!|#|#|$|%
]1|]2|]3|]4|]5
|Two| |Four|
!#$%^&%^*(){}|[]';/.,<>/|||
Lonely| | | |
Sep|er|ate| From| Th
There is no direct way to achieve this process, so each character must be revised in order to count the number of pipes in each line. It works, but it is somewhat slow.
#echo off
setlocal EnableDelayedExpansion
rem Number of desired pipes
set limit=5
for /F "delims=" %%a in (input.txt) do (
set "line=%%a"
rem Get position of last character
set last=0
for /L %%b in (12,-1,0) do (
set /A "last|=1<<%%b"
for %%c in (!last!) do if "!line:~%%c,1!" equ "" set /A "last&=~1<<%%b"
)
rem Copy each character to result, but just %limit% number of pipes
set pipes=0
set result=
for /L %%c in (0,1,!last!) do (
if "!line:~%%c,1!" neq "|" (
set "result=!result!!line:~%%c,1!"
) else (
set /A pipes+=1
if !pipes! leq %limit% set "result=!result!|"
)
)
echo !result!
)
Previous program will fail if the input line contain exclamation marks.
Output:
1|2|3|||
4|5|6|||
7|8||||
Antonio

Resources