Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

treat variable as string in batch

I am trying to make a batch script, that creates a new batch script called MigrateOldStickyNotes.bat with a couple of simple commands in it.

@echo SET CP=%~dp0> "%cp%\%ME%\%ME%\MigrateOldStickyNotes.bat"
@echo SET ME=%Username%>> "%cp%\%ME%\%ME%\MigrateOldStickyNotes.bat"

But insteadt of creating af new batchfile with these to lines that looks exactly like this.

SET CP=%~dp0
SET ME=%Username%

It will create a new file and use the variable as input. which will result i these to lines.

SET CP=\\sosy-nas\Backup\
SET ME=itcebrha

How do i make the script treat the variable as string insteadt of treating it as a variale.

like image 349
Brian Hansen Avatar asked Mar 19 '26 22:03

Brian Hansen


1 Answers

Escape the percent signs with a percent sign, like SET CP=%%~dp0.
All other (special) characters are escaped with a caret ^.

The cause is that percent signs are handled in another phase of the batch parser than all other charaters.
That's also the cause why escaping of percent signs works only in batch files, but not on the command line, as there the cmd-line parser have different expansion rules for percent signs.

like image 80
jeb Avatar answered Mar 23 '26 09:03

jeb