How to cut some blocks off the entire file then save into separated file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

How to cut some blocks off the entire file then save into separated file

#1 Post by goodywp » 21 Nov 2018 16:33

I have a task to cut off some blocks of entire file (Load_all.M83) then save it into a separated file (Load_NAR.M83).
Here is the outline of this entire file structure, it is a catalog file

;---- Groupe signed binaries
-epackage
;---- Component: EasyPath to Cless
;---- Catalog: Entry Point 0200000.Mxx
9992044568020000_APPCLEP.P3A
9992044568020000_APPCLEP.P3P
9992044690020000_LIBCLEPPX.P3L
9992044691020000_LIBCLEPRNGPX.P3L
9992044689000500_LIBEMVRNG.P3L
8445670405_EmvCoChecksum.P3L

;---- Catalog: JCB JSpeedy 0201.MXX
8445600201_JCB.P3A
9992047269020400_libJCB.P3L

........ (here all bunch of files with similar structure as above)
........
........

;---- NAR signed binaries
-epackage
;---- Component: Tetra Admin Resources 0001
;---- Catalog: TA_resources.mxx
8520440000_852044.P3P

;---- Component: Tetra Admin Proxy Lib 0001
;---- Catalog: TAProxylib_app.mxx
8400000100000000_libTAProxy.P3L

;---- Component: NAR TSA Tetra 0521
;---- Catalog: 8295010521_TSA.MXX
8295010521_TSA.P3A

;---- Component: NAR KIA Tetra 0521
;---- Catalog: 8295000521_KIA.MXX
8295000521_KIA.P3A

;---- Component: ORKI App 1.0.0.0020
;---- Catalog: orki_app.mxx
8520010002_852001.P3A

;---- Component: Tetra Admin App 0003
;---- Catalog: TA_app.mxx
8400000100000300_TetraAdmin.P3A

;---- Groupe data files
-eimport
device.txt
;---- NAR data files
-eimport
;---- Component: File COMM.INI
;---- Catalog: comm_ini.mxx
COMM.INI

Now I would like to cut off the block all the strings before the below line, in other words, save the string starting from the below lines
;---- NAR signed binaries
and also need to exclude these strings, sometimes it can have more lines in this block
;---- Groupe data files
-eimport
device txt

then save to a separate file as Load_NAR.M83


I also attached these two files as samples
Attachments
sample.7z
(3.22 KiB) Downloaded 433 times
Last edited by goodywp on 14 Sep 2019 04:57, edited 2 times in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to cut off some blocks off the entire file then save into separated file

#2 Post by Ed Dyreen » 21 Nov 2018 17:09

cutoff where ? at a specified byte position or based upon the occurrence of a string ?

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#3 Post by goodywp » 22 Nov 2018 05:38

Ed Dyreen wrote:
21 Nov 2018 17:09
cutoff where ? at a specified byte position or based upon the occurrence of a string ?
based upon the occurrence of a string.
cut off all the strings before this line, in other words to save the strings starting from below line

;---- NAR signed binaries
to the end.
but also need to cut off unwanted string (in this block) as
;---- Groupe data files
-eimport

Thanks

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to cut off some blocks off the entire file then save into separated file

#4 Post by Ed Dyreen » 22 Nov 2018 16:42

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$fileIn=Load_all.M83"
set "$fileOu=Load_NAR.M83"

> "!$fileOu!" type nul

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^) 2>nul' ) do set "$NumberOfLines=%%?"

