how do i make an output of ECHO SET V5=2>> %BAT%

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
nnnmmm
Posts: 117
Joined: 26 Aug 2017 06:11

how do i make an output of ECHO SET V5=2>> %BAT%

#1 Post by nnnmmm » 31 Dec 2021 03:02

AA=

Code: Select all

SET BAT="M:\123.BAT"
ECHO @ECHO OFF> %BAT%
ECHO SET D0=C:\UTILITY\DOSBox-X\!BOX-X-Interface1.BAT>> %BAT%
ECHO.>> %BAT%
ECHO SET D1=C:\UTILITY\EDT\EDT1.EXE>> %BAT%
ECHO SET D2=%%D1%%>> %BAT%
ECHO SET O3="">> %BAT%
ECHO SET C4=dosbox-x1.conf>> %BAT%
ECHO SET V5=2>> %BAT%
ECHO SET V6=4>> %BAT%
ECHO SET V7=1>> %BAT%
ECHO SET E8=0>> %BAT%
ECHO SET E9=1>> %BAT%
ECHO "%%D0%%" %%D1%% %%D2%% %%O3%% %%C4%% %%V5%% %%V6%% %%V7%% %%E8%% %%E9%%>> %BAT%
the result of AA is
@ECHO OFF
SET D0=C:\UTILITY\DOSBox-X\!BOX-X-Interface1.BAT

SET D1=C:\UTILITY\EDT\EDT1.EXE
SET D2=%D1%
SET O3=""
SET C4=dosbox-x1.conf
SET V7=
SET E9=
"%D0%" %D1% %D2% %O3% %C4% %V5% %V6% %V7% %E8% %E9%

but i want the result to be like below
@ECHO OFF
SET D0=C:\UTILITY\DOSBox-X\!BOX-X-Interface1.BAT

SET D1=C:\UTILITY\EDT\EDT1.EXE
SET D2=%D1%
SET O3=""
SET C4=dosbox-x1.conf
SET V5=2
SET V6=4
SET V7=1
SET E8=0
SET E9=1
"%D0%" %D1% %D2% %O3% %C4% %V5% %V6% %V7% %E8% %E9%

all i know is that things like 2> 1> have special meanings in DOS batch, but putting it like this way ECHO SET "V6=4">> %BAT% works but causes an output to have "", this means i can not change those numbers fast in a batch file editing because i have to use extra key presses to go over ""'s

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#2 Post by aGerman » 31 Dec 2021 05:34

Please read the best practice tips:
viewtopic.php?p=64711#p64711
Two of them would have avoided this issue.
- quote assignments
- write redirections first
The latter also makes your script so much more readable for a sequence of several redirections.

Steffen

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#3 Post by Compo » 31 Dec 2021 06:57

Something along theses lines, perhaps:

