Batch File That Only Runs Certain Code Upon First Use

by jackrabbit898 in Circuits > Software

753 Views, 9 Favorites, 0 Comments

Batch File That Only Runs Certain Code Upon First Use

0c1eb9d6b39292f5abd2529ed3b82eec.png

Hello. It was hard to explain in the title, but basically what this does is makes a batch file run certain code, but it only runs it the first time the file is opened. It works by creating a text file and checking for it. This can be useful to make tutorials that only open the first time a user runs the file.

Create the File

1a20c5dc93281ad15c007e8380a5cdd0.png

Pretty obvious. Just create a new file and make the file extension '.bat'

Now open it in an editor.

The Code

b395282a858f4b961ee68c0498359938.png

Here is the code. In this example, it will tell a tutorial the first time the file is opened, but after that it'll just go straight to the main menu. NOTE: You'll probably want to put the batch file in a folder so it can create the text file in the folder as well. Just helps keep it organized.

@echo off

if not exist first.txt goto tutorial

if exist first.txt goto mainmenu

:mainmenu

cls

echo This is the main menu. If this opened immediately when you ran this file, you've opened this file before.

pause

exit

:tutorial

echo don't delete this! >first.txt REM this line creates the file so next time

cls

echo Welcome! This is your first time running this file! Next time you run this, you won't see this menu.

pause

exit