1.

Solve : Control Characters in File - Help?

Answer»

I'm trying to write a batch file to replace certain data in a file. Here is approximately what the data looks like.

This is source file (source.dat)
LXXX0220451 0337 BEN:QUALEX KS& N
LXXX0220451 0337 TOTAL#OFITEMSI N
LXXX0220451 0337 L-C DIVISION O N
LXXX0220451 0337 2642, DEPART 4 N
LXXX0220451 0337 BEN_ /& TOT$ N

I need to copy the data to another file, then in the in the copy - replace the XXX in positions 2-5 with ZZZ. Then merge the data together so I have a combined file with rows with both the XXX and ZZZ. I have that part working ok. But I'm having an issue with the special characters that can appear in positions 18 to 32, especially the & which is truncating the rest of the line in the rows where I'm replacing the XXX with the ZZZ.

Here is what I'm using for code. This isn't exact as the file is longer than what I have here and there is a header and a footer but I'm not having an issue with the header and footer rows.

set _INPUT_FILE=source.DAT
set _OUTPUT_FILE=WORK.dat

copy source.dat work.dat.

in Work.dat
set _DET_PRE_CODE=%_STRING:~1,1%
set _DET_CODE_OLD=%_STRING:~2,3%
set _DET_DESC_STR=%_STRING:~5,137%

set _DET_CODE_NEW=ZZZ

echo %_DET_PRE_CODE%%_DET_CODE_NEW%%_DET_DESC_STR% >> %_OUTPUT_FILE%


copy source.dat+work.dat

Another issue is that the final source.dat file has the following control character at the end which is causing an issue with the next step in the process that has to read the file. (Not windows process.) Is there an attribute to copy command that will not create this or another command that I can use to merge the two files together without having the character on the end. I know its happening on the copy because the work.dat file doesn't have it. It doens't appear until the two files are merged.


At the end, I end up with something that looks like this:

LXXX0220451 0337 BEN:QUALEX KS& N
LXXX0220451 0337 TOTAL#OFITEMSI N
LXXX0220451 0337 L-C DIVISION O N
LXXX0220451 0337 2642, DEPART 4 N
LXXX0220451 0337 BEN_ /& TOT$ N
LZZZ0220451 0337 BEN:QUALEX KS&
LZZZ0220451 0337 TOTAL#OFITEMSI N
LZZZ0220451 0337 L-C DIVISION O N
LZZZ0220451 0337 2642, DEPART 4 N
LZZZ0220451 0337 BEN_ /&
SOMEHOW you need to get a carriage-return line-feed character at the end of each line. Once I did that I got this code to work:

Code: [Select]@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%i in (source.DAT) do (
set input=%%i
set input=!input:XXX=ZZZ!
echo !input! >> WORK.dat
)
copy source.dat+work.dat output.dat


Quote

I need to copy the data to another file, then in the in the copy - replace the XXX in positions 2-5 with ZZZ. Then merge the data together so I have a combined file with rows with both the XXX and ZZZ.

Text files can not be updated in place. Better to do a search & replace against the original file and output the changes to a new file.

Quote
copy source.dat+work.dat

Seems like a incomplete statement. When I tried it, just got a copy of the changed file. You can always use the DEL command to cleanup any work files.

Quote
At the end, I end up with something that looks like this:

What did you use to view the file? And just out of curiosity, what program created this file?

Thanks for your response.

I tried the set input then found it could be possible to have the "X's in other places in the file besides position 2-5 so I can't do a global replace on every instance of "X's" only that in position 2-5. The statement that I have is working to just replace the "X's" with ZZZ in 2-5. The main issue is the rows with the &. It's truncating data after the & as its writing the line to the output file. The other lines are ok. I'm trying to feed this file into another application so I can't have the carriage-return-line-feed at the end of the line.

The copy statement that I'm using has a folder structure in it that specifies where the files are. I'm using XP - this is the statement: and its working ok. I just get the control character at the end after the copy and its not in either of the files prior to the copy so I know its the copy statement that is putting it there. Not sure if there is an option to add to the copy to AVOID - I couldn't find one.
copy c:\RESIQ2\source.dat+c:\RESIQ2\WORK.dat

The actual file that has this data can be hundreds of lines long which is why we're trying to automate. The manual search and replace is very prone to error.

The file was created out of a banking program - I'm not sure of the exact software.

I'm open to another option besides dos batch. It just can't require elaborate setup or installation of other software as this has to run on the users desktop.

Again thanks. If you can think of anything else, let me know.Quote
If you can think of anything else, let me know.

We can always think of something else.

If you want to consider VBScript, we could probably whip something up for you. Does each line have a carriage-return line-feed? It's easier if each line has a beginning and an end.

[mind drift]
Back in the day there was a VM editor called Xedit that could easily gang punch columns of data. It was ported to the PC as The (). It could be scripted with REXX and was a godsend when working with files.
[/mind drift]



you can use vbscript. or you can use gawk ( if you can) . download and install from here.
save the code below as script.awk
Code: [Select]{
print $0
gsub(/&|\$/,"") #remove & and $.
a[++c]=substr($0,1,1)"ZZZ"substr($0,5)

}
END {
for (i=1;i<=c;i++) {
print a[i]
}
}
on command line
Code: [Select]C:\test>more test.txt
LXXX0220451 0337 BEN:QUALEX KS& N
LXXX0220451 0337 TOTAL#OFITEMSI N
LXXX0220451 0337 L-C DIVISION O N
LXXX0220451 0337 2642, DEPART 4 N
LXXX0220451 0337 BEN_ /& TOT$ N

C:\test>gawk -f change.awk test.txt
LXXX0220451 0337 BEN:QUALEX KS& N
LXXX0220451 0337 TOTAL#OFITEMSI N
LXXX0220451 0337 L-C DIVISION O N
LXXX0220451 0337 2642, DEPART 4 N
LXXX0220451 0337 BEN_ /& TOT$ N
LZZZ0220451 0337 BEN:QUALEX KS N
LZZZ0220451 0337 TOTAL#OFITEMSI N
LZZZ0220451 0337 L-C DIVISION O N
LZZZ0220451 0337 2642, DEPART 4 N
LZZZ0220451 0337 BEN_ / TOT N


Discussion

No Comment Found