1

I am trying to create a .Bat file that only runs and copy's things if the file Startapp.bat exits. This startapp file is created when a user commits code on github.

I have the following script.

taskkill /F /IM Webshop.exe

::%~dp0 means the current folder where this .bat is executed from
SET dest=%~dp0productionEnv

IF EXIST "%~dp0startApp.bat" (
    if not exist "%dest%" mkdir "%dest%" 
    xcopy /Y /s "%~dp0Webshop\bin\Debug" "%dest%"
    SET webDest="%dest%/webContent"

    if not exist %webDest% mkdir %webDest% 
    xcopy /Y /s "%~dp0Webshop\webContent\web" %webDest%
    copy /Y "%~dp0startApp.bat" "%dest%/startApp.bat"
    START "" "%dest%/startApp.bat"
    del "%~dp0startApp.bat"
    echo "Deleted startApp.bat"

) ELSE (
    echo "startApp.bat file not found"
)

But it is not working. Sometimes it echos both the deleted message and the file not found message while that shouldn't be possible. It should echo either of those messages but not both. Thats why there is an if else.

Please help!

MichaelS
  • 5,941
  • 6
  • 31
  • 46
Dilan
  • 91
  • 1
  • 10
  • You are setting a variable inside a code block and file paths generally use a back slash in Windows – Compo Jan 25 '17 at 13:02
  • What's the result of `echo "%~dp0startApp.bat"`? – Dominique Jan 25 '17 at 13:04
  • On which environment are you working? Pre-XP OS have a different handling of "IF EXIST". – MichaelS Jan 25 '17 at 13:09
  • The key is [delayed expansion](http://ss64.com/nt/delayedexpansion.html), as you are writing *and* reading a variable in the same parenthesised block of code. For paths in Windows `cmd`, always use the `\ `as separator. – aschipfl Jan 25 '17 at 18:23

2 Answers2

1

I'm not sure if there are more bugs in the code but I've found at least one:

SET webDest="%dest%/webContent"

if not exist %webDest% mkdir %webDest%

So if the folder doesn't exist, you are executing this line:

mkdir %webDest%

where %webDest% is "%dest%/webContent" means %~dp0productionEnv/webContent.

This line causes an error. You have two possible delimiters in path strings in windows: the right one is \ and the wrong one (yet supported) /. \ comes from DOS and windows and / from UNIX. While windows is often smart enough to parse your commands and even allows you to mix up \ and / the mkdir command does not allow this.

This means: mkdir C:\some\folder will work but mkdir C:/some/folder or mkdir C:\some/folder won't.

EDIT: Same holds for xcopy. Everything after / is regarded as parameters and not as part of the path.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
1

Here's the same thing with the forward slashes fixed and the unnecessary if block changed:

Taskkill /F /IM Webshop.exe

If Not Exist "%~dp0startApp.bat" (
    Echo= startApp.bat file not found
    GoTo Next
)

Rem %~dp0 means the current folder where this .bat is executed from
Set "dest=%~dp0productionEnv"
Set "webDest=%dest%\webContent"

If Not Exist "%dest%" MD "%dest%"
XCopy "%~dp0Webshop\bin\Debug" "%dest%" /Y /S
If Not Exist "%webDest%" MD "%webDest%"
XCopy "%~dp0Webshop\webContent\web" "%webDest%" /Y /S
Copy /Y "%~dp0startApp.bat" "%dest%"
Call "%dest%\startApp.bat"
Del "%~dp0startApp.bat"
Echo= Deleted startApp.bat

:Next
Compo
  • 36,585
  • 5
  • 27
  • 39