How do you use a batch file to create another batch file that contains %username%

2

I have been trying to make a batch file using another batch file.
The file I want to make needs to contain %username% in order for it to be functional in other computers as well.
Whenever I try to do it it types my actual username in the file which makes it impossible to run on other computers. Can someone please help me with my problem?

Here is what I wrote in the batch file:

Echo cd c:\Users\%username%\Documents > x.bat

But when I open x.bat it says:

Echo cd c:\Users\myname\Documents

I actually know what causes this problem but I couldn't find any way to get past it.

Ali Demir

Posted 2018-06-22T11:07:54.047

Reputation: 21

1To avoid instant expansion double the enclosing percent signs in the generating batch, %%USERNAME%% – LotPings – 2018-06-22T11:26:31.377

Does the same thing apply for %random% ? – Ali Demir – 2018-06-22T11:33:18.973

Yes, enclose similarly %%random%% – LotPings – 2018-06-22T11:36:25.580

% is escape character in batch file. Use %% instead of one %. More Details. – Sandeep – 2018-06-22T11:37:53.210

1@LotPings, why not expand your comment slightly as an answer, since it successfully resolves the issue. – DrMoishe Pippik – 2018-06-22T16:29:59.183

Answers

2

Some clever people examined the inner workings of cmd.exe processing batch files in this stackoverflow.com Q&A.

To escape a % it has to be doubled
(the above mentioned link explains that this is a step early undone by the batch parser)

If you repeatedly set and echo a variable containing a % you will have to do the doubling for every step involved to delay the expansion % -> %% -> %%%%.

The related issue delayedexpansion has two solutions .

  1. Setlocal EnableDelayedExpansion and usage of ! instead of %.
    This is required when you set and use a variable in a (code block)
  2. Usage of a pseudo call with set or echo commands to force a 2nd evaluation pass.
    (to indirectly reference a variable for example)

Examples:

:: Q:\bar\2018\06\22\SU_1333415.cmd
@Echo off
( Echo cd c:\Users\%username%\Documents
  Echo random number %random%
) >x.bat 
Echo============x.bat=============
type x.bat
Echo=
( Echo cd c:\Users\%%username%%\Documents
  Echo random number %%random%%
) >y.bat 
Echo============y.bat=============
type y.bat
Echo=
Echo ========call echo============
Set bar=6
Set foo=bar
Call Echo foo = %foo%, %%foo%% = %%%foo%%%
Echo=
Echo =====delayed expansion !===
Setlocal EnableDelayedExpansion
Set bar=6
Set foo=bar
Echo foo = %foo%, !foo! = !%foo%!
Echo=
Echo =======(code block)==========
Set "Answer=n/a"
( Set /P "Answer=Enter a number: "
       Echo 1. the Answer is [%Answer%]
  Call Echo 2. the Answer is [%%Answer%%]
       Echo 3. the Answer is [!Answer!]
)
Echo==============================

Sample output:

20:07:57 Q:\Test\2018\06\22
> SU_1333415.cmd
===========x.bat=============
cd c:\Users\LotPings\Documents
random number 5680

===========y.bat=============
cd c:\Users\%username%\Documents
random number %random%

========call echo============
foo = bar, bar = 6

=====delayed expansion !===
foo = bar, bar = 6

=======(code block)==========
Enter a number: 11
1. the Answer is [n/a]
2. the Answer is [11]
3. the Answer is [11]
=============================

LotPings

Posted 2018-06-22T11:07:54.047

Reputation: 6 150