0

This is my first time creating a batch file. I am trying to execute by adding all the following inside a batch file. however the flow stops at the for loop. these command works when directly executed on command prompt. And I am also facing error with the copy command

SET ROOT="C:\Rahul\Projects\sub-folder"

SET WEB1=%ROOT%\folder1\1.war
SET WEB2=%ROOT%\folder2\2.war

SET SOURCE=%WEB1% %WEB2% 

SET TARGET=C:\Rahul\softwares\apache-tomcat-6.0.29\webapps\

c:
cd C:\Rahul\softwares\apache-tomcat-6.0.29
rmdir /q /s work\Catalina
cd webapps

FOR %i in ("*.war") do rmdir /q /s  %~ni
FOR %i in ("*.war") do del /q /s  %i
FOR %i in (%SOURCE%) do copy %i %TARGET%

2 Answers 2

1

FOR %i in ("*.war") do rmdir /q /s %~ni will work in the command line but not in a batch. Inside a batch file you must replace %i with %%i. This should be it.

Sign up to request clarification or add additional context in comments.

3 Comments

I am also facing problem with the copy command as my folder have space in between them the last for loop is considering as different items.
Surround your variables with quotes: FOR %%i in (%SOURCE%) do copy "%%i" "%TARGET%"
got it i replace SET WEB1="%ROOT%\folder1\1.war"
0

In my experience, it is more often easier to not use quoting in the SET statement. Then, use quoting whenever it is used.

SET THEVAR=C:\Program Files
DIR "%THEVAR%\Common Files"

2 Comments

If i do not use the quote it converts the space between folder names as an another variable causing inconsistency within array SOURCE
You are right, @RahulThachilath. However SOURCE does not represent a single file or directory name. I my experience, it would be more clear to use FOR %i in ("%WEB1%" "%WEB2%") do ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.