[Batch] The Official Batch File Repository - Learn & ask questions about batch files

Discussion in 'Scripting' started by timesurfer, Oct 11, 2010.

  1. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    #1 timesurfer, Oct 11, 2010
    Last edited: Apr 19, 2013
    Hi,

    Seems to me a specific place to ask questions regarding batch files would be useful and contribute to MDL being the place not just to download new concepts we use and love to play with so much but rather a place to see and learn how it all happens behind the scenes in an alternative application use forum.

    Here is a bat script example:

    Trilogy 4.2

    Here are some helpful Web links to learn and play with these cmd's, switches and all that encompasses batch files that some developments at MDL are composed of

    http://www.computerhope.com/batch.htm
    http://www.robvanderwoude.com
    http://commandwindows.com/
    http://www.aumha.org/a/batches.php
    http://ss64.com/nt/

    Here are are some simple examples given to us by MDL members of certain command's and how you can use them in scripting in batch files

    Basics

    Change Console Color -- Color
    Display message -- ECHO
    Use Variables -- SET
    Conditional Statements -- IF
    Windows Console -- A Simple Menu
    Obtain script location -- %~dp0 more
    Looping Statements -- FOR
    Batch admin check - Perfected batch admin check

    Intermediate

    Creating Menu -- CHOICE,GOTO
    Using arguments -- SHIFT
    Subroutines -- CALL

    Advanced
     
  2. Matrix Leader

    Matrix Leader MDL Addicted

    Dec 8, 2009
    857
    176
    30
    Bookmarked, thanks dude
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    #3 timesurfer, Oct 11, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    I thought that people could both ask questions on how to script something as well as a place for advanced users to challenge beginners with questions on how a certain .bat file would be written while receiving help if needed:

    Example:

    How do you create a folder in a certain location with a certain name

    Answer:

    Code:
    mkdir "%SystemDrive%\Hey You"
     
  4. MasterDisaster

    MasterDisaster MDL Expert

    Aug 29, 2009
    1,256
    674
    60
    #4 MasterDisaster, Oct 11, 2010
    Last edited by a moderator: Apr 20, 2017
    Challenge #1:

    Write a batch file that takes one argument to display a diamond in numbers
    The single argument should be number.
    Eg. diamond.bat 5 will give the following output
    Code:
           0 
          0 1 
         0 1 2 
        0 1 2 3 
       0 1 2 3 4 
      0 1 2 3 4 5 
      0 1 2 3 4 5 
       0 1 2 3 4 
        0 1 2 3 
         0 1 2 
          0 1 
           0 
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Phazor

    Phazor MDL Expert

    Sep 1, 2009
    1,144
    518
    60
    #5 Phazor, Oct 11, 2010
    Last edited by a moderator: Apr 20, 2017
    Something simple. :)

    Code:
    REM [dir] [/b] [*.extension]or[/A(=Files and Folders)]or[/S(=+subfolders)] [> outputfilename.txt]
    
    REM EXAMPLES
    REM [dir /b /A > List.txt]         --> All Files And Folders
    REM [dir /b /S > List.txt]         --> All Files And Folders Plus Those In Subfolders
    REM [dir /b *.bmp > List.txt]      --> All BMPs via Wildcard
    REM [dir /b *.avi > List.txt]      --> All AVIs via Wildcard
    
    
    dir /b *.* > List.txt
    Obviously these commands are used to create a list containing the names of all and/or specific files and folders contained in the directory you execute the .bat in.

    Useful for making batch-rename lists and all sorts of other things.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. Reign_Of_Freedom

    Reign_Of_Freedom MDL Expert

    Aug 1, 2009
    1,247
    467
    60
    How would you do a batch file to check a .ini file on one of your installed programs and have it make a correction and save it? For instance -

    Find this in blah.ini:
    [Blah_Handle]
    Count=1
    Handle1=197694

    And change to:
    [Blah_Handle]
    Count=0
     
  7. DARKOR04

    DARKOR04 MDL Tester/Developer

    Jul 5, 2010
    497
    909
    10
    #7 DARKOR04, Oct 11, 2010
    Last edited by a moderator: Apr 20, 2017
    Dude you got me with that one damn
     
  8. dareckibmw

    dareckibmw MDL Expert

    Jun 16, 2009
    1,201
    1,360
    60
    Subscribed.
    [​IMG]
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. DARKOR04

    DARKOR04 MDL Tester/Developer

    Jul 5, 2010
    497
    909
    10
    Me too lol:popcorn:
     
  10. MasterDisaster

    MasterDisaster MDL Expert

    Aug 29, 2009
    1,256
    674
    60
    #10 MasterDisaster, Oct 12, 2010
    Last edited by a moderator: Apr 20, 2017
    The following batch file will take four arguments
    Filename - Name of configuration file
    Section - Section to be modified
    Name - Name of the property
    Value - New value of the property

    Only drawback of this batch file is commented lines are ignored, so these lines get removed in the modified file.
    Code:
    @echo off
    setlocal enabledelayedexpansion
    if '%1' EQU '' goto :usage
    if '%2' EQU '' goto :usage
    if '%3' EQU '' goto :usage
    if '%4' EQU '' goto :usage
    set file=new.tmp
    if Exist new.tmp del /a new.tmp
    for /f "tokens=1,2 delims==" %%A in (%1) do (
    set var=%%A
    set vat=!var:~1,-1!
    set vas=!var:~0,1!
    if '!vas!' EQU '[' ( 
    echo %%A>>%file%
    if '!vat!' EQU '%2' ( 
    set flag=1
    ) ELSE ( 
    set flag=0
    )
    ) ELSE (
    if '!flag!' EQU '1' ( 
    if '%%A' EQU '%3' ( 
    echo %%A=%4>>%file%
    ) ELSE ( 
    echo %%A=%%B>>%file%
    )
    ) ELSE ( 
    echo %%A=%%B>>%file%
    )
    )
    )
    move new.tmp %1 >NUL
    exit /b
    
    :usage
    echo syntax: setin filename section name value
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. Reign_Of_Freedom

    Reign_Of_Freedom MDL Expert

    Aug 1, 2009
    1,247
    467
    60
    @ MD I was never good with batch files so where in the script is the .ini file name suppose to be at? Attached is an example of the actual .ini itself. Pretty much just need the batch to check if "Handle" is present if so delete it then check the Count and make sure it reads Count=0.

    So where it reads ....

    [TCUP_Handle]
    Count=1 <-------No matter what this vale is at 1 or -1 or w/e needs to be 0
    Handle1=197694 [Information]
    bla ...

    Needs to be ...

    [TCUP_Handle] Count=0
    [Information]
    bla ...
    So there is no space and the ini continues as normal then saves the changes.
     

    Attached Files:

  12. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    #12 timesurfer, Oct 12, 2010
    Last edited: Oct 12, 2010
    (OP)
    lol...How bout any basics before we do conditions, variables, like simple cmd with switch :p (In tangent with stuff for advanced people)

    And how to find switches for certain cmd with cmd.exe

    Maybe explain what an argument is and demonstrate, etc...lol

    Just an suggestion so everybody and beginners like me can participate...lol

    :p Scripting Kindergarten...lol (For some of us)
     
  13. dabits

    dabits Guest

    I'd like to learn more :) so far I have basic knowledge of HTML and simple JavaScript usage and editing :cool: it's been a while tho :eek:
     
  14. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    The scripting we're learning here is for windows not the web but maybe a thread could be started for web help?
     
  15. dabits

    dabits Guest

    No, I don't know anything about this kind of scripting, but I do know that you find it everywhere ... that's why I want to learn.
     
  16. MasterDisaster

    MasterDisaster MDL Expert

    Aug 29, 2009
    1,256
    674
    60
    Where do you want to start from? The following list is based on my opinion

    Basics
    Display message -- ECHO
    Use Variables -- SET
    Conditional Statements -- IF
    Looping Statements -- FOR

    Intermediate
    Creating Menu -- CHOICE,GOTO
    Using arguments -- SHIFT
    Subroutines -- CALL
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    #17 timesurfer, Oct 12, 2010
    Last edited: Oct 12, 2010
    (OP)
    Absolute basics for me...lol Then I can put what I learn in first post as some kind of reference to start off with for those interested in learning this stuff. Plus then I will be able to be more helpful to beginners. Any other links other than the one I did in first post I can also put in first post as to add to the usefulness of this repository. What ideas do you have as how to use this thread other than we have mentioned?

    Thanks :)
     
  18. MasterDisaster

    MasterDisaster MDL Expert

    Aug 29, 2009
    1,256
    674
    60
    #18 MasterDisaster, Oct 12, 2010
    Last edited by a moderator: Apr 20, 2017
    I can just explain a command and give examples on how you can use it.

    Lets begin with ECHO

    ECHO command is used to display message on a console
    Code:
    Syntax:
    ECHO [ON | OFF] 
    ECHO [message]
    ECHO OFF is used to hide the command from being displayed on the console.
    Commonly a batch file starts with @ECHO OFF. The @ stands for immediate effect of the command including the command itself.

    ECHO Hello World displays the message 'Hello World' on the console.

    To display a blank line the ECHO command should be followed by one of the following characters (.,:;\([]+)

    Eg. ECHO.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  19. MasterDisaster

    MasterDisaster MDL Expert

    Aug 29, 2009
    1,256
    674
    60
    #19 MasterDisaster, Oct 12, 2010
    Last edited by a moderator: Apr 20, 2017
    The SET command is used create variables.

    The actual usage of SET command is to display, add or remove environment variables.

    Type SET in command prompt and hit enter. It will display all the system environment variables.

    To expand a variable, % symbol is used.

    Eg. ECHO %WINDIR% is expanded to C:\Windows

    A variable can be assigned a value once and can be used throughout the batch file.
    Batch file without using variables
    Code:
    echo This is line 1 >>"E:\test\test 1\test 2\test.txt"
    echo This is line 2 >>"E:\test\test 1\test 2\test.txt"
    echo This is line 3 >>"E:\test\test 1\test 2\test.txt"
    echo This is line 4 >>"E:\test\test 1\test 2\test.txt"
    
    Batch file using variables
    Code:
    SET file="E:\test\test 1\test 2\test.txt"
    echo This is line 1 >>%file%
    echo This is line 2 >>%file%
    echo This is line 3 >>%file%
    echo This is line 4 >>%file%
    
    If you want to change the name of the file in the former code, you need to replace all the instances of the filename. In the latter code, you just need to change it once.

    SET command also allows you to do perform arithmetic operations on variables.
    SET /A expression
    Code:
    @ECHO OFF
    ECHO Basic Arithmetic Operations
    SET /a x=15
    SET /a y=5
    SET /a z=x+y
    ECHO %x% + %y% = %z%
    SET /a z=x-y
    ECHO %x% - %y% = %z%
    SET /a z=x*y
    ECHO %x% * %y% = %z%
    SET /a z=x/y
    ECHO %x% / %y% = %z%
    SET /a z=x%%y
    ECHO %x% %% %y% = %z%
    
    SET command allows you to set the value of a variable to a line of input entered by the user
    Syntax:
    SET /P varName=[promptString]
    Code:
    SET /P select="Do you wish to continue? (Y/N): "
    ECHO %select%
    
    The next thing you can do with variables is string manipulation.

    Concatenation
    Code:
    SET var1=Hello
    SET var2=World!
    SET var2=%var1% %var2%
    ECHO %var2%
    
    Substring
    Code:
    SET var=This is a very nice day!
    echo %var:~10%
    echo %var:~-10%
    echo %var:~10,-10%
    
    Replace a string within a variable
    Code:
    SET var=This is a very nice day!
    ECHO %var% 
    ECHO %var:nice=beautiful%
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  20. urie

    urie Moderator
    Staff Member

    May 21, 2007
    9,039
    3,388
    300