I have a bat file to create folder.
:X3Main
IF EXIST "%1%\JBoss" goto Test
IF EXIST "%1%\DB" goto DB
goto end
:Test
mkdir "%DESTINATION%\IX3\COSMIC\JBoss"
goto end
:DB
mkdir "%DESTINATION%\IX3\COSMIC\DB"
goto end
here first folder is created successfully(IF EXIST "%1%\JBoss" goto Test) but second function is not working. If I remove first function then second function is working.
Please can you someone explain reason behind this?
Some extra info: In the NT line of Windows for a reliable test for a folder you need to end the foldername with a backslash
and you may have meant %~1 instead of %1%
IF EXIST "%~1\JBoss\" do task
and in your case you can always use this which will do nothing if the folder already exists: the 2>nul eliminates a harmless error message when the folder exists.
mkdir "%DESTINATION%\IX3\COSMIC\JBoss" 2>nul
mkdir "%DESTINATION%\IX3\COSMIC\DB" 2>nul
try this:
:X3Main
IF EXIST "%1%\JBoss\" call:Test
IF EXIST "%1%\DB\" call:DB
goto :eof &rem or goto:end or where you want
:Test
mkdir "%DESTINATION%\IX3\COSMIC\JBoss"
goto :eof
:DB
mkdir "%DESTINATION%\IX3\COSMIC\DB"
goto :eof
use call to return from function and goto:eof to end a function.
Edited concerning foxidrive's answer.
Related
I have a below batch file where if both folder1 and folder2 both exist then I want to execute else don't want to do anything.
Here even my folder NOT exist, I am getting echo message Both folders exist. What I am doing wrong here?
#ECHO OFF
set folder1="C:\Temp1"
set folder2="C:\Temp2"
IF EXIST %folder1% IF EXIST %folder2% goto bothfound
:bothfound
echo Both folders exist.
goto end
:end
echo Done.
pause
This is because you don't have a GOTO to bypass the :bothfound block. After the if exist statements, it continues on because it never invoked the goto. After you do your if exist, you need a goto end or a goto to a not found block.
example:
IF EXIST %folder1% IF EXIST %folder2% goto bothfound
goto end
:bothfound
For the life of me, I can't figure out why the below set prompt won't work when it is in the if statement:
#echo off
REM :askdeletecsvs
if exist *.csv (
echo Warning! All files in the scripts folder that have the "CSV" extension will be deleted!
echo Answering "n" will continue the script without deleting the CSVs.
set /p ASKDELETE=Delete CSVs? (y/n):
REM
REM if ( /i %ASKDELETE% equ "y" goto :deletecsvs )
REM if ( /i %ASKDELETE% equ "n" goto :runscripts )
REM goto :askdeletecsvs
)
When I run the batch file as it is above the cmd window opens and then shuts quickly. If I move the set line outside of the if statement then the prompt shows as expected. (There are csvs in the folder the bat file is running from)
What am I missing?
To start with you had used a closing parenthesis which was prematurely ending your opening If parenthesis.
I'd suggest reversing the thinking:
If Not Exist *.csv GoTo runscripts
Echo Warning!
Echo All files in the scripts folder that have the "CSV" extension will be deleted!
Echo Answering "N" will continue the script without deleting the CSVs.
Choice /M "Delete CSVs"
If ErrorLevel 2 GoTo runscripts
:deletecsvs
Del /F /Q /A "PathTo\scripts\*.csv"
GoTo :EOF
:runscripts
You can change GoTo :EOF to a relevant valid label as necessary or remove it if you want to continue on to :runscripts. You can also replace PathTo\scripts\ with %~dp0 if the batch file is running from the scripts directory, or remove PathTo\scripts\ if the current directory holds those files. (note that the current directory and batch file path may not necessarily be the same)
I would like to delete a specific file if this one is empty by the use of a windows .bat file. Here is my non-working script:
if %~z1 == 0
echo %~z1===================
del "%1"
goto :eof
if %~z1==0 (
echo %~z1===================
del "%1"
)
goto :eof
The goto should be placed inside the parentheses if you want to go to end-of-file having deleted the file. As I've placed it, in the absence of further information, the batch would terminate regardless.
So basically I want to create a batch script that can run any notepad file which the user specifies. I tried this...
#Echo Off
SET /P ANSWER=What is the name of the file to open?
IF /i (%ANSWER%)==('FIND /i "*.txt" %ANSWER%) (goto :Filename)
goto :exit
:Filename
Start *.txt
EXIT
:exit
ECHO FAILLLLLLLL
PAUSE
EXIT
The issue here is the first IF statement. I know its wrong. But, I don't know how to specify the entry of any filename. A different way to do this task is also appreciated.
Thanks for help :)
If your goal is simply to open a file that the user specifies in Notepad, the following works for me in Windows 7:
#echo off
set /P answer=What is the file name?
if exist %answer% (
start notepad.exe %answer%
) else (
echo Unable to locate %answer%
)
I am playing around with the IF EXIST batch file command but ran into a scenario.
What i am trying to do is
IF EXIST C:\Windows\system32 call batchfile2
IF EXIST C:\WINNT\system32 call batchfile3
But there are scenarios where both directories exist on PCs if win2k was upgraded to XP instead of a fresh XP install. What i want it to do if it detects both directories is to "do nothing" since the first two options above already takes care of what I want to do. Can someone tell me how i can manipulate this?
Besides the above, I believe I can also call subroutines within the same batch but how can I create a subroutine to end the script if it detects both "Windows\system32" and
"WINNT\system32"?
IF EXISTS C:\Windows\system32 goto sub1 else goto sub2
:sub1
:sub2
Many thanks in advance.
I'm not sure when exactly you want which option to execute, but you can combine gotos and labels as much as you want. A bit elaborate, maybe, but at least structured:
#echo off
IF EXIST C:\Windows\system32 goto windowsfound
:afterwindows
IF EXIST C:\WINNT\system32 goto winntfound
:afterwinnt
goto end
:windowsfound
IF EXIST C:\WINNT\system32 goto bothexist
echo Windows folder found, do something.
call batchfile2
goto afterwindows
:winntfound
echo WINNT folder found, do something.
call batchfile3
goto afterwinnt
:bothexist
echo Both folders already exist.
goto end
:end
echo Exiting.
I think it would be possible to check for both on one row as well:
#echo off
IF EXIST C:\Windows\system32 IF EXIST C:\WINNT\system32 goto bothfound
IF EXIST C:\Windows\system32 goto windowsfound
IF EXIST C:\WINNT\system32 goto winntfound
:windowsfound
echo Windows folder found, do something.
call batchfile2
goto end
:winntfound
echo WINNT folder found, do something.
call batchfile3
goto end
:bothexist
echo Both folders already exist.
goto end
:end
echo Exiting.
One simple way is:
if exist c:\windows\system32 if exist c:\winnt\system32 goto morestuff
if exist c:\windows\system32 call batchfile2
if exist c:\winnt\system32 call batchfile3
:morestuff
...
you can remove the "#ECHO OFF" ... the REM are just comments in the file.. and the ECHO are just things it outputs.. (if you delete the echo off it will show all of it..)
essentially, you can jump to different sections of the file with the goto statment.. you just reference a goto label.. and then later in the file use a colen and the label name as the anchor/target/label...
#ECHO OFF
REM Check to see if windows\system32 exists.. if so skip to the part 2 section
IF EXIST C:\WINDOWS\system32 goto parttwo
REM if windows\system32 didnt exist, it will check for the other dir...
IF EXIST C:\WINNT\system32 goto partthree
REM if we get to this point.. neither directory existed... so skip to a message about that
goto neither
:parttwo
echo windows\system32 existed
REM because it was not checked earlier, check to see if the second directroy exists
IF EXIST C:\WINNT\system32 goto end
echo windows\system32 existed, but winnt\system32 does not...
echo do or call whatever for part 3....
goto end
:partthree
echo winnt\system32 existed
echo do or call whatever for part three
goto end
:neither
echo Could not find windows or winnt \system32
:end
echo goodbye
You can always hit up MS for more info:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true