Code: Select all

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
SET "BAT=.\123.BAT"
(	ECHO @ECHO OFF
	ECHO SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
	ECHO SET "D0=C:\UTILITY\DOSBox-X\!BOX-X-Interface1.BAT"
	ECHO(
	ECHO SET "D1=C:\UTILITY\EDT\EDT1.EXE"
	ECHO SET "D2=%%D1%%"
	ECHO SET "O3="""
	ECHO SET "C4=dosbox-x1.conf"
	ECHO SET /A "V5=2, V6=4, V7=1, E8=0, E9=1"
	ECHO CALL "%%D0%%" "%%D1%%" "%%D2%%" %%O3%% "%%C4%%" %%V5%% %%V6%% %%V7%% %%E8%% %%E9%%
) 1>"%BAT%"

nnnmmm
Posts: 117
Joined: 26 Aug 2017 06:11

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#4 Post by nnnmmm » 31 Dec 2021 08:03

>"foo.txt" echo 1
>>"foo.txt" echo 1
i kind of knew about this them, but they never got to have stuck in my head
kind of like, i'll cross the bridge when i come to it, thanks! i get to finish making my dosbox shell thing or making dosbox version of shortcuts meaning more numbers of batch files.

the below worked
>>%BAT% ECHO SET V5=2
>>%BAT% ECHO SET V6=4
>>%BAT% ECHO SET V7=1
>>%BAT% ECHO SET E8=0
>>%BAT% ECHO SET E9=1

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#5 Post by aGerman » 31 Dec 2021 10:33

I strongly advise you to make yourself familiar with proper quoting. You still disregard quoting of assignments and paths/file names. This prevents you from writing code fluently, just because you have to permanently think about where you necessarily need it. Just default to quote! Also, maintaining code is getting difficult. E.g. once you update the file name to something which requires quotes, you have to review every line of your code in the hope that you find every occurrence of an unquoted name.

Another thing you should think about is variable names. Make them intuitive. If you review your code, say, in in half a year, I'm sure you don't remember what D, O, C, or E are for, and the numbers behind them are even more obscure. (I'm not even talking about other people who try to understand your code.)

Steffen

nnnmmm
Posts: 117
Joined: 26 Aug 2017 06:11

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#6 Post by nnnmmm » 31 Dec 2021 12:30

>>proper quoting
i have a hard time making batches with quotes work, i tried, because i dont quite understand batches. these were what i observed (because i dont quite understand it). double quotes make quotes disapear sometime, double slashes \\ display as double slashes but work as no slash behind my back, similar things go with "." and %, at the end i dont know how many i need to repeat putting them to make them work. so i type and put something until it works blindly but sensibly. but i will have to watch out for stray empty spaces at the end of variables. sometimes CD path doesnt work the way it used to, i have to do it like
CD H:
CD H:\
CD H:\\
CD H:\\\\\\
CD "H:\ "

>>'m sure you don't remember what D O, C, or E are..
i always put excessive amounts of subtexts in my batches, like
ECHO ~dp1\ = "%~dp1\" (damn OS interprets as no \\, but displays as \\)

all these batches will go to ztree and xtree and link there, these utilities provide paths as variables for every click i make.

Code: Select all

REM D1 = program for desktop
REM D2 = program for laptop
REM O3 = option
REM C4 = 2nd conf
REM V5 = resolution for desktop (1=800x600 2=867x650 3=1024x768 4=1200x900 O=original)
REM V6 = resolution for laptop  (1=800x600 2=867x650 3=1024x768 4=1200x900 O=original)
REM V7 = scaler                 (1 2 3 4 5 or name)
REM E8 = fullscreen             (1=yes  0=no) 
REM E9 = EXIT                   (1=yes  0=no)

REM V7=1 norma12x
REM V7=2 norma13x
REM V7=3 advinterp2x
REM V7=4 advmame2x
REM V7=5 hq2x

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#7 Post by aGerman » 31 Dec 2021 13:35

Backslashes might escape quotes if used in arguments that you pass to other tools. It has nothing to do with Batch but with the way certain tools interpret and tokenize a command line.
i always put excessive amounts of subtexts in my batches
Unnecessary as soon as the variable names are self-explanatory.
ECHO ~dp1\ = "%~dp1\" (damn OS interprets as no \\, but displays as \\)
If you passed a path to your batch file then %~dp1 already expands to the path of the parent directory, including the trailing backslash. Thus, %~dp1\ will show up with two trailing backslashes. However, the OS might treat a double backslash like a single backslash in a path, as well as it might accept a forward slash instead of a backslash because that are common mistakes users make.

As to the . at the end of paths, also this is well-defined. "C:\somewhere\else\." is the same like "C:\somewhere\else". And "C:\somewhere\else\.." is the same like "C:\somewhere".
CD H:
CD H:\
CD H:\\
CD H:\\\\\\
CD "H:\ "
Huh? If you want to change the drive you have to use the /D switch along with the CD command. FWIW Even the last will usually work well. Again the OS is nice and ignores the space as a common typo.
stray empty spaces
Quoting the assignments will protect you from them. Just another advantage.

Code: Select all

set x=a 
set "y=b" 
Both lines have an extra space at their end. However '%x%' expands to 'a ' (with trailing space), while '%y%' expands to 'b' (without trailing space).

Steffen

nnnmmm
Posts: 117
Joined: 26 Aug 2017 06:11

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#8 Post by nnnmmm » 06 Jan 2022 12:19

%2 and %3 ztree's variables, nothing more.
IF /I "%3"=="\" (SET DD1=%2:%3\.) ELSE (SET DD1=%2:%3) this is dosbox version to get the correct root drive that i made it to work at the end
IF /I "%3"=="\" (SET DD1=%2:%3\ 'needs a big SPACE') ELSE (SET DD1=%2:%3) this is dosbox-x version to get the correct root drive that i made it to work at the end
then doxbox says to me "you succesfully mounted M:\\\.\ to C:"

at least now i know when i get to handle directories, a file in a root drive or a root drive itself.
i put these 3 lines every step of the way,
echo %AA1%
pause
goto :eof

once i finished coding. scripts were so messed up and ugly, all the subtexts in the world that i put in to explain what i did were not enough to help me, i forget most of them in a week or two, if i need to change the code again, i have to do this again and again step by step
echo %AA1%
pause
goto :eof


SET "K1=%*"
this one costed me my life, %K1% couldnt handle ! & for years, so i got tired of it, decided to fix the damn drag and drop batch files by doing this
echo %AA1%
pause
goto :eof

SET K1=%* was the corect one, without the quotes, it was like a miracle there was actually a correct solution. it solved the problems of URL names having ! or &, now i can feed them to youtube-dl without a problem, my having problems with ! & in dos batch is not just a today's problem, it has been always. whats gonna be different this time than any other and make me fix the batch this time was anyone's guess. so i have been letting them be. but once in a while i try to poke at them again....

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#9 Post by aGerman » 06 Jan 2022 13:46

SET K1=%* was the corect one, without the quotes
Simple reason: Arguments which need to be quoted are already quoted in %*.
Say, you pass "1 & 2" "3 & 4" to the script, then your unquoted SET command behaves like that:
SET K1="1 & 2" "3 & 4"
where the green terms are protected by the pairs of quotes.
In contrast, a quoted SET command behaves like that:
SET "K1="1 & 2" "3 & 4""
So, assigning %* to a variable might be one of the rare exceptions where you shouldn't use the additional quoting.
However, problems with exclamation points are unrelated. Most likely those are caused by a wrong use of delayed variable expansion.

Steffen

nnnmmm
Posts: 117
Joined: 26 Aug 2017 06:11

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#10 Post by nnnmmm » 09 Jan 2022 21:23

>>However, problems with exclamation points are unrelated.
i dont know about this one, if URL name string had !, i cant forget this what it did. i couldnt use it for many years, i had to change the name to any quick strings like gagafdg to make it work. but for a recent year it didnt cause a problem at all, maybe related to OS update. or i thought people were maybe suddenly using the unicode versions of ! in name making, or vice versa, or maybe a company oneday decided to apply "replace all" not working version ! with working version of !... then agian i use this "delayed variable expansion" without knowing exactly what it does in just about all of my batches.

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#11 Post by atfon » 10 Jan 2022 09:15

nnnmmm wrote:
09 Jan 2022 21:23
... then agian i use this "delayed variable expansion" without knowing exactly what it does in just about all of my batches.
If you would like to know a bit more about how Delayed Expansion works, I would suggest this page:

https://ss64.com/nt/delayedexpansion.html

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#12 Post by aGerman » 10 Jan 2022 11:18

I also wrote about how to relatively safely use it
viewtopic.php?p=64717#p64717

Steffen

Jedininja
Posts: 25
Joined: 11 Jan 2022 22:41
Location: CanafukpilesDa
Contact:

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#13 Post by Jedininja » 11 Jan 2022 23:21

will append output into a file.

Code: Select all

set a=b
echo %a% >> %systemdrive%/file.txt
will output into a file but overwrite the file if used multipal times.

Code: Select all

set a=b
echo %a% > %systemdrive%/file.txt
will overwrite file
>
will append in a file
>>

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#14 Post by penpen » 12 Jan 2022 07:03

Though that information is technically correct, this is not the issue of the OP,
since nnnmmm used that specific detail corrrectly in his opening post.

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: how do i make an output of ECHO SET V5=2>> %BAT%

#15 Post by Compo » 12 Jan 2022 18:45

If you had shown the common decency to have acknowledged my code earlier than the twelve days, since I offered it to you, I'd have happily explained to you how to get it to work properly with your modified commands.

I gave you ample opportunity to do so, and prompted you about that too, but you decided otherwise. In life, we have to live by our decisions, and you will now have to rely upon somebody else to waste their time and effort on you instead.

Post Reply