else inside a for loop batch
I’m trying to make a for /d loop but for some reason I can’t put an else statement in a for loop. I can put just an if inside a for loop but no if else what am I doing wrong
for /D %%i in (*) do ( if exist %%~fi%\Desktop.ini ( echo file found ) esle ( echo file not found ) )
See also questions close to this topic
Batch file I made to keep track of archery scores keeps crashing after entering 3 scores
:inputScores ( if %arrowInputCount% gtr %totalNumOfArrows% ( goto output ) if %updateCount% == true ( set /a arrowInputCount+=1 ) else ( set updateCount=true ) cls echo Input the score for arrow %arrowInputCount%^/%totalNumOfArrows% echo Score range: 1-%bullseyeWorth% echo Input 'x' for an X echo Input 'm' for a miss set /p tempScore=Input: :testScore ( if %tempScore% == x ( rem user entered x if %scoreString% == null ( set scoreString=%tempScore% set /a totalScore+=%bullseyeWorth% goto inputScores ) else ( set scoreString=%scoreString%^,%tempScore% set /a totalScore+=%bullseyeWorth% goto inputScores ) ) else if %tempScore% == m ( rem user entered m if %scoreString% == null ( set scoreString=%tempScore% goto inputScores ) else ( set scoreString=%scoreString%^,%tempScore% goto inputScores ) ) else ( if %tempScore% gtr 0 if %tempScore% leq %bullseyeWorth% ( rem this will handle if the user enters a number echo stub pause exit ) else ( rem user didn't enter a valid number echo - echo Invalid Input pause set updateCount=false goto inputScores ) ) ) ) :output ( rem program outputs the data cls echo stub pause exit )
This is the offending fragment of the code for my program that is supposed to take in archery scores and output a file tat contains all the scores along with other data such as score % out of total possible score.
For the purposes of this fragment working, assume that these variables have already been set to these values:
arrowInputCount=0 totalNumOfArrows=9 updateCount=0 bullseyeWorth=5 scoreString=null
When I run the file it works fine up until I enter the third score at which point it crashes. I have done a desk check of the whole thing and I can't see why it would work fine for the first 2 scores entered and then crash once I enter the third one. I haven't written code to handle when the user enters a number yet so in my testing I was only entering 'x' and 'm'. I have tried entering the first 3 scores as all x's, all m's and a combination of x's and m's. In all cases it crashes after entering the third score.
Explanation of error using "color" in a batch script
In order to set the color of a batch script's console/terminal,
colorcan be used. E.g.
color 70. However, in order to reset the color of a the console,
colorwithout any arguments/values can be used. What is causing confusion for me is why it only works inside the command prompt or a
called script but not a script started specifically with
cmd /c. It fails and returns an errorcode of
1. Is there some legacy reason for this or is it a bug in Windows?
cmd /c color || echo foobar
cmd /c color 70 || echo foobar
call color || echo foobar
Cron-Job for maintaining multi task that can be identified by UserID
I am writing a logic where i need to run multiple jobs for different user, but when i am trying to stop it stop all job as its not identifiable. If anyone have different methodology to proceed, it will be highly appreciated.
Python: navigate to directory, and insert command line on cmd window inside Python
I've read the documentations that other stackoverflow posts provided, but I couldn't understand what they are saying.
Here is what I want to achieve: I want to navigate to a specific directory that contains a specific executable file, and insert a command line.
.\> cd C:\Program Files\MongoDB\Server\3.4\bin C:\Program Files\MongoDB\Server\3.4\bin> mongoimport -h <IP_ADDRESS> -d <DB> -c <COLLECTION> -u <USER> -p <PASSWORD> --drop --file C:\data.txt
I need a code will perform the two lines of code above inside Python. In .\3.4\bin directory, there is an executable file called 'mongoimport.exe'. And on the SAME LINE, I need to insert additional sentences to specify the external server I want to access.
How can this be done?
Extracting variable from array for use in command on CMD
I am trying to make a CMD batch script that constantly logs IP statistics and puts them in different .txt files, for this I have created 2 arrays, one that contains the names I wish to give to the txt files and other one that contains the ips I wish to ping.
I try to loop the elements in the arrays as names/parameters for different commands on the CMD shell, the part for the .txt names works but I can't get extracting the ip addresses from the corresponding array and passing them to the ping command to work. My code is:
@echo off set Noms=(bni csc cba lpz oro pnd pts scz trj) set Dirs=(18.104.22.168 22.214.171.124 126.96.36.199 188.8.131.52 184.108.40.206 220.127.116.11 18.104.22.168 22.214.171.124 126.96.36.199) rem -------------------------------------------------------------- rem Builds .txts with names in list Noms pinging Addresses in Dirs rem -------------------------------------------------------------- for %%j in %Noms% do ( set direccion = %Dirs[%%j]% ping /n 3 %direccion% > c:\log\p0.txt findstr "Media" c:\log\p0.txt > c:\log\p1.txt for /f "tokens=3 delims=," %%i in (c:\log\p1.txt) do @echo %%i > c:\log\p2.txt for /f "tokens=4 delims=<equal> " %%i in (c:\log\p2.txt) do @echo %%i > c:\log\p0.txt for /f "tokens=1 delims=m" %%i in (c:\log\p0.txt) do (set pingvar=%%i) for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b) For /f "tokens=1-3 delims=/:," %%a in ("%TIME%") do (set mytime=%%a:%%b:%%c) set totvar=%mydate% %mytime% %pingvar% echo %totvar% > c:\log\%%j.txt )
I found no mistake in my code, basically I try to put the IP address from the array "Dirs" in the variable "direccion" and then passit as an argument to ping.
Thank you for helping me.
Check for changes in specifics files with batch not working as expected
I have differents files, and I want to check for any changes in any of them, using batch. This is what I have:
@echo off setlocal enableDelayedExpansion set "list=test.txt test1.txt" :loop timeout -t 1 >nul for %%j in (%list%) do ( REM Here only prints test.txt echo %%j for %%i in (%%j) do echo %%~ai|find "a">nul || goto :loop echo file was changed %%j attrib -a %%j ) goto :loop
If I use only one file, works, but not with two or more. The idea is, that
echo file was changed %%jonly outputs the correct file, and not both.
If I do some changes:
@echo off title LAN chat reader setlocal enableDelayedExpansion set "list=test.txt test1.txt" :loop timeout -t 1 >nul for %%j in (%list%) do ( echo %%j echo %%~aj|find "a">nul || goto :loop echo file was changed %%j attrib -a %%j ) goto :loop
works, but only alerts me the changes in the second file, after I make changes in the first. I can understand that the problem is in the loop, so there is where I'm confused.