0

I'm trying to write a text to a file using a batch file:

start https://www.google.com/search?q=%clipboard%%"

@echo off
set "Text=start https://www.google.com/search?q=%clipboard%%"
set "Text=%Text:^%=%%%"
echo %Text%>>lala.txt

But what is writen to the text file is only:

start https://www.google.com/search?q=%

The idea is to duplicate the percent signs so they are escaped and seen as text only but I guess for some reaseon the batch still things it's a variable. I know I could just use:

echo start https://www.google.com/search?q=%%clipboard%%%%"

But let's say I don't know what the line looks like I only know it has percent signs in it how should I proceed? The only thing I want to do is write the line as is into a text file without any manipulation.

Ricardo Bohner
  • 301
  • 2
  • 10
  • `...?q=%clipboard%%` -- is this really the literal URL string you want to preserve, or should it read `...?q=%clipboard%`? – aschipfl May 02 '20 at 22:05

1 Answers1

3

When you define a literal string in a batch file, like set "Text=A single %%-sign" or echo A single %%-sign, you always have to manual double %-signs; otherwise, the %-expansion phase consumes them and tries to expand variables (refer to this answer for more details).

However, when the input text comes from somewhere else, like user input (set /P Text="Enter text: ") or from a file (e. g., read by for /F) you do not have to manually double %-signs, because the %-expansion phase is already completed when the text arrives.


This is the original answer before I recognised the real problem:

Well, the following line in your code cannot work:

set "Text=%Text:^%=%%%"

Because, besides the fact that it would actually replace ^% rather than %, the %-sign behind the =-sign finishes this sub-string substitution expression, and the remaining %% become then replaced by a single literal % (refer to this answer for more details).

To double %-signs in an arbitrary string you need to enable delayed expansion, because this uses ! instead of % to mark variables, which do not interfere with the literal %-signs you want to replace/double:

@echo off

rem /* You have to double `%`-signs when you put a literal string here; so
rem    this literaly sets `https://www.google.com/search?q=%clipboard%`: */
set "Text=https://www.google.com/search?q=%%clipboard%%"

rem // Enable delayed expansion:
setlocal EnableDelayedExpansion

rem // Literal `%`-signs still have to be doubled here:
set "Text=!Text:%%=%%%%!"

rem // Return the string with `%`-signs doubled:
echo Delayed expansion: !Text!
echo Normal  expansion: %Text%
set Text & rem // (avoiding `echo` here to review the true variable value)

rem // Variables set/changed since `setlocal` become lost past this point:
endlocal
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Thanks, this seems to work but only if the string ends with %%clipboard%% but it is actually %clipboard%% and again I don't know how the string is going to be I only know it has percent signs in it... – Ricardo Bohner May 02 '20 at 23:06
  • 2
    When you have to put the input string into the script (like `set "Text=..."`) you will always have to manually double the `%`-signs; when the input comes from the user (by `set /P Text="Please type a string: "`) or from a file (e. g., `< "file.txt" set /P Text=""`) you don't have to double anything manually; so where does your string actually come from? – aschipfl May 02 '20 at 23:21
  • Sorry I made a mistake actually when you pharse a text file and pass the replacement variable to a normal variable it gets the link right as it should be. – Ricardo Bohner May 02 '20 at 23:32
  • So the problem actually only happens if you manually pass a value to a variable... – Ricardo Bohner May 02 '20 at 23:37
  • 2
    Yes, that's correct... seems we tried to solve an [XY Problem](http://xyproblem.info/), didn't we? ;-) – aschipfl May 02 '20 at 23:40
  • Yeah, I just assumed when you pharse a text file or get user input you would have the same problem as when you manually set the variable but apparently not. – Ricardo Bohner May 02 '20 at 23:45