Discussion:
Set an environment variable YWD to characters 20-29 of a one-line file?
(too old to reply)
John Stockton
2023-04-11 22:16:54 UTC
Permalink
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
2023-04-12 01:50:11 UTC
Permalink
Post by John Stockton
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
Post by John Stockton
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
2023-04-12 10:24:34 UTC
Permalink
Post by JJ
Post by John Stockton
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).
Post by JJ
Post by John Stockton
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.
--
(c) John Stockton, near London, UK. Using Google Groups. |
Adam Lubszczyk
2023-04-12 10:58:34 UTC
Permalink
Post by JJ
Post by John Stockton
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).
Post by JJ
Post by John Stockton
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.
--
(c) John Stockton, near London, UK. Using Google Groups. |
@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
2023-04-13 05:29:26 UTC
Permalink
Post by Adam Lubszczyk
Post by JJ
Post by John Stockton
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).
Post by JJ
Post by John Stockton
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.
--
(c) John Stockton, near London, UK. Using Google Groups. |
@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
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
2023-04-13 10:50:57 UTC
Permalink
... ...
Post by JJ
Post by Adam Lubszczyk
FOR /F "tokens=*" %%a IN (file_name.txt) Do CALL :label1 "%%a"
... ...
Post by JJ
Post by Adam Lubszczyk
Adam
I saw that yesterday, and has been thinking about "FOR /F" and not needing a "CALL"; but decided to leave that until today.
Post by JJ
Characters 20-29 is inclusive. Meaning that, 10 characters are extracted.
Yes.
So
Post by JJ
SET YWD=%YWD:~19,10%
that is better, but ...
Post by JJ
@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.
Post by JJ
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
2023-04-14 06:10:25 UTC
Permalink
Post by Adam Lubszczyk
@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
2023-04-14 10:31:15 UTC
Permalink
Post by Adam Lubszczyk
Post by Adam Lubszczyk
@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
OP already mentioned it's a one-line file.

Loading...