In a backup-restore scenario, I'm trying to generate a helper from within a batch.
The helper just combines restore.bat
and an argument of the form state-%today%.zip
to allow a specific restore without opening a command prompt or drag and drop:
call restore.bat state-YYYYMMDD.zip
This way it can be easily generated by backup.bat
. But the helper fails if it was run as administrator (which is the actual goal of the coupling) because due to the context switch the current work directory changes to C:\Windows\system32
and that's why the helper's sibling file is not found.
This can be fixed by using %~dp0
that returns the path of the helper batch dynamically. My hand-made solution works as expected:
pushd %~dp0
call restore.bat %~dp0state-YYYYMMDD.zip
popd
Since YYYYMMDD
is determined at backup time. I now need to generate the helper dynamically from within backup.bat
, the first approach is
backup.bat
set HELPER=restore-%today%.bat
echo pushd %~dp0 > %HELPER%
echo restore.bat %~dp0state-%today% >> %HELPER%
echo popd >> %HELPER%
but the result
helper-20210630.bat
pushd c:\users\me\test\
call restore.bat c:\users\me\test\state-20210630.zip
popd
...is not portable to other folders. How do I pipe %~dp0
verbatim to the helper batch?
I found nothing in
And the advice to escape %
via ^
that I found in batch-file - Echo output to file | batch-file Tutorial didn't work.