Set statement not working

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Docfxit
Posts: 130
Joined: 12 Nov 2015 12:42

Set statement not working

#1 Post by Docfxit » 26 Dec 2019 23:04

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?

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: Set statement not working

#2 Post by miskox » 27 Dec 2019 03:16

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

Docfxit
Posts: 130
Joined: 12 Nov 2015 12:42

Re: Set statement not working

#3 Post by Docfxit » 27 Dec 2019 08:36

That worked really great.

Thanks,
Docfxit

Post Reply