Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Set an environment variable YWD to characters 20-29 of a one-line file?

86 views
Skip to first unread message

John Stockton

unread,
Apr 11, 2023, 6:16:55 PM4/11/23
to
I can set an environment variable YWD to characters 20-29 of a one-line file, ending CRLF, using an ancient utility COLS, written by me.

But it must be nicer to do it in pure Batch - how?


--
(c) John Stockton, near London, UK. Using Google Groups. |

JJ

unread,
Apr 11, 2023, 9:50:14 PM4/11/23
to
On Tue, 11 Apr 2023 15:16:54 -0700 (PDT), John Stockton wrote:
>
> But it must be nicer to do it in pure Batch - how?

Are those character code numbers in hexadecimal or in decimal?

If they're in hexadecimal, it'd be easy:

set YWD= !"#$%&'()

However, if they're hexadecimal, i.e. decimal control code 20 to 29; then I
think it's not possible using pure batch, since the control code 26 is for
EOF.

At command prompt, EOF is treated as input terminator or end of input, and
any following characters are ignored. e.g. below command line displays "abc"
and doesn't cause any error due to syntax error because of the missing
command after the pipe character. Note: the `^Z` is the EOF character
emittable from the command prompt using CTRL+Z. It's not a 2-characters
literal string.

echo abc^Zxyz |

In batch file, EOF is treated as a line separator or a command line
separator. e.g. below code displays "abc" and "xyz" in separate lines.

echo abc^Zecho xyz

> I can set an environment variable YWD to characters 20-29 of a one-line
> file, ending CRLF, using an ancient utility COLS, written by me.

If your COLS tool use `SetEnvironmentVariable` Windows API function, you're
only setting an environment variable which belong to that tool's process. It
won't affect the environment variable of the batch file (the CMD process)
where that tool is executed from. Meaning that, the batch file won't have
the YWD variable added. It would require `WriteProcessMemory` function to
modify environment variables of other process, and it's not a trivial thing
to achieve.

John Stockton

unread,
Apr 12, 2023, 6:24:35 AM4/12/23
to
On Wednesday, 12 April 2023 at 02:50:14 UTC+1, JJ wrote:
> On Tue, 11 Apr 2023 15:16:54 -0700 (PDT), John Stockton wrote:
> >
> > But it must be nicer to do it in pure Batch - how?
> Are those character code numbers in hexadecimal or in decimal?

They are decimal, but not character code. That means from the 20th character to the 29th character (there is a "W", 2 "-", and 7 digits).


> > I can set an environment variable YWD to characters 20-29 of a one-line
> > file, ending CRLF, using an ancient utility COLS, written by me.

My COLS is a fairly simple stream editor, reading lines from Standard Input, processing each in turn, and writing them to Standard Output. Its arguments include strings and position indication.
So I just use it to edit the input line to a SET YWD=?????????? line, redirect the output to a new file, call that file, and delete it.

COLS was written in 16-bit Borland Pascal, probably version 7, last edited & compiled 2005-10-29, and recompiled in Delphi 3 on 2009-02-10 to get the current 32-bit EXE file.

Adam Lubszczyk

unread,
Apr 12, 2023, 6:58:35 AM4/12/23
to
@ECHO OFF
FOR /F "tokens=*" %%a IN (file_name.txt) Do CALL :label1 "%%a"
EXIT

:label1
REM ~ delete "
SET YWD=%~1
REM Substr 9 chars begin 20 (offset 19)
SET YWD=%YWD:~19,9%
ECHO %YWD%
REM ...
GOTO :EOF


Adam

JJ

unread,
Apr 13, 2023, 1:29:29 AM4/13/23
to
Characters 20-29 is inclusive. Meaning that, 10 characters are extracted. So
it should be:

SET YWD=%YWD:~19,10%

FYI, shorter version of the code:

@ECHO OFF
SETLOCAL
SET YWD=
FOR /F "usebackq tokens=*" %%a IN ("file name.txt") do set "YWD=%%a"
SET "YWD=%YWD:~19,10%"
ECHO %YWD%

Use of `backq` option is recommended to allow parsing file whose name
contains space or special character.

John Stockton

unread,
Apr 13, 2023, 6:50:58 AM4/13/23
to
On Thursday, 13 April 2023 at 06:29:29 UTC+1, JJ wrote:
> On Wed, 12 Apr 2023 03:58:34 -0700 (PDT), Adam Lubszczyk wrote:
... ...
> > FOR /F "tokens=*" %%a IN (file_name.txt) Do CALL :label1 "%%a"
... ...
> > Adam

I saw that yesterday, and has been thinking about "FOR /F" and not needing a "CALL"; but decided to leave that until today.

> Characters 20-29 is inclusive. Meaning that, 10 characters are extracted.
Yes.
So
> it should be:
> SET YWD=%YWD:~19,10%
that is better, but ...

> FYI, shorter version of the code:
>
> @ECHO OFF
> SETLOCAL
> SET YWD=
> FOR /F "usebackq tokens=*" %%a IN ("file name.txt") do set "YWD=%%a"
> SET "YWD=%YWD:~19,10%"
> ECHO %YWD%

The code is actually in the start-up file for a CMD.EXE window, so SETLOCAL had to be removed. H'mm - it should be in the log-in setup file for a User, or the one for All Users. For me, that is not important.

As is, the "19" must be "17", because the input line starts with two spaces; so perhaps "tokens=*" is not needed.

> Use of `backq` option is recommended to allow parsing file whose name
> contains space or special character.

The file name is "INC-YWWD.JS", and I have no expectation of changing it in any way!
I use short names for manually-executed routines (e.g. "N" calls N.BAT which calls an improvement on "dir | find "%date% | sort") and four-letter ones <etla>.TXT for complex reasons. But otherwise
I use only upper-case letters, digits, minus signs, and the "8.3" pattern - - - except sometimes.

The context of that file is currently machine-generated on CMD.EXE start-up, the only regular change being to the values of digits in it. My YWD is actually the ISO-8601 Week-Numbering Date.

So, question solved, but the actual code has been learned from, and may be adjusted a little.

Thanks.

Adam Lubszczyk

unread,
Apr 14, 2023, 2:10:26 AM4/14/23
to
czwartek, 13 kwietnia 2023 o 07:29:29 UTC+2 JJ napisał(a):

> FYI, shorter version of the code:
>
> @ECHO OFF
> SETLOCAL
> SET YWD=
> FOR /F "usebackq tokens=*" %%a IN ("file name.txt") do set "YWD=%%a"
> SET "YWD=%YWD:~19,10%"
> ECHO %YWD%
>
This code set variable to value from LAST LINE of file.txt only ( if file.txt is multi lines)

Adam

JJ

unread,
Apr 14, 2023, 6:31:17 AM4/14/23
to
OP already mentioned it's a one-line file.
0 new messages