IF Statement

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

IF Statement

#1 Post by sp11k3t3ht3rd » 22 May 2010 13:45

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: IF Statement

#2 Post by aGerman » 22 May 2010 16:43

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

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: IF Statement

#3 Post by sp11k3t3ht3rd » 22 May 2010 17:49

You aGerman are AMAZING! :D

Post Reply