< "!$fileIn!" (

	set /A $at = 0 &for /L %%i in ( 1, 1, !$NumberOfLines! ) do (

		set "$=" &set /P "$="
		
		if defined $ if "!$:;---- NAR signed binaries=!" NEQ "!$!" set /A $at = 1
		
		if !$at! NEQ 0 (

			set /A $filter = 0
			
			if defined $ if "!$:;---- Groupe data files=!" NEQ "!$!" set /A $filter = 1
			
			if !$filter! EQU 0 >>"!$fileOu!" (echo(!$!)
		)
	)
)
Just out of my head, this may work.

Hope there are no exclamation marks and carets in that file because I am not protecting any tokens that are problematic.

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#5 Post by goodywp » 23 Nov 2018 08:26

Ed Dyreen wrote:
22 Nov 2018 16:42

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$fileIn=Load_all.M83"
set "$fileOu=Load_NAR.M83"

> "!$fileOu!" type nul

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^) 2>nul' ) do set "$NumberOfLines=%%?"

< "!$fileIn!" (

	set /A $at = 0 &for /L %%i in ( 1, 1, !$NumberOfLines! ) do (

		set "$=" &set /P "$="
		
		if defined $ if "!$:;---- NAR signed binaries=!" NEQ "!$!" set /A $at = 1
		
		if !$at! NEQ 0 (

			set /A $filter = 0
			
			if defined $ if "!$:;---- Groupe data files=!" NEQ "!$!" set /A $filter = 1
			
			if !$filter! EQU 0 >>"!$fileOu!" (echo(!$!)
		)
	)
)
Just out of my head, this may work.

Hope there are no exclamation marks and carets in that file because I am not protecting any tokens that are problematic.
Thanks! ED! tried and got
2> was unexpected at this time. It was part of code

Code: Select all

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^) 2>nul' ) do set "$NumberOfLines=%%?"
When I took it off, it works but have some extra lines before the String

Code: Select all

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^)' ) do set "$NumberOfLines=%%?"
;---- NAR signed binaries

And there are some cut off towards end, meaning less than expected..
Thanks for the effort!

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to cut off some blocks off the entire file then save into separated file

#6 Post by Ed Dyreen » 23 Nov 2018 08:39

goodywp wrote:
23 Nov 2018 08:26
Thanks! ED! tried and got
2> was unexpected at this time. It was part of code

Code: Select all

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^) 2>nul' ) do set "$NumberOfLines=%%?"
When I took it off, it works but have some extra lines before the String
my mistake, 2>nul should be 2^>nul because it's in a for. You need it because otherwise zero length files will be miscounted as length 1 where it should 0. That is find will echo a message on stream 2 also known as stderr, 2>nul means redirect stream 2 to device nul.
goodywp wrote:
23 Nov 2018 08:26
And there are some cut off towards end, meaning less than expected..
Thanks for the effort!
if you can't solve it post an example so we can figure out what you expect and what is happening.

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#7 Post by goodywp » 23 Nov 2018 09:14

Ed Dyreen wrote:
23 Nov 2018 08:39
goodywp wrote:
23 Nov 2018 08:26
Thanks! ED! tried and got
2> was unexpected at this time. It was part of code

Code: Select all

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^) 2>nul' ) do set "$NumberOfLines=%%?"
When I took it off, it works but have some extra lines before the String
my mistake, 2>nul should be 2^>nul because it's in a for. You need it because otherwise zero length files will be miscounted as length 1 where it should 0. That is find will echo a message on stream 2 also known as stderr, 2>nul means redirect stream 2 to device nul.
goodywp wrote:
23 Nov 2018 08:26
And there are some cut off towards end, meaning less than expected..
Thanks for the effort!
if you can't solve it post an example so we can figure out what you expect and what is happening.
Tried as you suggested and similar to my previous trying.
Got some extra line before the start string and end up earlier, meaning cut off some wanted strings. I attached the results of this try
as below quote
P

;---- Catalog: ALL_TERMINALS_QATCHER.MXX
9991047171035902_KERNEL_MOD_QATCHER.P3S
9992048009160530_OPT_QATCH.P3P

;---- NAR signed binaries
-epackage
;---- Component: Tetra Admin Resources 0001
;---- Catalog: TA_resources.mxx
8520440000_852044.P3P

