Myanmar IT Resource Forum
Myanmar IT Resource Forum
Myanmar IT Resource Forum

You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Batch File Programming [ Programming ] Empty Batch File Programming [ Programming ] 25th May 2009, 11:34 pm

sHa92

sHa92
Founder



Founder

  1. Check if a batch parameter contains wildcards.
    Code:
    FOR %%f in (%1) do IF %%f==%1 GOTO nowilds
  2. Get the date or time without
    having to press [Enter].

    Code:
    FIND.EXE /V "): " ECHO. |DATE |FIND.EXE "Current date"
     ECHO. |TIME |FIND.EXE "Current time"
     :: [font=Times New Roman][size=12] Or, the following which should be language independent.[/size][/font]
      ECHO. |DATE |FIND.EXE /V "): "
     ECHO. |TIME |FIND.EXE /V ": "
  3. Find a file using a partial file name.
    This is also useful for finding 'other' strings in the
    DIR output.


    Code:
    DIR/A-D/-P *.* %2 %3 %4 |FIND.EXE /I %1 |MORE.COM
  4. Omit specific files from DIR output.
    Code:
    :: Omit .bat and sys files, without /B switch.
     DIR/A-D/S/-P |FIND.EXE /V " BAT " |FIND.EXE /V " SYS "
     
     :: Omit .bat and sys files, with /B switch.
     DIR/A-D/B/S/-P |FIND.EXE /V ".BAT" |FIND.EXE /V ".SYS"
  5. Redirect a command's ouput to a file & place date and time into a file.
    Redirection works with many, but not all DOS commands.


    Code:
    :: Creates a new file.
     ECHO. |DATE |FIND.EXE "Current date" >results.txt
     
     :: Creates a new file, or appends to an existing file.
     ECHO. |TIME |FIND.EXE "Current time" >>results.txt
  6. Get the date and
    time into variables.

    Code:
    :: datetime.bat
     :: also works with mo/da/year format
     :: revised 30 April 2005
     @echo off
     echo @prompt set time=$t$_set date=$d$_>%temp%.\$dattim1.bat
     %comspec%/e:4096/c%temp%.\$dattim1.bat>%temp%.\$dattim2.bat
     call %temp%.\$dattim2.bat
     for %%v in (%time%) do set time=%%v
     echo set day=%%1>%temp%.\$dattim3.bat
     echo set date=%%2>>%temp%.\$dattim3.bat
     call %temp%.\$dattim3.bat %date%
     del
    %temp%.\$dattim?.bat
     ::
  7. Redirect a batch file's ouput to a file.
    Code:
    COMMAND/CTEST.BAT >results.txt
  8. Add a directory to the path.
    Code:
    PATH [i]drv[/i]:\[i]DirName[/i];%path%
     :: Or
     PATH %path%;[i]drv[/i]:\[i]DirName[/i]
  9. Remove a
    specific line from an ASCII (plain text) file.

    Code:
    FIND.EXE /V "[i]String[/i]" <[i]anyfile[/i]> %temp%.\[i]anyfile.tmp[/i]
     COPY/Y %temp%.\[i]anyfile.tmp[/i] [i]anyfile[/i]
     DEL %temp%.\[i]anyfile.tmp[/i]
  10. Get user input.
    Here is the standard method widely accredited to batch file pioneer, Tom Lavedas,
    which has been adapted to work no matter which directory is current. In other
    words,
    it will not slow down or fail if current is a floppy, write protected, full or otherwise
    unavailable. This DOS batch solution is broken in NT.


    Code:
    :: input.bat
     :: adaptation of a routine by Tom Lavedas
     @ECHO off
     IF "%temp%"=="" ECHO. the temp variable must be set
     IF "%temp%"=="" GOTO end
     IF not exist %temp%.\nul ECHO. temp variable is invalid
     IF not exist %temp%.\nul GOTO end
     IF exist enter.bat ECHO. enter.bat in current directory.
     IF
    exist enter.bat GOTO end
     SET input=
     ECHO Type input and press [Enter].
     FC.EXE CON nul /LB1 /N |DATE |FIND.EXE "1:"> %temp%.\setinput.bat
     ECHO :loop> %temp%.\enter.bat
     ECHO IF not "%%input%%"=="" SET input=%%input%% %%5>>%temp%.\enter.bat
     ECHO IF "%%input%%"=="" SET input=%%5>> %temp%.\enter.bat
     ECHO SHIFT>> %temp%.\enter.bat
     ECHO IF not "%%5"=="" GOTO loop >> %temp%.\enter.bat
     PATH> %temp%.\respath.bat
     PATH
    %temp%.\;%path%
     FOR %%c in (call del) do %%c %temp%.\setinput.bat
     FOR %%c in (call del) do %%c %temp%.\respath.bat
     DEL %temp%.\enter.bat
     IF not "%input%"=="" ECHO. Input was: "%input%"
     :end
     ::
  11. Get user input with verification and option to quit.
    This is another version of the previous batch. Instead of utilizing the %temp%
    directory from the current, it changes to the %temp% directory (and
    back again).
    Verification and option to quit added.


    Code:
    :: input2.bat
     :: get user input with verification and option to quit
     @ECHO off
     ECHO @PROMPT $N:$_CD $P> %temp%.\setback1.bat
     %COMSPEC%/E:2048/C%temp%.\setback1.bat> %temp%.\goback.bat
     DEL %temp%.\setback1.bat
     FOR %%c in (%temp%.\ CD) do %%c %temp%.
     
     :again
     SET input=
     ECHO. Type input and press [Enter].
     FC.EXE CON nul /LB1 /N |DATE |FIND.EXE "1:"> setinput.bat
    ECHO :loop > enter.bat
     ECHO IF not "%%input%%"=="" SET input=%%input%% %%5>>enter.bat
     ECHO IF "%%input%%"=="" SET input=%%5>> enter.bat
     ECHO SHIFT >> enter.bat
     ECHO IF not "%%5"=="" GOTO loop >> enter.bat
     FOR %%c in (call del) do %%c setinput.bat
     DEL enter.bat
     IF not "%input%"=="" CHOICE.COM /N/CYQN " Is this correct? Yes, No or Quit "
     IF not "%input%"=="" IF errorlevel 3 GOTO again
     IF not "%input%"=="" IF errorlevel 2 SET input=
     
     :end
     FOR %%c in (call del) do %%c %temp%.\goback.bat
     IF not "%input%"=="" ECHO. Input was %input%
     ::
  12. Get user input with doskey.
    A batch by Tom Lavedas slightly modified to write to %temp% instead of
    current, eliminated use of tildes (~), shortened variable name. Changed
    'echo' to 'echo.' and 'now:' to 'now.' which improves readability of
    initial output line. And switched order of
    first two lines after @echo off


    Code:
    :: InputLin - A routine to get a line of text from user.
     :: $ƴǩǾ
     :: http://www.myanmaritresource.com/
     @echo off
     doskey [4]=echo $*$G %temp%.\tmp.txt$T exit >nul
     echo. Enter your data now.
     fc con nul /lb1 /n | find /n " 1: " | %comspec% /k > nul
     doskey 1:=echo set Input=$*$G %temp%.\tmp.bat$T exit
     %comspec% /k < %temp%.\tmp.txt > nul
     for %%v in ([4]
    1:) do doskey %%v=
     for %%v in (%temp%.\tmp.bat del) do call %%v %temp%.\tmp.??t
     for %%v in (%for example%) do echo Input Line: "%input%"
  13. Get the complete file name of a running batch file.
    This will return the complete batch file name, whether an extension is
    used or not. However, if a path is used, it will also be included in
    the %src% variable.


    Code:
    ECHO %0? |FIND.EXE /I
    ".bat?" >nul
     IF not errorlevel=1 SET src=%0
     IF    errorlevel=1 SET src=%0.bat
  14. Determine if a path is used when a batch file is run.
    In addition to getting the complete file name, this code checks to see
    if the file exists as passed to the batfile. If it doesn't exist, then
    a path to the batch was not entered, or the batch file is not in the
    current directory.


    Code:
    ECHO %0? |FIND.EXE /I ".bat?" >nul
     IF not errorlevel=1 SET src=%0
     IF    errorlevel=1 SET src=%0.bat
     IF not exist %src% FOR %%c in (ECHO GOTO:end) do %%c. %src% not in current directory; path required
     :: or
     IF not exist %src% FOR %%c in (ECHO PAUSE GOTO:end) do %%c. %src% not in current directory; path required
  15. Insure an absolute path is used to run a
    batch file.

    This code does this by insuring that the complete path is entered from
    the start.


    Code:
    ECHO %0 |FIND.EXE ":\" >nul
     IF errorlevel=1 FOR %%c in (ECHO GOTO:end) do %%c  complete path required
     ECHO %0? |FIND.EXE /I ".bat?" >nul
     IF not errorlevel=1 SET src=%0
     IF    errorlevel=1 SET src=%0.bat
  16. Reboot the computer from a batch file.
    These work in MS-DOS 6.22/Windows 3.11. Use at your own risk.

    Code:
    :: warmboot.bat
     @ECHO E 0040:0072 34 12 >%temp%.\reboot.dat
     @ECHO G=FFFF:0000 >>%temp%.\reboot.dat
     @DEBUG.EXE <%temp%.\reboot.dat >nul
     
     :: coldboot.bat
     @ECHO G=FFFF:0000 | DEBUG.EXE >nul
  17. Check how many hard disks are present.
    This will show how many hard drives are installed and
    provide a summary of each.

    Code:
    UNFORMAT.COM /L /PARTN |FIND.EXE "Drive #"

    Typical output:
    Drive # 80h has 3695 cylinders, 255 heads, 63 sectors (from BIOS).
    Drive # 81h has 1811 cylinders, 128 heads, 63 sectors (from BIOS).

    If you only want the number of hard drives, use the /C switch on FIND.EXE.
  18. See which is the last valid drive letter.
    Code:
    MEM.EXE /D |FIND.EXE "LASTDRIVE"
  19. Get drive letter assigned to the ramdrive.
    Looks for "ramdrive" in a label by default, but accepts a different string to
    search for as an argument. Message displays to screen if label is found; remove %
    signs around %>nul% to disable
    this feature. Stops searching if string is
    found in a label. Does not find labels with more than one adjacent space, but this
    feature could be added.


    Code:
    :: Begin f-ramvol.bat
     @echo off
     IF "%1"=="ReCuRs" FOR %%C in (SHIFT GOTO:loop) do %%C
     IF not "%1"=="" SET vol=%1
     IF "%1"=="" SET vol=ramdrive
     %0 ReCuRs d e f g h i j k l m n o p q r s t u v w x y z
     
     :loop
     IF "%1"=="" FOR %%C in (SET GOTO:end) do %%C vol=
     %COMSPEC%/F/CVOL %1: |FIND/I
    "%vol%" %>nul%
     IF errorlevel=1 FOR %%C in (SHIFT GOTO:loop) do %%C
     SET drv_%1=%vol%
     SET vol=
     :end
     ::
  20. Save and restore the path.

    Code:
    :: save
     PATH>%temp%.\respath.bat
     :: restore
     CALL %temp%.\respath
  21. Convert a string to all uppercase letters.
    Here is a way to do it, since
    PATH always converts to uppercase.


    Code:
    PATH> %temp%.\respath.bat
     PATH [i]string[/i]
     SET [i]variable[/i]=%path%
     CALL %temp%.\respath.bat
     DEL %temp%.\respath.bat
  22. Check if ansi.sys is installed.
    Code:
    :: isansi.bat
     @ECHO off
     MEM.exe /M:ANSI |FIND.EXE "=ANSI"> nul
     IF not errorlevel=1 ECHO. ansi.sys installed
     IF    errorlevel=1
    ECHO. ansi.sys not installed
  23. Check if there exists any sub-directories.
    Code:
    DIR/AD/W/-P |FIND.EXE " 2 file(s) " >nul
     IF not errorlevel=1 ECHO  no sub-directories exist
     IF    errorlevel=1 ECHO  sub-directories exist
     :: [font=Times New Roman][size=12]Or[/size][/font]
     TREE.COM [i]DirName[/i] |FIND.EXE "No sub-directories" >nul
     IF not errorlevel=1 ECHO
     no sub-directories exist
     IF    errorlevel=1 ECHO  sub-directories exist
  24. Check if there an extension on a file name in a batch parameter.
    You can use this, if the file name is passed as an existing file.


    Code:
    IF not exist %1 FOR %%c in (ECHO GOTO:end) do %%c  file is invalid
     IF    exist %1.* SET ext=none

  25. Find out if Windows is running, from a batch file.
    Code:
    SET |FIND.EXE "windir=" >nul
     IF not errorlevel=1 ECHO  Windows is running
     IF    errorlevel=1 ECHO  Windows is not running
     :: Or
     MEM.EXE /M:WIN |FIND.EXE "WIN is not"
     IF errorlevel=1 ECHO  Windows is running
     :: Or
     MEM.EXE /C |FIND.EXE "WIN" >nul
     IF not errorlevel=1 ECHO  Windows is running
     IF
        errorlevel=1 ECHO  Windows is not running
     :: Or
     MEM.EXE /D |FIND.EXE "WIN" >nul
     IF not errorlevel=1 ECHO  Windows is running
     IF    errorlevel=1 ECHO  Windows is not running
  26. Place a banner at the top of the screen.
    This example incorporates a technique used in the previous answer.


    Code:
    :: winbannr.bat
     :: Ansi.sys
    required; there are ten spaces between $d and Enter.
     @ECHO off
     SET |FIND.EXE "windir=" |IF not errorlevel=1 GOTO ok
     ECHO. Windows not running
     GOTO end
     
     :ok
     @ECHO on
     @PROMPT $e[0m
     CLS
     @ECHO off
     @ECHO on
     @PROMPT $_$e[s$e[H$e[1;36;44m $d        Enter $e[37mEXIT $e[36m to return to Windows$e[K$e[1;37;46m$e[u$p$g
     CLS
     @ECHO off
     ECHO.
     
     :end
    ::
  27. Create a simple menu without using ansi.sys. {last modified Apr. 28, 2002}
    Code:
    :: menu.bat
     @ECHO off
     :loop
     FOR %%c IN (CLS ECHO ECHO ECHO) do %%c.
     ECHO.                          1. Current directory listing
    ECHO.                            in size order
     ECHO.      % 26 spaces %      2. Edit Config.sys
    ECHO.                          3. MEM
     ECHO.                          4. MS Diagnostics (MSD)
    ECHO.                          5. Exit
     ECHO.
     :: There are 20 spaces between (") and (Your).
     CHOICE/C12345/N"                    Your choice: "
     IF errorlevel=5 GOTO end
     IF errorlevel=4 FOR %%c in (MSD.EXE GOTO:loop) do %%c/I
     IF errorlevel=3 FOR %%c in
    (MEM.EXE PAUSE GOTO:loop) do %%c
     IF errorlevel=2 FOR %%c in (edit goto:loop) do %%c c:\config.sys
     %COMSPEC%/CDIR/A/O-S/P
     FOR %%c in (pause goto:loop) do %%c
     :end
     ::
ၿမန္မာလုိ ၿပန္မရွင္းၿပေတာ့ပါဘူး.... က်ေနာ္ဖတ္ခဲ့တဲ့ Tutorials ထဲက ကူးယူၿပရတာပါ
ၿမန္မာလို ၿပန္ရွင္းၿပဖို႔အတြက္ဆုိရင္ အခ်ိန္ယူရမွာမုိ႔လု႔ိပါ....
ေနာက္ပိုင္း ဖိုရမ္ကို ေသခ်ာၿပင္ၿပီးအေၿခအေန တည္ၿငိမ္သြားရင္ Batch File Programming နဲ႔ ပတ္သက္လို႔ ေသခ်ာရွင္းၿပပါမယ္
DOS ကို အေၿခခံရိွတဲ့ သူေတြ အားလုံးနားလည္ၾကမွာပါ။

လုိအပ္တာရိွရင္ ေဝဖန္အၾကံၿပဳေပးပါခင္ဗ်ာ....

http://www.myanmaritresource.info

asbadbro

asbadbro
MITR New User



thank you. buddy

ေက်းဇူးတင္ခ်င္တင္လုိ႔ရေအာင္ ကၽြန္ေတာ္တို႔ SA က အသည္းပုံခလုတ္ေလး လုပ္ေပးထားပါသည္ ။

အဲ႔ခလုတ္ကို ႏွိပ္ျပီး ေက်းဇူးတင္လို႔ရပါတယ္ ။ စာေရးျပီး ေက်းဇူးတင္ဖို႔ အတြက္ မလိုေတာ႔ပါဖူးခင္ဗ်ာ ။

3Batch File Programming [ Programming ] Empty Re: Batch File Programming [ Programming ] 4th September 2011, 7:00 pm

paoh

paoh
MITR New User



sHa92,

ေက်းဇူးတင္ပါတယ္ ၊။
မသိတာေတြ အမ်ားၾကီး ဆို ေတာ ့
ေနာက္က်ရင္လည္း ကူညီ ပါအုံး ေနာ္။

Sponsored content


View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum

 

Create a forum on Forumotion | ©phpBB | Free forum support | Report an abuse | Cookies | Forumotion.com