Page 1 of 1

IF Statement

Posted: 22 May 2010 13:45
by sp11k3t3ht3rd
From this previous code i have written i want to add and if statement:

Code: Select all

@echo off
cls
title Java Code Generator
echo An example of an item number would be: 995
set /p itemNumber=Please type the item number here:
pause
cls
echo The number of items can range from 1 to 999999999 in most cases.
set /p numberOfItem=Please type the number of that item here:
echo addItem(%itemNumber%,%numberOfItem%); >> AddItemCode.txt
pause
cls
echo Your code has been place in a .txt file named AddItemCode.txt. You will find this file in the directory this program is in.
pause
cls
start AddItemCode.txt
echo The file has been opened.
pause
exit

I want the program to check if the file AddItemCode.txt exists and if it does then make the code go to AddItemCode2.txt and if it doesn't exist create it. Is this possible?

Re: IF Statement

Posted: 22 May 2010 16:43
by aGerman
This should work too in case AddItemCode2.txt would exist etc.

Code: Select all

@echo off

if not exist AddItemCode.txt (
  set "filename=AddItemCode.txt"
) else (
  set /a n=1
  call :createNewName
)

cls
title Java Code Generator
echo An example of an item number would be: 995
set /p itemNumber=Please type the item number here:
pause
cls
echo The number of items can range from 1 to 999999999 in most cases.
set /p numberOfItem=Please type the number of that item here:
echo addItem(%itemNumber%,%numberOfItem%); >> %filename%
pause
cls
echo Your code has been place in a .txt file named %filename%. You will find this file in the directory this program is in.
pause
cls
start %filename%
echo The file has been opened.
pause
exit

:createNewName
set /a n+=1
if exist AddItemCode%n%.txt goto :createNewName
set "filename=AddItemCode%n%.txt"
goto :eof



Regards
aGerman

Re: IF Statement

Posted: 22 May 2010 17:49
by sp11k3t3ht3rd
You aGerman are AMAZING! :D