Batch file programming

This is a discussion on Batch file programming within the Tech Board forums, part of the Community Boards category; Hi I was learning about batch file programming and making this paper so that i could remember the commands but ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    22

    Cool Batch file programming

    Hi
    I was learning about batch file programming and making this
    paper so that i could remember the commands but this paper
    solely taken the form of tutorial.Well it isnt completed yet but i
    will provide soon the complete text.

    So here it goes

    ++++++++++++++++++++++++++++++
    Well batch file is the easiest way to implement and run set of
    DOS commands by just running a single file.Batch files can be
    easily made and edited using any of the text editor (I preffer
    using notepad because i use it :-)

    Simply if you want to read the code of the batch file open it in
    notepad or select edit option by right clicking on it. and if you
    want to create one batch file write code in notepad and SAVE AS
    file as xxx.bat. The main thing is to make it .bat extension.

    Codes which can be used in batch files.

    According my current knowledge every command which can be
    use in DOS can be used in a batch file.


    1. dir
    ===

    if you want to run dir command then the code for batch
    file will be as follows:
    Code:
    	dir
    After writing this in notepad save it whatever.bat and
    when you run it,a dos window will appear and you can see the
    directory structure of that directory where whatever.bat is
    placed. Now if you want to look at the other dirctory say windows
    then code will be
    Code:
    	cd c:\windows\
    	dir /p
    Here "cd c:\windows\" will switch us to windows folder
    and "dir /p" will show the files and folders.( /p at the end of dir
    stands for showing files and folders page wise)
    Now there is another facility through which instead of
    displaying output it can save the output to a file.This is done as :-
    Code:
    	dir > c:\info.txt
    This will create a text file named as "info" at C: drive
    which contain the detail files and folders of the directory where
    the batch file is executed.


    2. echo
    ====
    This will show whatever you write after it.

    EXAMPLE
    Code:
    	echo hi
    OUTPUT
    Code:
    	C:\WINDOWS\Desktop>echo hi
    	hi
    the first line shows where the batch file is placed
    followed by command executed.
    You can create the fake error message just for fun.

    EXAMPLE
    Code:
    	echo This program uses to much resourses.Please shut
    down windows now.
    OUTPUT
    Code:
    	C:\WINDOWS\Desktop>echo This program uses to
    much resourses.Please shut down windows now.
    	This program uses to much resourses.Please shut
    down windows now.
    but any one can easily understand that it is a fake
    message by reading first line.


    3. @
    =
    Well one can hide first line i.e path and command
    executed simply by adding "@" before any command.It just dont
    shows the path and the command executed.It can be applied
    approximately on every command.

    EXAMPLE
    Code:
    	@ echo Welecome to Dos
    OUTPUT
    Code:
    	Welcome to Dos
    If echo command is followed by off keyword (i.e "echo
    off") then nothing will be displayed and the dos window will
    appere only for a sec or less than a sec.
    Well this command will be very useful its importance is
    discussed later in this article.


    4. md and rd
    =========
    This is stand for make directory and remove
    directory.The syntax is simple "md name" AND "rd
    name".Here "name" is the name of the folder which you want to
    create.

    NOTE:-For removing a directory or we can say for deleting the
    folder it is nessecary that the folder should be empty.I dont think
    that example for these command is needed



    5. pause
    =====
    Here another intresting command.this will ask the user
    to press any key to continue.

    EXAMPLE
    Code:
    	@ echo FATAL ERROR
    	@ pause
    OUTPUT
    Code:
    	FATAL ERROR
    	Press any key to continue . . .
    Now this actually looks like a error message:-)


    6. start
    =====
    This command is used when we want to open files from
    the batch file.

    EXAMPLE
    Code:
    	
    	@ start abc.jpg
    this will open up the "abc" named jpg file within that
    folder.we can also open .exe .bmp etc...; even we can open other
    batch file from this command.For the file located in other folder
    we have to just add the path of it followed by file name.OK one
    thing is to be noted that whatever file you open from this
    command will open in a new window.
    Now some evil fun.you can create infinite loop of start
    for make your screen messy.just you have to do it that make two
    batch files within a folder.for example xyz.bat and abc.bat. Now in
    xyz.bat write code:-
    Code:
    	@ start abc.bat
    and in abc.bat write code:-
    Code:
    	@ start xyz.bat
    This will open up endlessly dos windows.well i warn
    you for for getting out this infinte loop you have to very quickly
    click on the close button and then to yes button before the next
    window opens.


    7. call
    ====
    It is similar like the start command but it can only be
    used to open other batch files.
    Start command opens the file on other dos window While this
    command will open other batch files within the same window.So
    the call command is the extension of start command which is
    limited on batch files only.

    EXAMPLE

    (abc.bat)
    Code:
    	@ Eminem is cool	
    	@ call xyz
    (xyz.bat)
    Code:
    	@ pause
    OUTPUT
    Code:
    	Eminem is cool
    	Press any key to continue . . .
    This is all happens in a single dos window.If the file is
    placed on other folder then blah blah blah.... you know what to
    do.
    NOTE:- This command is only limited to batch files thats why it is
    not nessecary to specify extension i.e *.bat.Our command will
    work without it too.


    8. goto
    ====
    Goto command is used like a unconditional repeate
    loop or a jump instruction.There are to part in this command that
    are goto itself and label.now we understand them one by one
    1st LABEL:- this act like a pointer to goto
    instruction.This can be any keyword specified by you.before it we
    have to add a colon " : ".
    2nd goto:- This will call the specifed label.
    I know this will not clear you to till now.example will clear your
    view.

    EXAMPLE
    goto as a jump command
    Code:
    	@ echo This text will displayed
    	@ goto itsalabel
    	@ echo This text will not be displayed
    	: itsalabel
    Here Last line is label.the colon should be followed by
    any keyword.In our example "itsalabel" is our keyword.

    OUTPUT
    Code:
    	This text will displayed
    
    	C:\WINDOWS\Desktop\New Folder>
    after outputting the first line the goto instruction will
    send excecution to its label that is fourth line due to which third line will be skipped.

    ANOTHER EXAMPLE
    goto as unconditional repeat loop
    Code:
    	@ echo hello
    	@ pause
    	: itsalabel
    	@ echo hi
    	@ goto itsalabel
    It will print hello one time and then will print infintly "hi".

    ANOTHER ANOTHER EXAMPLE
    Code:
    	@ echo This text will displayed
    	@ pause
    	@ goto label1
    	: label2
           @ exit
    	@ echo This text will not be displayed
    	: label1
    	@ goto label2
    This example can be easily understood if you have
    readed this text thourghly.


    9. For
    ===
    This is the conditional loop.Dont have much information
    on it:-(


    10. del
    ===
    For deleting files.

    EXAMPLE
    Code:
    	@ del *.txt
    This will delete all text files of that folder.As you can
    see that it would ask for a confirmation.

    If you are using win98 then and want to crack the password of a
    windows login account write code
    Code:
    	@ del c:\windows\*.pwl
    NOW SOME EVIL STUFF(Do Not Try This At Home)
    Code:
    	@ echo off
    	@ echo y | del c:\window\*.*
    Second line code will make it to not to ask for
    confirmation.This will damage your system.but the system and
    hidden files will not be deleted.
    Windows XP's del command give you facilite to delete files on
    basis of there attributes( /A Selects files to delete based on
    attributes.attributes: R Read-only files,S System files,H Hidden
    files,A Files ready for archiving,- Prefix meaning not)


    11. Deltree
    =====
    Delete files and directory too.

    Code:
    	@ echo off
    	@ deltree c:\newfolder\*
    special about this command is that this deletes
    dirctories and sub directories including files.It is a command to
    delete files and folders.

    Another Speciality is that it also delete the hidden and system
    files which the del command was unable to do.

    Another Another specialty is that windows does provide to skip
    confirmation.here how to do it.

    MORE EVIL STUFF(Do Not Try This At Home)
    Code:
    	@ echo off
    	@ detree /y c:\window\*
    the '/y' stands for yes to all.

    NOTE:-
    I would prefer not to become Evil.Think how to use your skills to
    create some thing not to destroy.Well I would not be responsible
    for any kind of damage occured in your system by my tutorial.By
    the way I have taken precaution.I have written "windows"
    as "window" in code so that it would not damage any moron
    system who just do's the copy-paste ........ and run it without
    reading note like this.
    INCOMPLETE
    +++++++++++++++++

    Well my intension is not to create harmful files and codes but to
    get aware you how it (batch file viruses) works.As i have
    mentioned before that i was not making tutorial so please donot
    post drawbacks.The posts which tells more about batch file
    programming is MOST WELCOME.and also apprietiation posts

    Does i have posted in right place?

    KNOWLEDGE IS POWER
    BUT ONLY IN RIGHT HANDS
    Last edited by year2038bug; 09-03-2005 at 01:54 PM.

  2. #2
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    Not commenting on the actual tutorial, I knew most of it, but if it were cleaned up and expanded it might be helpful to beginners. I actually wanted to cmoment on your last line,

    "KNOWLEDGE IS POWER
    BUT ONLY IN THE RIGHT HANDS"

    No, knowledge is power no matter what, which is exactly why you have to warn people NOT to make viruses. If knowledge wasn't power to people who meant harm, then we wouldn't have to worry, but we do. Knowledge is power, period.
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Excellent. Now I can finally uninstall Perl!

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    God help a person looking for something specific on the MSDN, but this is as close as I could come.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    22
    Quote Originally Posted by Stan100
    Not commenting on the actual tutorial, I knew most of it, but if it were cleaned up and expanded it might be helpful to beginners. I actually wanted to cmoment on your last line,

    "KNOWLEDGE IS POWER
    BUT ONLY IN THE RIGHT HANDS"

    No, knowledge is power no matter what, which is exactly why you have to warn people NOT to make viruses. If knowledge wasn't power to people who meant harm, then we wouldn't have to worry, but we do. Knowledge is power, period.
    "KNOWLEDGE IS POWER
    BUT ONLY IN RIGHT HAND"

    that was left and right's right.right

  6. #6
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    Quote Originally Posted by year2038bug
    "KNOWLEDGE IS POWER
    BUT ONLY IN RIGHT HAND"

    that was left and right's right.right
    I'm left handed
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  7. #7
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    @echo off is good to put at the beginning of a batch file...
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by Stan100
    I'm left handed


    >>The posts which tells more about batch file
    programming is MOST WELCOME<<

    Take a look at %WINDIR%\Help\ntcmds.chm
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    For installing application and not going to oether app until this command is over
    Code:
    @ECHO OFF
    TITLE Installing App
    ECHO Installing Application
    start /wait app.exe
    REGEDIT /S SomeReg.reg
    COPY /Y File.exe "%alluserprofile%\Desktop\"
    ECHO.
    ECHO Script Generated by xxxrugby
    PAUSE
    EXIT
    ECHO. -> Puts blank line.!
    REGEDIT /S -> Silent put reg script into registry
    COPY /Y dont ask if file already exist overwrite it
    %alluserprofile% -> this is short so you dont haveto write c:/Documents and Setting/All Users/
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    9. For
    ===
    This is the conditional loop.Dont have much information
    on it:-(
    Try
    Code:
    C:\>help for
    assuming you have help.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Code:
    REM this is comment. It will not be used for nothing. 
    REM This is just for you to know what goin on
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21