;---- Component: Tetra Admin Proxy Lib 0001
;---- Catalog: TAProxylib_app.mxx
8400000100000000_libTAProxy.P3L

;---- Component: NAR TSA Tetra 0521
;---- Catalog: 8295010521_TSA.MXX
8295010521_TSA.P3A

;---- Component: NAR KIA Tetra 0521
;---- Catalog: 8295000521_KIA.MXX
8295000521_KIA.P3A

;---- Component: ORKI App 1.0.0.0020
;---- Catalog: orki_app.mxx
8520010002_852001.P3A

;---- Component: Tetra Admin App 0003
;---- Catalog: TA_app.mxx
8400000100000300_TetraAdmin.P3A

;---- Component: Tetra Admin Skins 0001
;---- Catalog: TA_skins.mxx
8520830001_SKIN_SK480_320.P3P

;---- Component: NAR Apps Signed Resources
;---- Catalog: NarAppsSignedResources.mxx
8520460101_852046.P3P

;---- Catalog: NarAppsSignedResources.mxx
8295490203_829549.P3P
8296250000_829625.P3P
844
.
.
.
.
from above these are extra
P

;---- Catalog: ALL_TERMINALS_QATCHER.MXX
9991047171035902_KERNEL_MOD_QATCHER.P3S
9992048009160530_OPT_QATCH.P3P
and these are missing from 844 and afterwards
;---- Catalog: NarAppsSignedResources.mxx
8295490203_829549.P3P
8296250000_829625.P3P
844
.
.
.
.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to cut off some blocks off the entire file then save into separated file

#8 Post by Ed Dyreen » 23 Nov 2018 11:10

;---- NAR signed binaries
-epackage
;---- Component: Tetra Admin Resources 0001
;---- Catalog: TA_resources.mxx
8520440000_852044.P3P

;---- Component: Tetra Admin Proxy Lib 0001
;---- Catalog: TAProxylib_app.mxx
8400000100000000_libTAProxy.P3L

;---- Component: NAR TSA Tetra 0521
;---- Catalog: 8295010521_TSA.MXX
8295010521_TSA.P3A

;---- Component: NAR KIA Tetra 0521
;---- Catalog: 8295000521_KIA.MXX
8295000521_KIA.P3A

;---- Component: ORKI App 1.0.0.0020
;---- Catalog: orki_app.mxx
8520010002_852001.P3A

;---- Component: Tetra Admin App 0003
;---- Catalog: TA_app.mxx
8400000100000300_TetraAdmin.P3A

-eimport
device.txt
;---- NAR data files
-eimport
;---- Component: File COMM.INI
;---- Catalog: comm_ini.mxx
COMM.INI
I don't see anything wrong, this is what I get when running your input file from the first post. Starting where you said it should and stripping of the line ';---- Groupe data files'. Or do all consecutive lines after the line ';---- Groupe data files' need to be stripped of also until the next line starting with a ";" ?

using the fixed version

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$fileIn=Load_all.M83"
set "$fileOu=Load_NAR.M83"

> "!$fileOu!" type nul

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^) 2^>nul' ) do set "$NumberOfLines=%%?"

