How to Creating batch file example.

Open note pad or any text editor.
Copy below test and paste. 
Save file as .bat.Then to run double click on file.


@ECHO OFF
CLS
IF "%1" == "" GOTO BLANK
IF NOT "%1" == "" GOTO SORTTXT
:BLANK
@ECHO.
@ECHO   * * * * * * * * * * * * * * * * * * *
@ECHO   *  Q U I C K - S O R T  B A T C H   *
@ECHO   * * * * * * * * * * * * * * * * * * *
@ECHO.
@ECHO   * * * * * * * * * * * * * *
@ECHO * Hello world* 
@ECHO               * * * * * * * ** * * * * * * 
@ECHO.
@ECHO   Copyright (C) 2002-2003 by Michael L. White.  All rights reserved.  See the
@ECHO   redistribution requirements listed at the end of this display.  Original 
@ECHO   release date:  21 December 2002.  Update for post-Win9x versions release 
@ECHO   date:  19 September 2003.  
@ECHO.
@ECHO.
@ECHO   You did not type in a file path.  You must execute this batch file from 
@ECHO   either the DOS prompt or the Run dialog on the Windows Start menu.  See 
@ECHO   directions below.
@ECHO.
@ECHO.
@ECHO   DIRECTIONS:
@ECHO.
@ECHO   1. Type or copy and paste the list you want sorted into a new file,
@ECHO      making sure each item to be sorted is on a separate line, and save
@ECHO      the file.  Quick Sort will place numbers in numerical order, too, so 
@ECHO      keep that in mind if your list contains arabic numerals.
@ECHO   2. Press the Start button, Windows Start key, or CTRL+ESC together.
@ECHO.
@ECHO.
PAUSE
CLS
@ECHO.
@ECHO   3. Select Run from the Start menu.
@ECHO   4. Type in C:\QKSORT.BAT (or whatever the path is where you stored
@ECHO      QKSORT.BAT; letter case does not matter), then type a blank space,
@ECHO      followed by the path for the file you want to sort, another blank
@ECHO      space, and finally the path for the destination file where you
@ECHO      want your results to be saved.  
@ECHO   5. At either the DOS prompt or in the Run dialog, follow the example 
@ECHO      below using the name by which you chose to save your file earlier.
@ECHO.
@ECHO.
@ECHO   EXAMPLE:  c:\qksort.bat c:\sample.txt c:\sample2.txt
@ECHO.
@ECHO.
@ECHO   Note that the destination file has a different name.  While Quick-Sort
@ECHO   will append your sorted results to the end of your source file, you are
@ECHO   wise to save them to a new file to avoid source file alteration.  You 
@ECHO   should also limit your filename to eight characters or less to 
@ECHO   accommodate DOS file naming restrictions.  
@ECHO.
@ECHO.
@ECHO.
set /p option=For handier reference, would you like to print the entire contents of this batch file?  Type Y or N and press ENTER now.  (Y=Yes N=No) 
CLS
@ECHO.
IF "%option%"=="N" GOTO CLOSE
IF "%option%"=="n" GOTO CLOSE
IF "%option%"=="" GOTO CLOSE
IF NOT EXIST C:\QKSORT.BAT COPY QKSORT.BAT C:\QKSORT.BAT
PRINT /D:PRN C:\QKSORT.BAT
DEL C:\QKSORT.BAT
:CLOSE
@ECHO.
@ECHO   If you chose to print this file, you may need to press the sheet feed
@ECHO   button when printing is complete to eject the final page from your
@ECHO   printer.  If the print option failed, you can edit this file using
@ECHO   a plain text editor such as Windows Notepad and print it from there.
@ECHO.
@ECHO.
@ECHO   Please press ALT+F4 together to close this window ...
@ECHO.
@ECHO.
@ECHO   Copyright (C) 2002-2003 by Michael L. White.  All rights reserved.  See the
@ECHO   redistribution requirements listed below.  Original release date:  
@ECHO   21 December 2002.  Update for post-Win9x versions release date:  
@ECHO   19 September 2003.  
@ECHO.
@ECHO.
@ECHO   REDISTRIBUTION REQUIREMENTS:
@ECHO   This file may be freely distributed PROVIDED it is unaltered from its 
@ECHO   present state and the copyright notice and this notice are included on 
@ECHO   each copy, and PROVIDED FURTHER that no charge is made for its 
@ECHO   redistribution.  Contact the author, Michael L. White, by email at 
@ECHO   info@parsonplace.com for permission to use Quick Sort in any way other 
@ECHO   than what is prescribed in this notice.  Visit Parson Place on the Web
@ECHO   at http://www.parsonplace.com for more great software!
@ECHO.
PAUSE
GOTO END
:SORTTXT
SORT < "%1" >> "%2"
@ECHO.
IF "%2" == "" GOTO DESTFILE
IF NOT "%2" == "" GOTO FINISH
:DESTFILE
@ECHO   Sorting is complete...  Open your sorted file at:
@ECHO   "%1"
GOTO CLOSE
:FINISH
@ECHO   Sorting is complete...  Open your sorted file at:
@ECHO   "%2"
GOTO CLOSE
:END



How to create a bat file.

Create a .bat file 
1. Open note pad.
2. Write some command.
3.Save as .bat file(Chhose allfiles from dropdown )

Run a .bat file from console application. And how to get project folder directory in console application.

How to start process in .bat file.
Check whether it is open. 
If it is open then need not to open again.
 If it is not open then open it.


using System;
using System.IO;
using System.Diagnostics;

namespace runBatFile
{
    class Program
    {
        private static bool isOpen = false;
        private static Process batProcess = null;
        static void Main(string[] args)
        {
            var dir = Directory.GetCurrentDirectory();
            var dir1 = Directory.GetParent(dir);
            var dir2 = Directory.GetParent(dir1.ToString());

            string filepath = dir2+ "/BatFile/Test.bat";// = Server.MapPath("testBat.bat");
            filepath = filepath.Replace(@"\", @"/");
            if (isOpen == false)
            {
                batProcess = Process.Start(filepath);
                isOpen = true;
            }
            if (batProcess.HasExited)
            {
                batProcess = Process.Start(filepath);
            }
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment