Working with Batch variables and For loops


By: pball
October 3, 2008

I though I would write a how to about using variables in batch. I have learned many things from people on this site and though I would try and help out.

How to set a variable

set var=value

var is the variable name and value is what you are setting it too. It is good to keep variable names short and meaningful. Just numbering or lettering them could make it hard to read your script later.

---------------------------

How to have the user input a value

set /p var=

This will stop the program and wait for something to be entered by the user, hitting the enter key will enter that value and continue the program.

---------------------------

List of common environment variables

Í% - expands to the current directory string.
ÚTE% - expands to current date using same format as DATE command.
%TIME% - expands to current time using same format as TIME command.

---------------------------

Editing the length or choosing certain characters in a variable.

If you have the name of a file set as a variable but you only want to use the name without the file extension this is one way to do it.

Lets say we have pball's guide.txt as our variables value but we want to return the value pball's

Note: The first character in variable is 0

%var:~0,7% returns pball's (starts at character 0 and displays the first 7 characters)

%var:~0,-4% returns pball's guide (starts at character 0 and removes 4 character from the end)

%var:~-9% returns guide.txt (displays the last 9 characters)

%var:~8,5% returns guide (starts at character 8 and displays 5 characters)

Those are some common ways to get part of a varible. The starting character can be changed as noted in the last example.

---------------------------

Now how to to replace things.

This method can be used to replace parts of file names to replacing something in a text based file.

Here is an example where part of file name is replaced

set C=pball's guide.txt
set B=%C:pball's=Batch script%
ren "%C%" "%B%"

The first line sets the current file name to the variable C

The second line sets the variable B to C with pball's being replaced by Batch Script

After the colon is the text to find, then after the equals sign is the text to replace it with.

The last line renames the variable C to the variable B, the quotes are needed since there are spaces in the file name.

Now an example to replace text in a text file.

I made text file with my name (pball) in it a couple times on different lines. But now I want to replace pball with some guy. What am I to do.

setlocal EnableDelayedExpansion
for /f "tokens=*" %%a in (pball.txt) do (
set C=%%a
set B=!C:pball=some guy!
echo !B! >> pball2.txt
)

This works on the same premise of replacing text except it uses a for loop to get each line in the file one at a time.

The first line is to turn on the option to use the exclamation points with variables, this is very important since this allows the variables to be updated each time the loop runs.

The next line will set the first line in the text file pball.txt equal to %%a

Then C is set equal to %%a

Then B is set equal to C with the value pball replaced by some guy

The last line then writes B to the file pball2.txt ( the >> means it will add as a new line if the file exists)

The loop will then set the second line in pball.txt equal to %%a and so on till it's read every line in that file.

---------------------------

Create a text file with a list of all of the files in a folder

for /f "tokens=*" %%I in ('dir /b *.txt') do (

echo %%~I
echo expands I removing any surrounding quotes (")

echo %%~fI
echo expands I to a fully qualified path name

echo %%~dI
echo expands I to a drive letter only

echo %%~pI
echo expands I to a path only

echo %%~nI
echo expands I to a file name only

echo %%~xI
echo expands I to a file extension only

echo %%~sI
echo expanded path contains short names only

echo %%~aI
echo expands I to file attributes of file

echo %%~tI
echo expands I to date/time of file

echo %%~zI
echo expands I to size of file
)

Each different line outputs a different thing. This will currently list all the txt files in the folder where the script is. You can pick and choose what you would like and make it echo it to a file.

This following script will list the name, ext, size, and date for all files in the folder.

Note: notice the *.* that means any file name and any extension

for /f "tokens=*" %%I in ('dir /b *.*') do (
echo %%~nI %%~xI %%~zI %%~tI >> list.txt
)

The list may not look to organized since the file names could be really long so nice columns won't be made.

---------------------------

This is a simple way to get a user make a choice.

This example will let the user choose the "subprogram" that they wish to run.

:again
echo 1. Option 1
echo 2. Option 2
echo 3. End Script
set /p choice=
if [%choice%]==[] goto again
if [%choice%]==[1] goto 1
if [%choice%]==[2] goto 2
if [%choice%]==[3] goto end
goto again

:1
do something here
goto end

:2
do something here
goto end

:end

This will write each option to it's own line and if 1, 2, or 3 isn't entered it will ask again.

---------------------------

Delete a batch file when if finishes.

on the last line type del %0
this will delete the batch file at that point, so make sure it's the last line and don't use it till you know the script works.

---------------------------

Please pm if you find a problem or a better way to word something ( I'm not the best with words) or have simple questions

I will add more to this when I find more to add


Need more help?
Describe your Problem
Example: Hard Drive Not Detected on My PC

Ask Question