Page 1 of 1
Quick Question about File Reading
Posted: 06 Jul 2014 11:43
by JWinslow23
Is there a way to read a file character by character and put each character in an array if it is any of a few certain characters? For example, if the file is:
and the only allowed characters are in the string "+-<>.,[]", the expected contents of an array "chars" would be:
Code: Select all
chars[0]=","
chars[1]="["
chars[2]="."
chars[3]="-"
chars[4]="<"
chars[5]="]"
chars[6]="."
Is there any way that I can do that?
Re: Quick Question about File Reading
Posted: 06 Jul 2014 12:26
by foxidrive
The actual scope of the file would be useful information, like so:
One line, 3000 characters?
Twenty lines, 40 characters each?
Are any of the characters Unicode or Non-Latin characters?
Re: Quick Question about File Reading
Posted: 06 Jul 2014 12:56
by JWinslow23
foxidrive wrote:The actual scope of the file would be useful information, like so:
One line, 3000 characters?
Twenty lines, 40 characters each?
Are any of the characters Unicode or Non-Latin characters?
Well, it can vary. There could be one line with 3000 characters, there could be multiple lines with any amount of characters, maybe the same characters per line, maybe not, it depends.
And none of the characters are Unicode. They are ASCII values below 128.
Re: Quick Question about File Reading
Posted: 06 Jul 2014 22:25
by foxidrive
This uses a helper batch file called `repl.bat` (by dbenham) - download from:
https://www.dropbox.com/s/qidqwztmetbvklt/repl.batPlace `repl.bat` in the same folder as the batch file or in a folder that is on the path.
That's half the job.
Code: Select all
type file.txt |repl "[^+-<>.,\[\]]" "" >newfile.txt
This will separate each valid character onto it's own line.
Code: Select all
type file.txt |repl "[^+-<>.,\[\]]" "" |repl "(.)" "$1\r\n" xm
From there it's a simple loop to store them all in an array.
Re: Quick Question about File Reading
Posted: 07 Jul 2014 06:19
by Aacini
The solution below is short and simple, but may be somewhat slow. It also may have problems if the file have exclamation marks; both points may be fixed, if needed. This method can read lines up to 8192 characters long.
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "allowedChars=+-<>.,[]"
set i=-1
for /F "delims=" %%a in (test.txt) do (
for /F "delims=" %%b in ('cmd /U /C echo "%%a"^| find /V ""') do (
if "!allowedChars:%%b=!" neq "%allowedChars%" (
set /A i+=1
set chars[!i!]="%%b"
)
)
)
SET CHARS[
Output example:
Code: Select all
C:\> type test.txt
This, i[s a.tes t-c< as] e.
C:\> test.bat
chars[0]=","
chars[1]="["
chars[2]="."
chars[3]="-"
chars[4]="<"
chars[5]="]"
chars[6]="."
Antonio
Re: Quick Question about File Reading
Posted: 07 Jul 2014 10:13
by JWinslow23
Well, that's nice, I gotta say. And thank you; I will not need to search for exclamation marks.