Close Menu
Computing.net
    Facebook X (Twitter) Instagram
    Computing.netComputing.net
    • News
      1. AI
      2. Crypto
      3. Gaming
      4. Hardware
      5. Security
      6. Software
      7. View All

      Anthropic’s COBOL Automation Tool Triggers IBM Stock Plunge and Crypto Market Decline

      February 24, 2026

      AI Trading Bot Loses $441K in Crypto After Decimal Point Mistake

      February 23, 2026

      Tesla (TSLA) Stock: Goodbye Sedans, Hello Robots in Dramatic Production Shift

      January 29, 2026

      Palantir Technologies (PLTR) Stock: Why Bears May Be Wrong About Valuation Concerns

      January 29, 2026

      SUI Token Rallies 40% Following Major Staking Event and CME Futures Announcement

      May 12, 2026

      Chainlink (LINK) Surges to $10.40 as Network Activity Hits Eight-Month Peak

      May 12, 2026

      Dogecoin Whales Ramp Up Accumulation as DOGE Eyes Critical Breakout Levels

      May 12, 2026

      Bitcoin Holds $81K While Burry Flags Nasdaq Bubble and Oil Surges Past $105

      May 12, 2026

      Hamster Kombat: Unraveling TON’s Gaming Phenomenon

      August 7, 2024

      W-Coin: Exploring the Latest Telegram Tap-to-Earn Phenomenon

      August 7, 2024

      Hamster Kombat: 300 Million Players & Counting, HMSTR Token Airdrop Soon!

      July 31, 2024

      Hamster Kombat Developers Work with TON Team on Airdrop Solution

      July 30, 2024

      Nothing Expands Product Line with New AI Feature & Phone Update

      July 31, 2024

      Security Audit Reveals Concerns in Atari’s Blockchain Game on Base

      August 6, 2024

      SideWinder Group Targets Maritime Facilities in New Cyber Espionage Campaign

      July 30, 2024

      OAuth Implementation Flaw Exposes Millions of Websites to XSS Attacks

      July 30, 2024

      Hamster Kombat Players Face Growing Cybersecurity Threats

      July 25, 2024

      Anthropic’s COBOL Automation Tool Triggers IBM Stock Plunge and Crypto Market Decline

      February 24, 2026

      Cookie Crumble: Google Halts Plans to Eliminate Third-Party Cookies in Chrome

      July 23, 2024

      Big Brother is Watching: Apple’s Creepy New Ad Urges iPhone Users to Ditch Chrome

      July 23, 2024

      Nvidia Stock Soars to New Record at $219.44 Ahead of May 20 Earnings

      May 12, 2026

      Rocket Lab Shares Surge Past $120 Following Wave of Analyst Upgrades

      May 12, 2026

      GM Shares Decline Following 600 IT Layoffs Amid Strategic AI Workforce Transformation

      May 12, 2026

      SES Delivers €847M Q1 Performance as Intelsat Integration and Aviation Deals Fuel Expansion

      May 12, 2026
    • How To

      Batch Files: Tokens and Delimiters (FOR Loops)

      July 31, 2024

      Types of Ethernet Cabling & Electrical Low Voltage Wiring

      July 9, 2024

      What You Should Know About .JSON File Extension

      January 10, 2023

      Bkup File Extension

      November 19, 2022

      HEIC File Extension

      November 19, 2022
    • Office
      1. Excel
      2. Google Sheets
      3. View All

      How to Convert Column List to Comma Separated List in Excel

      July 24, 2024

      How to Find the Last Monday of the Month in Excel

      July 24, 2024

      Convert Bytes to MB or GB in Excel: 3 Methods!

      July 24, 2024

      How to Remove Characters from Right in Excel

      July 30, 2023

      How to Subtract in Google Sheets: Complete Guide

      July 31, 2024

      Bullet Points in Google Sheets

      January 20, 2022

      Sort by Date in Google Sheets

      January 18, 2022

      Google Sheets Timestamp

      January 17, 2022

      How to Subtract in Google Sheets: Complete Guide

      July 31, 2024

      How to Convert Column List to Comma Separated List in Excel

      July 24, 2024

      How to Find the Last Monday of the Month in Excel

      July 24, 2024

      Convert Bytes to MB or GB in Excel: 3 Methods!

      July 24, 2024
    • Answers
    • About
    • Contact
    Facebook X (Twitter)
    Computing.net
    How To

    Working with Batch variables and For loops

    Computing StaffBy Computing StaffOctober 6, 2021
    Twitter LinkedIn Email Telegram
    Working with Batch variables and For loops
    Twitter LinkedIn Email Telegram

    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.

    —————————

    Contents:

    Toggle
    • How to have the user input a Value
    • How to Replace filenames or String of Text

    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.

    —————————

    How to Replace filenames or String of Text

    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

    Share. Twitter LinkedIn Email Telegram
    Avatar photo
    Computing Staff
    • Website

    Related Posts

    Batch Files: Tokens and Delimiters (FOR Loops)

    July 31, 2024

    Types of Ethernet Cabling & Electrical Low Voltage Wiring

    July 9, 2024

    What You Should Know About .JSON File Extension

    January 10, 2023

    Bkup File Extension

    November 19, 2022

    HEIC File Extension

    November 19, 2022

    Virtual PC 2007 How To Guide

    October 6, 2021
    Add A Comment

    Comments are closed.

    Latest

    Nvidia Stock Soars to New Record at $219.44 Ahead of May 20 Earnings

    May 12, 2026

    Rocket Lab Shares Surge Past $120 Following Wave of Analyst Upgrades

    May 12, 2026

    GM Shares Decline Following 600 IT Layoffs Amid Strategic AI Workforce Transformation

    May 12, 2026

    SES Delivers €847M Q1 Performance as Intelsat Integration and Aviation Deals Fuel Expansion

    May 12, 2026

    Trump Dismisses Iran Peace Proposal — Oil Markets React as Hormuz Remains Restricted

    May 12, 2026
    • Facebook
    • Twitter

    Latest Reviews

    Meta Platforms Shares Tumble 8% Despite Strong Q1 Performance Amid AI Investment Surge

    April 30, 2026

    Flush.com Review: Casino & Sportsbook With 275% Welcome Bonus

    March 7, 2026

    Katsubet Review: Crypto Casino With 300% Welcome Bonus & Free Spins

    March 7, 2026

    7Bit Review: Crypto Casino With 325% Bonus & 250 FS

    March 7, 2026

    Mega Dice Review: Crypto Casino With 200% Bonus & 50 Free Spins, Legit?

    March 7, 2026


    Home / Privacy Policy / Terms & Conditions

    Computing.net © 1996 - 2026 Kooc Media Ltd. All rights reserved. Registered Company No.05695741

    Type above and press Enter to search. Press Esc to cancel.