Why is batch not running this second for loop? What am i doing wrong? The first loop runs fine, but the second loop is never hit. I even put an echo statement after the first loop and it is not even displayed.
FOR /D %%X in (..\Apps\Mine\*) do if exist "%%X\AndroidManifest.xml" ("%1\android.bat" update project -p "%%X") else (
echo This is not an android project.
)
FOR /D %%Y in (..\Apps\Theirs\*) do if exist "%%Y\AndroidManifest.xml" ("%1\android.bat" update project -p "%%Y") else (
echo This is not an android project.
)
More details
The current working directory also has no () or spaces in the name.
Windows7 64bit.
This is the exact argument i am using:
> update_project.bat C:\Users\MyUserName\android-sdks\tools
That is all the contents of the batch script. There is nothing else in it.
Here is the directory structure. The batch script is run from the CWD.
Projects
Apps
Mine
App1
App2
Theirs
App3
App4
Tools (CWD)
Add a CALL statement in front of "%1\android.bat". If you don't use CALL, control will not be returned from "%1\android.bat".
Like this,
FOR /D %%X in (..\Apps\Mine\*) do if exist "%%X\AndroidManifest.xml" (CALL "%1\android.bat" update project -p "%%X") else (
echo This is not an android project.
)
FOR /D %%Y in (..\Apps\Theirs\*) do if exist "%%Y\AndroidManifest.xml" (CALL "%1\android.bat" update project -p "%%Y") else (
echo This is not an android project.
)
Related
This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed last month.
I'm trying to make it so when the script is first ran it checks if there is a file, if there isn't then this is the first time the script has been run and it should run some code else it is been run more then once and should execute other code. Every time I run the file cmd crashes.
This is my file
#echo off
cd src/ops
IF EXIST setupdone.txt (
set /p setting=Do you want to run the server or change your settings?:
echo "1 - Run Server"
echo "2 - Change Settings"
IF %setting% == "1" (
echo it is 1
) ELSE (
IF %setting% == "2" (
echo it is 2
) ELSE (
echo Invalid Input! Rerun this file to try again.
)
)
) ELSE (
cd src/server
echo Installing Modules...
call npm install
cd ../settings
call npm install
cd %~dp0
cd src/ops
echo setupdone>setupdone.txt
cd %~dp0
)
echo done
pause
The solution to the main issue with your submitted code is probably the most common throughout the pages under the batch-file tag. Whenever a variable is defined or modified within a parenthesized block, in order for it to be used with its new value, delayed variable expansion needs to be enabled.
However, you had no need to do it using a variable, you simply needed not to use Set with its /P option. The choice.exe utility is provided for exactly this type of task.
Example:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
CD /D "%~dp0."
If Exist "src\ops\setupdone.txt" (
Echo 1. Run Server
Echo 2. Change Settings
"%SystemRoot%\System32\choice.exe" /C 12 /M "Do you want to run the server, or change your settings"
If Not ErrorLevel 2 (Echo It is 1.) Else Echo It is 2.
) Else (
PushD "src\server" 2>NUL && (
Echo Installing Modules...
Call "npm.cmd" install
PopD
)
PushD "src\settings" 2>NUL && (
Call "npm.cmd" install
1>"..\ops\setupdone.txt" Echo setupdone
PopD
)
)
Echo Done.
Pause
I want to run multiple commands in if condition in batch file.
I have tried below code but its not working for me.
IF not exist %directoryPath% (echo Invalid directory. goto :InvalidDirectory) ELSE (echo Sencha app build development started..)
Code:
:EnterDirectory
echo Enter your project directory
set /P directoryPath=
IF exist %directoryPath% (goto :init) ELSE (goto :InvalidDirectory)
:InvalidDirectory
echo This directory does not exists.
(goto :EnterDirectory)
:init
IF not exist %directoryPath% (goto :InvalidDirectory) ELSE (echo Sencha app build development started..)
When using batchfiles, don't try to do everything in one line. That makes your script unreadable and might even give you wrong/unexpected results, when you don't pay enough attention to the syntax (which easily happens). This kind of failures is hard to troubleshoot. Split it into several lines (ideally one command per line):
IF not exist %directoryPath% (
echo Invalid directory.
goto :InvalidDirectory
) ELSE (
echo Sencha app build development started..
)
If you insist on doing it in one line, & is the proper way to chain two commands:
IF not exist %directoryPath% (echo Invalid directory. & goto :InvalidDirectory) ELSE (echo Sencha app build development started..)
I have created a batch script that imports a virtual box appliance and configures it for that machine. Part of the script deletes the share (between the guest and host) and creates it again with a new host path like this:
"%vbPath%VBoxManage" sharedfolder remove "%devBoxName%" --name wwwshare
"%vbPath%VBoxManage" sharedfolder add "%devBoxName%" --name wwwshare --hostpath "%shareName%"
When I put these lines of code into a separate file and set shareName directly above, it works fine (eg. shareName=c:/test). However, when I run it as part of the script, the share that is created would have c:/test" as the share path, which of course means it fails.
This is all the code that is used for this variable:
setlocal enabledelayedexpansion
:: Check they want the default share folder name
SET shareName=C:\ld-sd%devNum%-rh7-www\
:getShareName
SET /P shareNameOK="Your shared folder will be %shareName%. Do you want to keep this value (enter y to keep and n to change): "
cls
if /I "%shareNameOK%"=="n" (
SET /P shareName="Please enter the path and name you would like for your shared folder: "
GOTO :getShareName
) else (
if /I not "%shareNameOK%"=="y" (
echo WARNING: You did not enter y or n
GOTO :getShareName
)
)
:getShareCheck
:: Check if this folder exists and if they plan to keep or overwrite it if it does
if /I exist "%shareName%" (
echo WARNING: Selecting yes will result in all website files being erased!
SET /P shareNameOverwrite="This folder already exists, would you like to overwrite the files in it with a clean copy (yes/no)? "
if /I not "!shareNameOverwrite!"=="no" (
if /I "!shareNameOverwrite!"=="yes" (
#RD /S /Q "!shareName!"
timeout /t 1 /nobreak > NUL
mkdir "!shareName!"
) else (
echo WARNING: You did not enter yes or no - NOTE full name
GOTO :getShareCheck
)
)
) else (
SET shareNameOverwrite=yes
mkdir "!shareName!"
)
Whenever I echo the variable (anywhere in the file) it shows no quotations in it. This is the only point it causes an issue. I have even echoed the entire line in question:
echo "%vbPath%VBoxManage" sharedfolder add "%devBoxName%" --name wwwshare --hostpath "%shareName%"
and it shows fine. Where are the quotation marks coming from?
The problem ended up being the line:
SET shareName=C:\ld-sd%devNum%-rh7-www\
should have been:
SET shareName=C:\ld-sd%devNum%-rh7-www
I had missed that there was a \ on the end.
The result of this is that with a \" it will take the " as part of the string and not the end of the string.
So if ever you have a rouge " at the end of a variable, make sure your variable does not end in a backslash!
#echo off
rem setting folder variables
set "mymov=D:\Movies\My"
set "hermov=D:\Movies\Her"
set "themmov=D:\Movies\Them"
set folders=%mymov% %hermov% %themmov%
set ext=".mp4" ".mov" ".avi"
set test=0
for %%f in (%folders%) do (
echo "searching in %%f"
cd /D %%f
for %%i in (*.*) do (
for %%b in (%ext%) do (
if "%%~xi"==%%b (
set test=1
echo "match_found_not_deleting %%f")
)
)
if %test%==0 (
echo deleting %%f
rd /s /q %%f
)
)
I am trying to delete the folders stored in %%f but as my batch file is processing it I get an error that it is being used in some other process.
How can i fix it?
Please help.
Windows thinks that another application needs that folder and will not allow deletes until the other application is done using it. Try these items to find what else is accessing your folder.
check if you have another command window or windows explorer that is in (or under) that folder.
Check if you have a application open that is using a file in (or under) that folder.
Stop all the applications that are not absolutely needed.
if that doesn't work, reboot the computer, and only start the applications you need.
Have a weird issue, firstly am using batch files to install applications..the installations are working fine without any issue.
I have at the end of the batch if the error level is NEQ 0 output the error code to a file, but the output is always 9009.
From searching the web for 9009 it says that the executable cannot be found, but surely it can as the application installs fine.
Here is a sample of one of my batch scripts to install:
IF exist %windir%\LogFolder\BoxSync4.0x86.txt ( goto eof ) ELSE ( goto BoxSyncInstall )
:BoxSyncInstall
msiexec /i "\\servername\InstallFolder\BoxSync\SyncMSI32.msi" /qn
if %ErrorLevel% EQU 0 (
>>"\\servername\gpolog\BoxSync4.0x86.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Box Sync 4.0 x86 Installed"
>>"%windir%\gpologs\BoxSync4.0x86.txt" echo "Box Sync 4.0 x86 Installed"
)
else if %ErrorLevel% NEQ 0(
>>"\\servername\gpolog\BoxSyncErrorsx86.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Error trying to install/upgrade to BoxSync4.0x86"
)
:eof
Does anyone have any ideas why I might be getting this error constantly?
Thanks
Mikoyan
Try using setlocal enabledelayedexpansion at the start of your batch file, and !ERRORLEVEL! inside your IF.
See also:
ERRORLEVEL inside IF
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/setlocal.mspx?mfr=true
http://batcheero.blogspot.ru/2007/06/how-to-enabledelayedexpansion.html
The problem is syntax, you need a space between your 0 and (, like so:
IF exist %windir%\LogFolder\BoxSync4.0x86.txt ( goto eof ) ELSE ( goto BoxSyncInstall )
:BoxSyncInstall
msiexec /i "\\servername\InstallFolder\BoxSync\SyncMSI32.msi" /qn
if %ErrorLevel% EQU 0 (
>>"\\servername\gpolog\BoxSync4.0x86.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Box Sync 4.0 x86 Installed"
>>"%windir%\gpologs\BoxSync4.0x86.txt" echo "Box Sync 4.0 x86 Installed"
)
else if %ErrorLevel% NEQ 0 (
>>"\\servername\gpolog\BoxSyncErrorsx86.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Error trying to install/upgrade to BoxSync4.0x86"
)
:eof
EDIT: If the installer needs admin privileges and this is a Win8+ OS, you might have security restrictions, in that case, copy the msi installer into the %TEMP% folder and run it from there. The reason this happens is because when you run the command prompt in Administrator mode, it restricts use of unc paths (for "security" reasons).
Try also pushd and popd as #dbenham keeps reminding us: LINK
found where the issue lied it is under the ELSE IF as I watched the install script run by not being silent.
After removing ELSE the script ran fine. Thanks again for your help as you have guided me with other tricks.