Page 1 of 1

Set statement not working

Posted: 26 Dec 2019 23:04
by Docfxit
I originally didn't have the single quotes on these set statements and the @echo. >>%~f0.txt worked
They worked fine except when it got to the "If defined" line. And it ended the script.
I tried putting double quotes in place of the single quotes and it still ended the script.

Code: Select all

set 'FullTarget1=Docfxit 192.168.168.9'
set 'FullTarget2=DocfxitLT 192.168.168.7'
set 'FullTarget3=ErrolLT530 192.168.2.5'
set 'FullTarget4=CONSOLE2-I5 192.168.168.16'

@echo. %FullTarget1% >>%~f0.txt
@echo. %FullTarget2% >>%~f0.txt
@echo. %FullTarget3% >>%~f0.txt
If defined FullTarget4 (If /i not %FullTarget4%.=="". echo. %FullTarget4%>>%~f0.txt)
Now nothing gets written to %~f0.txt and I would like it to.
What can I do to fix it?

Re: Set statement not working

Posted: 27 Dec 2019 03:16
by miskox
First you must replace ' with ":

Code: Select all

set "FullTarget1=Docfxit 192.168.168.9"
set "FullTarget2=DocfxitLT 192.168.168.7"
set "FullTarget3=ErrolLT530 192.168.2.5"
set "FullTarget4=CONSOLE2-I5 192.168.168.16"
Then replace:

Code: Select all

If defined FullTarget4 (If /i not %FullTarget4%.=="". echo. %FullTarget4%>>%~f0.txt)
with

Code: Select all

If defined FullTarget4 if /i not "%FullTarget4%"=="" echo. %FullTarget4% >>%~f0.txt
But note: this will add a space to the end of the line (to avoid problems if FullTarget4 variable has a number at the end).

Another option:

Code: Select all

If defined FullTarget4 if /i not "%FullTarget4%"=="" >>%~f0.txt (echo. %FullTarget4%)
Saso

Re: Set statement not working

Posted: 27 Dec 2019 08:36
by Docfxit
That worked really great.

Thanks,
Docfxit