< "!$fileIn!" (

	set /A $at = 0 &for /L %%i in ( 1, 1, !$NumberOfLines! ) do (

		set "$=" &set /P "$="
		
		if defined $ if "!$:;---- NAR signed binaries=!" NEQ "!$!" set /A $at = 1
		
		if !$at! NEQ 0 (

			set /A $filter = 0
			
			if defined $ if "!$:;---- Groupe data files=!" NEQ "!$!" set /A $filter = 1
			
			if !$filter! EQU 0 >>"!$fileOu!" (echo(!$!)
		)
	)
)

exit /b

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#9 Post by goodywp » 23 Nov 2018 13:17

Ed Dyreen wrote:
23 Nov 2018 11:10
;---- NAR signed binaries
-epackage
;---- Component: Tetra Admin Resources 0001
;---- Catalog: TA_resources.mxx
8520440000_852044.P3P

;---- Component: Tetra Admin Proxy Lib 0001
;---- Catalog: TAProxylib_app.mxx
8400000100000000_libTAProxy.P3L

;---- Component: NAR TSA Tetra 0521
;---- Catalog: 8295010521_TSA.MXX
8295010521_TSA.P3A

;---- Component: NAR KIA Tetra 0521
;---- Catalog: 8295000521_KIA.MXX
8295000521_KIA.P3A

;---- Component: ORKI App 1.0.0.0020
;---- Catalog: orki_app.mxx
8520010002_852001.P3A

;---- Component: Tetra Admin App 0003
;---- Catalog: TA_app.mxx
8400000100000300_TetraAdmin.P3A

-eimport
device.txt
;---- NAR data files
-eimport
;---- Component: File COMM.INI
;---- Catalog: comm_ini.mxx
COMM.INI
I don't see anything wrong, this is what I get when running your input file from the first post. Starting where you said it should and stripping of the line ';---- Groupe data files'. Or do all consecutive lines after the line ';---- Groupe data files' need to be stripped of also until the next line starting with a ";" ?

using the fixed version

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$fileIn=Load_all.M83"
set "$fileOu=Load_NAR.M83"

> "!$fileOu!" type nul

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^) 2^>nul' ) do set "$NumberOfLines=%%?"

