1

Hi Guys having a really hard time getting an environmental variable piped to clip.exe, in a regular command prompt it works but once in batch file it fails. I have tried escaping with double %% and a few other tricks. Nothing seems to work.

I am trying to pipe the output of the citrix environmental variable %clientname% to clip.exe like the following:

@echo off
echo %clientname% | clip
phuclv
  • 37,963
  • 15
  • 156
  • 475
user3138092
  • 13
  • 1
  • 3
  • 1
    wtf why are you talking abut escaping? btw you can escape with ^% No idea why echo %blah% isn't working for you. I suggest you troubleshoot. like try other variables. – barlop Dec 27 '13 at 00:37
  • I have been troubleshooting, this isnt just some variable that I have set, this is one that is created only in citrix sessions. Like I said this works from a command line but once run in a batch it does not. Thanks for all the help though. – user3138092 Dec 27 '13 at 00:43
  • Do you want to echo the literal %name% or the contents of the variable? Your code above will be including a trailing space in both situations. – foxidrive Dec 27 '13 at 00:53
  • The contents of the variable is what I am after. I am ok with the trailing space. – user3138092 Dec 27 '13 at 00:54
  • Then what happens when you try that code you have shown? Place `echo."%clientname%"` on the line above - does it echo what you expect? The quotes will show you if the variable is empty. – foxidrive Dec 27 '13 at 00:55
  • If I run what I have above I end up with "ECHO is on." in clipboard. I just ran what you posted and it ends up with just two quotes. – user3138092 Dec 27 '13 at 01:03
  • then it's acting like there is no such variable. are you sure you spelt the variable name right? try set clientna see if the clientname variable appears if not, try set cl Try creating a var called clientname2 try echoing it. view the variables with set | more ot set cli i.e. troubleshoting. See if it shows as set when within a batch file – barlop Dec 27 '13 at 01:07
  • From command prompt on the client I run: Set Clientname and the output is as expected: CLIENTNAME=ENROLL-02 (enroll is the name of the local machine which is correct) – user3138092 Dec 27 '13 at 01:10
  • do it from batch file, as that is where the issue is – barlop Dec 27 '13 at 01:14
  • 1
    Your variable is empty - and the reason might be because you are setting the variable in some other program, Citrix? Or you typo'd the name. – foxidrive Dec 27 '13 at 01:24
  • Actually `echo %%clientname%%| clip` works, but only if variable `clientname` is undefined; if it could be defined, use `echo %%^^clientname%%| clip` instead (assuming there is no variable named `^clientname`)… – aschipfl Jun 30 '21 at 20:09

1 Answers1

1

Just use the ^ sign to escape the % char in command line:

C:\> echo ^%clientname^%

But you need to use the % sign to escape a % symbol in a batch file:

@echo %%clientname%%
nowox
  • 25,978
  • 39
  • 143
  • 293
  • 1
    You can't escape a percent sign with a caret, in a batch file you can escape it with a second `%` – jeb Jan 15 '15 at 15:50