Using the goto command within a batch easily allows a user to loop or restart a batch file after it has been completed. Below are some examples of how this command can be used. This page was created with the easiest, but not necessarily recommended solution first, to the most difficult solution but recommended method last.
@echo off
cls
:start
echo This is a loop
goto start
In this first example, the computer will print “This is a loop” over and over until you terminate the file. To cancel this example press: CTRL + C.
@echo off
cls
:start
echo This is a loop
pause
goto start
Next, adding the pause statement before the goto line will prompt the user to press any key before looping the batch file. This is helpful and recommended to help prevent the computer from utilizing all or most of its resources in having to continuously rerun the loop and will allow the user to rerun the batch when they’re ready.
@echo off
cls
:start
echo This is a loop
set choice=
set /p choice=”Do you wish to restart? Press ‘y’ and enter for Yes: ”
if not ‘%choice%’==” set choice=%choice:~0,1%
if ‘%choice%’==’y’ goto start
Finally, in this last example and most recommend method, the user would be prompted if they wish to rerun the batch file. Pressing “y” would use the goto command and go back to start and rerun the batch file. Pressing any other key would exit the batch file. This example is for Windows 2000, XP and later users if you’re running earlier Windows 98 or earlier you’d need to use the choice command.
Note: Replacing the “echo This is a loop” line with your batch file will allow any of your batch files to loop or rerun.
July 4th, 2009 | Posted in Uncategorized | No Comments
In some situations you may want to run a batch file in full screen.
1. Create a shortcut to the batch file.
2. Once the shortcut has been created right-click it and select Properties.
3. Within the Properties window click the Options tab.
4. In Options select Full screen under the Display options and click Ok.
5. If the batch file is now ran from this shortcut it will run in full screen.
July 4th, 2009 | Posted in Uncategorized | No Comments
Using the Windows mplay32.exe or mplayer.exe file included with Microsoft Windows allows you to play Windows sound files. Depending on your version of Windows will depend on what file you’ll run. Windows 95 and 98 users will be using mplayer instead of mplay32 as shown in the below examples.
As can be seen in the example below we’re using mplay32 as the program to play the file. The /play and /close switches are used to tell the program to play the file once open and then close when done. Finally, the remainder of the line is the full path to where the audio file is stored. In this example we’re playing the Windows chimes file.
mplay32 /play /close c:\windows\media\chimes.wav
If the file and/or directory has a space in it make sure to include the full path in quotes as shown below.
mplay32 /play /close “c:\windows\media\windows xp error.wav”
Finally, this media player is only capable of playing .wav, .mid, *.cda, *.avi, *.asf files. If you’re trying to play .mp3 files this program will not work. If you wish to play other media files you’ll need to use an alternative command line media player that is not included with Windows.
Alternatively you can use the start command to start the program associated with the file. For example, if you wanted to open the player to play “music.mp3″ in the command line or batch file you could type the below command.
start music.mp3
Unfortunately this method would only open the file in the default player associated with .mp3 files and because the Windows player would not have a switch to automatically start the file it would not play the file until you clicked play.
July 4th, 2009 | Posted in Uncategorized | No Comments
There are a few different methods of how this can be done. Below is an example of how you could use the date command within the for command to extract the current date and use that data to rename the file. Each of the for commands listed in this document would be placed into a batch file.
Date
for /f “tokens=1-5 delims=/ ” %%d in (”%date%”) do rename “hope.txt” %%e-%%f-%%g.txt
Below is a breakdown of the above command and what it all means.
- for /f - The for command and the /f switch.
- “tokens=1-5 delims=/ “ - How many tokens the incoming data (in this case the date) will be broken into; 1-5 is five different tokens. Finally, delims is short for delimiters and is what is used to break up the date, in this example the / (forward slash) and a space (space before the quote).
- %%d - The beginning character used for the token. Since there are 5 tokens in this example it would be d,e,f,g, and h.
- in (”%date%”) - The data being used, in this case the %date% is the current date of the computer.
- do - What the for command will do. The rename command can be substituted for anything else.
- rename “hope.txt” %%e-%%f-%%g.txt - rename the file “hope.txt” to the tokens e,f, and g with a .txt file extension. This example also has a - (hyphen) in-between each token to separate the month, day, and year in the file name.
When %date% is used in a batch file it displays the date in the following format: Sun 09/02/2007 this command breaks this date into the tokens: “Sun” (%%d), “09″ (%%e), “02″ (%%f), and “2007″ (%%g).
In this example using the above date mentioned hope.txt would be renamed to 09-02-2007.txt.
Time
for /f “tokens=1-5 delims=:” %%d in (”%time%”) do rename “hope.txt” %%d-%%e.txt
This command is very similar to the above example. However, instead of using the forward slash and space to break up the data we’re using a : (colon) because the time is split up with this character. Finally, because we’re renaming the file to only the hour and minute this example is only using the d and e token. Additional information about what everything in this line means is found in the above date example.
When %time% is used in a batch file it displays the time in the following format: 19:34:52.25, this command breaks this time into the tokens: “19″ (%%d), “34″ (%%e), and “52.25″ (%%f).
In this example using the above time mentioned hope.txt would be renamed to 19-34.txt.
July 4th, 2009 | Posted in Uncategorized | No Comments
Microsoft Windows users can run batch files or other files in a minimized window by using the command prompt start command. This is useful for when a user needs to run a batch file but doesn’t want the user to interrupt its operation. Below is an example of how the start command can be used to start the batch file myfile.bat as a minimized window.
start /min myfile.bat
Additional information about this command and other options it’s able to perform can be found on our start command page.
Note: If the batch file does not end with the exit command it will keep an MS-DOS command prompt window open after the batch file is closed. If you wish for the user to not have to hassle with manually closing the window make sure to add this at the end of your batch file.
July 4th, 2009 | Posted in Uncategorized | No Comments
With the introduction of Microsoft, Microsoft has included Scheduled Tasks, a software program designed to run any program, including batch files, at any time or any schedule you desire. To run Scheduled Tasks, follow the below steps.
- Click Start
- Click Programs
- Click Accessories, System Tools, and then Scheduled Tasks.
Once open, you’ll be able to create your own custom task by clicking Add Scheduled Task, which will start you through the wizard, prompting you with what program you wish to execute, how often you wish to run it (daily, weekly, monthly, one time only, when my computer starts, and when I log on), the time you wish to run, and how often each day to run.
July 4th, 2009 | Posted in Uncategorized | No Comments
Shut down the computer
C:\Windows\RUNDLL32.EXE user,exitwindows
exit
NOTE: When typing the above two lines, spacing is important. It is also very important that the exit line be placed into the batch file as many times Windows may be unable to restart the computer because of the open MS-DOS window.
July 4th, 2009 | Posted in Uncategorized | No Comments
Restarting the computer
START C:\Windows\RUNDLL.EXE user.exe,exitwindowsexec
exit
NOTE: When typing the above two lines, spacing is important. It is also very important that the exit line be placed into the batch file as many times Windows may be unable to restart the computer because of the open MS-DOS window.
July 4th, 2009 | Posted in Uncategorized | No Comments
A .BAT file can very easily be made into a .EXE or .COM file. This can help give you additional abilities such as file versions and keeping the program hidden from the user and/or keep the source of the batch file hidden from the user. We recommend users use the bat_to_exe_converter file to convert their batch files. This utility is free and can be found on our utility download page.
Once this file has been downloaded unzip the file and double-click the bat_to_exe_converter.zip file to open the program. Within this program you’ll be able to point to the batch file you wish to convert into an executable file, include additional files, change the icon of the file, and add addition details such as the file version, company, copyright, etc.
* If you want to keep the file hidden from the user to prevent them from closing it before the file can be completed make sure sure to check the Ghost application option. However, make sure all pause statements are removed from the batch file or it will remain in memory as b2e.exe.
Alternative solution
In addition to the above solution for users running earlier versions of MS-DOS and windows can use the bat2exe.com file, which is program that converts your batch files into a .com file. Which is executable and also impossible for a user to view the contents or commands ran in the batch file. This utility can also be found on our utility download page.
Once the file has been downloaded move the file into the same directory as your batch file and type a command similar to the below example.
bat2exe myfile.bat
July 4th, 2009 | Posted in Uncategorized | No Comments
It may be necessary to rename several files to allow compatibility with another program. A good example where this could be used is renaming an ASP file to an HTM or HTML file.
Use the below command at the MS-DOS prompt or within a batch file.
xcopy *.shn *.wav
This would rename all files with .shn to .wav, while keeping the original files.
If you simply wish to rename the extensions without keeping the original file you can also use a command similar to the below example.
rename *.shn *.wav
See our ren and rename command and/or xcopy command pages for additional information about these commands.
July 4th, 2009 | Posted in Uncategorized | No Comments