< "!$fileIn!" (

	set /A $at = 0 &for /L %%i in ( 1, 1, !$NumberOfLines! ) do (

		set "$=" &set /P "$="
		
		if defined $ if "!$:;---- NAR signed binaries=!" NEQ "!$!" set /A $at = 1
		
		if !$at! NEQ 0 (

			set /A $filter = 0
			
			if defined $ if "!$:;---- Groupe data files=!" NEQ "!$!" set /A $filter = 1
			
			if !$filter! EQU 0 >>"!$fileOu!" (echo(!$!)
		)
	)
)

exit /b
Very strange, I tried this original input file again using the latest code of yours
got results as below:
10900_FOALMONO_TTF_IT.P3P
9990044261010900_FOALMONO_TTF_BO.P3P
9990044260010900_FOALMONO_TTF_NO.P3P

;---- Catalog: ALL_TERMINALS_PCL_SPMCI.MXX
9990044625050500_LIBPCL.P3L
9990044625050500_LIBPCL.P3P
9990044627050500_PCLVIEW.P3L
9990044626050500_LIBSPMCI.P3L
9990044626050500_LIBSPMCI.P3P
0002046010050500_SPMCIWR.P3A
9992048008160530_OPT_PCL.P3P

;---- Catalog: ALL_TERMINALS_QATCHER.MXX
9991047171035902_KERNEL_MOD_QATCHER.P3S
9992048009160530_OPT_QATCH.P3P

;---- NAR signed binaries
-epackage
;---- Component: Tetra Admin Resources 0001
;---- Catalog: TA_resources.mxx
8520440000_852044.P3P

;---- Component: Tetra Admin Proxy Lib 0001
;---- Catalog: TAProxylib_app.mxx
8400000100000000_libTAProxy.P3L

;---- Component: NAR TSA Tetra 0521
;---- Catalog: 8295010521_TSA.MXX
8295010521_TSA.P3A

;---- Component: NAR KIA Tetra 0521
;---- Catalog: 8295000521_KIA.MXX
8295000521_KIA.P3A

;---- Component: ORKI App 1.0.0.0020
;---- Catalog: orki_app.mxx
8520010002_852001.P3A

;---- Component: Tetra Admin App 0003
;---- Cata
oftware.tmscall.PBT

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#10 Post by goodywp » 23 Nov 2018 13:36

I also used this site library function as below:

Code: Select all

	
@ECHO OFF

REM.-- Prepare the Command Processor

SETLOCAL ENABLEEXTENSIONS

SETLOCAL ENABLEDELAYEDEXPANSION

 
echo.Extracting hello world block into "Load_NAR.M83" file

call:extractFromFile ";---- NAR signed binaries" "DEVICE_ID.TXT">"Load_NAR.M83"

::------------------------------------------------------------------

::-- functions start below here

::------------------------------------------------------------------
  

:extractFromFile - extract lines from a file between begin and end mark

:: - %~1: begin mark, use '...$' mark to allow variable substitution

:: - %~2: optional end mark, default is end of file

:: - %~3: optional source file, default is THIS file

SETLOCAL

set bmk=;---- NAR signed binaries

set emk=DEVICE_ID.TXT

set src=Load_all.M83

set /a b=-1

set /a e=-1

if "%src%"=="" set src=%~f0& ::- if no source file then assume THIS file

for /f "tokens=1,* delims=:" %%a in ('"findstr /n /b /c:"%bmk%" "%~f0""') do (

set b=%%a

set bmk=%%b

)

if /i %b%==-1 echo.ERROR: begin mark '%bmk%' not found in '%src%'&GOTO:EOF

if "%emk%"=="" (set /a e=2000000000) ELSE (

for /f "delims=:" %%a in ('"findstr /n /b /c:"%emk%" "%~f0""') do (

if /i %b% LSS %%a if /i !e!==-1 set e=%%a& rem -- find only the first one after b

)

)

if /i %e%==-1 echo.ERROR: end mark '%emk%' missing in '%src%'&GOTO:EOF

if /i %b% GEQ %e% echo.ERROR: end mark '%emk%' detected before begin mark '%bmk%' in '%src%'&GOTO:EOF

for /f "delims=: tokens=1,*" %%a in ('"findstr /v /n /b /c:"#$*ReturnAll*$#" "%src%""') do (

if /i %b% LSS %%a if /i %%a LSS %e% (

if "%bmk:~-1%"=="$" (

rem --substitution variables within %%b

call echo.%%b

) ELSE (

rem --no variable substitution

echo.%%b

)

)

)

GOTO:EOF
I tried the above code to see if I can get anything, but not working, any reason about the set variable for bmk and src ???

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to cut off some blocks off the entire file then save into separated file

#11 Post by Ed Dyreen » 23 Nov 2018 14:32

I downloaded your sample, and I notice the file is using LF as line terminator but dos expects CRLF. So the lines are never terminated and cause the variable to overflow as it can only hold 8kb and your input is 12kb which I believe explains the data getting lost. Then this 8kb is treated as a single line because DOS doesn't use LF as terminator.

If I convert the LF terminators to CRLF it seems ok.

I want you to create a file LFtoCRLF.CMD. In there put this

Code: Select all

@echo off &setlocal disableDelayedExpansion &title %~n0

set "$iFile=%~1"
set "$oFile=%~dp0%~nx1"

echo.¯ '%$iFile%'
type "%$iFile%" &echo.
echo.® '%$iFile%'

more "%$iFile%" > "%$oFile%"
echo.

echo.¯ '%$oFile%'
type "%$oFile%" &echo.
echo.® '%$oFile%'
Now backup your original input and drag and drop it onto LFtoCRLF.CMD. Now try this converted file as your input.

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#12 Post by goodywp » 23 Nov 2018 15:57

Ed Dyreen wrote:
23 Nov 2018 14:32
I downloaded your sample, and I notice the file is using LF as line terminator but dos expects CRLF. So the lines are never terminated and cause the variable to overflow as it can only hold 8kb and your input is 12kb which I believe explains the data getting lost. Then this 8kb is treated as a single line because DOS doesn't use LF as terminator.

If I convert the LF terminators to CRLF it seems ok.

I want you to create a file LFtoCRLF.CMD. In there put this

Code: Select all

@echo off &setlocal disableDelayedExpansion &title %~n0

set "$iFile=%~1"
set "$oFile=%~dp0%~nx1"

echo.¯ '%$iFile%'
type "%$iFile%" &echo.
echo.® '%$iFile%'

more "%$iFile%" > "%$oFile%"
echo.

echo.¯ '%$oFile%'
type "%$oFile%" &echo.
echo.® '%$oFile%'
Now backup your original input and drag and drop it onto LFtoCRLF.CMD. Now try this converted file as your input.
Thanks so much ED! I would like to know if there is a better way to do the convert. BTW, I was trying to drag and drop and looks like not working, please a little bit details

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#13 Post by goodywp » 23 Nov 2018 16:12

goodywp wrote:
23 Nov 2018 15:57
Ed Dyreen wrote:
23 Nov 2018 14:32
I downloaded your sample, and I notice the file is using LF as line terminator but dos expects CRLF. So the lines are never terminated and cause the variable to overflow as it can only hold 8kb and your input is 12kb which I believe explains the data getting lost. Then this 8kb is treated as a single line because DOS doesn't use LF as terminator.

If I convert the LF terminators to CRLF it seems ok.

I want you to create a file LFtoCRLF.CMD. In there put this

Code: Select all

@echo off &setlocal disableDelayedExpansion &title %~n0

set "$iFile=%~1"
set "$oFile=%~dp0%~nx1"

echo.¯ '%$iFile%'
type "%$iFile%" &echo.
echo.® '%$iFile%'

more "%$iFile%" > "%$oFile%"
echo.

echo.¯ '%$oFile%'
type "%$oFile%" &echo.
echo.® '%$oFile%'
Now backup your original input and drag and drop it onto LFtoCRLF.CMD. Now try this converted file as your input.
Thanks so much ED! I would like to know if there is a better way to do the convert. BTW, I was trying to drag and drop and looks like not working, please a little bit details
I used this piece code to do the convert and works.

Code: Select all

@echo off &setlocal disableDelayedExpansion

set "$iFile=Load_old.M72"
set "$oFile=Load_all.M72"

(for /f ^"usebackqeol^=^

delims^=^" %%? in (

       "%$iFile%"

) do   (echo(%%?)

)> "%$oFile%"

echo.
echo.'%$oFile%'
type "%$oFile%"

pause
exit
and it works pretty well! Thanks again!

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: How to cut off some blocks off the entire file then save into separated file

#14 Post by goodywp » 23 Nov 2018 16:27

Ed Dyreen wrote:
23 Nov 2018 11:10
;---- NAR signed binaries
-epackage
;---- Component: Tetra Admin Resources 0001
;---- Catalog: TA_resources.mxx
8520440000_852044.P3P

;---- Component: Tetra Admin Proxy Lib 0001
;---- Catalog: TAProxylib_app.mxx
8400000100000000_libTAProxy.P3L

;---- Component: NAR TSA Tetra 0521
;---- Catalog: 8295010521_TSA.MXX
8295010521_TSA.P3A

;---- Component: NAR KIA Tetra 0521
;---- Catalog: 8295000521_KIA.MXX
8295000521_KIA.P3A

;---- Component: ORKI App 1.0.0.0020
;---- Catalog: orki_app.mxx
8520010002_852001.P3A

;---- Component: Tetra Admin App 0003
;---- Catalog: TA_app.mxx
8400000100000300_TetraAdmin.P3A

-eimport
device.txt
;---- NAR data files
-eimport
;---- Component: File COMM.INI
;---- Catalog: comm_ini.mxx
COMM.INI
I don't see anything wrong, this is what I get when running your input file from the first post. Starting where you said it should and stripping of the line ';---- Groupe data files'. Or do all consecutive lines after the line ';---- Groupe data files' need to be stripped of also until the next line starting with a ";" ?

using the fixed version

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$fileIn=Load_all.M83"
set "$fileOu=Load_NAR.M83"

> "!$fileOu!" type nul

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^) 2^>nul' ) do set "$NumberOfLines=%%?"

