Reputation: 365
is there some redirect from a string to a variable in batch?
main.cmd
@setlocal enableextensions Enabledelayedexpansion
@echo off
set log=C:\log\test.log
set my_var=foo
set vars_to_check=my_var
call check_empty.cmd "%vars_to_check%" || goto :error_handler
check_empty.cmd
if "%1" == "" (
echo no param. >> !log!
exit /b 2
)
if not defined %1 (
echo the variable %1 is empty. >> !log!
exit /b 1
)
echo %1 has value %%1%%. >> !log!
exit /b 0
Now I did run main.cmd: I did expect the echo 'my_var has value foo.'. However, I just get 'the variable "%1" is empty.' Please note the quotes around %1. Therefore, I removed the quotes from the call in main.cmd and it almost worked, as I got the message: 'my_var has value %my_var%.'. At least I am arriving at the point where the variable is realized to be defined.
The problem with avoiding the quotes is that the next step would have been: pass multiple vars and loop the check...
set vars_to_check=my_var some_other_var something_else
and now I need the quotes so I can pass the variable...
Upvotes: 0
Views: 67
Reputation: 56180
check_emtpy.cmd
:
@echo off
if "%~1" == "" echo no param. & exit /b 2
if not defined %~1 echo the variable %1 is empty.& exit /b 1
call echo %1 has value %%%~1%% & exit /b 0
If you don't need the message, the last line is optional. If you can guarantee that there is always a parameter (but who could guarantee that...), the first if
is optional too.
Upvotes: 3