This could possibly a duplicate of several questions out there in the SO, dostips and ss64. The research I've done point me to look out for _scope_ in functions. but my solutions is simple and straight forward but still problem exists
- what is really behind the
SETLOCALandENDLOCAL - How
contextworkd in batch scripts ex(goto) 2>nul - Why
(goto)wrapped in braces (explained in dostips)
Here is the code i've written so far to copy file from one place to another. My Goals were:
- Research the
scopeandcontextof batch script - code reuse
@echo off
setlocal EnableDelayedExpansion
goto :main
:main
setlocal
set _app=test
set _base=C:/wamp64/www
set _destination=!_base!/test
set _source=%~dp0%/build
set /A _flag=0
echo *********************************************
echo Deploying in %~1 mode: %TIME%
echo Deploy path: !^_destination!
echo *********************************************
call :check !_base!, !_app!, _flag
if !_flag!==0 (
call :create !_base!, !_app!
)
xcopy "!_source!" "!_destination!" /D /C
exit /b 0
endlocal
:setbase
echo ::::: setting up base :::::
chdir /D C:
rem the base dir for app to exists=> %1
chdir %~1
exit /b 0
:check
echo ::::: checking for local web server destination :::::
call :setbase %~1
set %~3= dir /p|find /C "%~2"
exit /b 0
:create
echo ::::: creating the app folders :::::
rem setting the base to create app folder %1
call :setbase %~1
mkdir %~2
exit /b 0
endlocal
This is the output i get when i initiate deploy.bat
*********************************************
Deploying in production mode: 19:28:53.13
Deploy path: C:/wamp64/www/test
*********************************************
::::: checking for local web server destination :::::
::::: setting up base :::::
0
::::: creating the app folders :::::
::::: setting up base :::::
A subdirectory or file test already exists.
seems like the If !_flag!==0 which is checking whether the app folder exist in the server root and is not working at all. When i learnt the way to pass parameters to other functions; i thought it as passing a pointer like reference but it looks like it is deeply tied to scope.
So what's going on here in the code.
:check, you are aren't actually running the command when you set_flag, you're setting that variable to the literal string. Usefor /Fto store command output in a variable.%~dp0, not%~dp0%!