< "!$fileIn!" (

	set /A $at = 0 &for /L %%i in ( 1, 1, !$NumberOfLines! ) do (

		set "$=" &set /P "$="
		
		if defined $ if "!$:;---- NAR signed binaries=!" NEQ "!$!" set /A $at = 1
		
		if !$at! NEQ 0 (

			set /A $filter = 0
			
			if defined $ if "!$:;---- Groupe data files=!" NEQ "!$!" set /A $filter = 1
			
			if !$filter! EQU 0 >>"!$fileOu!" (echo(!$!)
		)
	)
)

exit /b
Hi ED,
just realized that I also need to cut off these lines
;---- Component: NAR KIA Tetra 0521
;---- Catalog: 8295000521_KIA.MXX
8295000521_KIA.P3A

I tried add these 3 lines in your code and works. Only thing is these files can change in future in terms of version

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$fileIn=Load_all.M72"
set "$fileOu=Load_NAR.M72"

> "!$fileOu!" type nul

for /F %%? in ( '^( find /V /C"" ^< "!$fileIn!" ^) 2^>nul' ) do set "$NumberOfLines=%%?"

< "!$fileIn!" (

	set /A $at = 0 &for /L %%i in ( 1, 1, !$NumberOfLines! ) do (

		set "$=" &set /P "$="
		
		if defined $ if "!$:;---- NAR signed binaries=!" NEQ "!$!" set /A $at = 1
		
		if !$at! NEQ 0 (

			set /A $filter = 0
			
			if defined $ if "!$:;---- Groupe data files=!" NEQ "!$!" set /A $filter = 1

			if defined $ if "!$:;---- Component: NAR KIA Tetra 0521=!" NEQ "!$!" set /A $filter = 1
			
			if defined $ if "!$:;---- Catalog: 8295000521_KIA.MXX=!" NEQ "!$!" set /A $filter = 1
			
			if defined $ if "!$:8295000521_KIA.P3A=!" NEQ "!$!" set /A $filter = 1

			if !$filter! EQU 0 >>"!$fileOu!" (echo(!$!)
		)
	)
)

exit /b
Is any better way to do it instead of hardcoded

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to cut off some blocks off the entire file then save into separated file

#15 Post by Ed Dyreen » 23 Nov 2018 16:49

goodywp wrote:
23 Nov 2018 15:57
Ed Dyreen wrote:
23 Nov 2018 14:32
I downloaded your sample, and I notice the file is using LF as line terminator but dos expects CRLF. So the lines are never terminated and cause the variable to overflow as it can only hold 8kb and your input is 12kb which I believe explains the data getting lost. Then this 8kb is treated as a single line because DOS doesn't use LF as terminator.

If I convert the LF terminators to CRLF it seems ok.

I want you to create a file LFtoCRLF.CMD. In there put this

Code: Select all

@echo off &setlocal disableDelayedExpansion &title %~n0

set "$iFile=%~1"
set "$oFile=%~dp0%~nx1"

echo.¯ '%$iFile%'
type "%$iFile%" &echo.
echo.® '%$iFile%'

more "%$iFile%" > "%$oFile%"
echo.

echo.¯ '%$oFile%'
type "%$oFile%" &echo.
echo.® '%$oFile%'
Now backup your original input and drag and drop it onto LFtoCRLF.CMD. Now try this converted file as your input.
Thanks so much ED! I would like to know if there is a better way to do the convert. BTW, I was trying to drag and drop and looks like not working, please a little bit details
The output file will have the same name as the input file and will be placed in the same directory as LFtoCRLF.CMD the input file should be in a different folder or more will be using the same input as output file which doesn't work. If you want the output filename to be different from the input filename so this doesn't happen you can change set "$oFile=%~dp0%~nx1" to set "$oFile=%~dp0_%~nx1" for example.

The conversion is done by this single line more "%$iFile%" > "%$oFile%" and should work.

Post Reply