Jump to content

batch files


pob

Recommended Posts

hi,

can anybody tell me if there is a idiots guide to making batch files around?

i am very new to this unnatended thing, but only seem to be stumbling on

this batch file thingy. :hello:

sorry if this is in the wrong place......

Link to comment
Share on other sites


First lesson..  it's no longer called "batch script" it's "command script"  :)  and technically you should use the extension .cmd instead of .bat.  BAT is only for backwards compatability.

I think your playing with words. A batch file is a batch file (.bat or .cmd)

If you search in Windows help you find help about command-line only if you type "batch files". If you type command script you get nothing.

From Windows help..

Using batch files. With batch files, which are also called batch programs or scripts, you can simplify routine or repetitive tasks. A batch file is an unformatted text file that contains one or more commands and has a .bat or .cmd file name extension. When you type the file name at the command prompt, Cmd.exe runs the commands sequentially as they appear in the file.

Can you give me something to read about this backward compatibity a links maybe? I'm curious. :P

Link to comment
Share on other sites

Welcome pob! :hello: Let's learn together.

My one and only bible for using DOS commands is this. :P

%systemroot%\help\ntcmds.chm

I still want to learn more. Thanks jdoe for the links. Do you have some more? :D

un4given1, I still don't understand the difference between .bat and .cmd. Maybe I'm just stupid but at first I thought they were the same. :lol:

Link to comment
Share on other sites

un4given1, I still don't understand the difference between .bat and .cmd. Maybe I'm just stupid but at first I thought they were the same.
A *.cmd file will not execute on a 9x operating system. Commands in a *.cmd file may not work for a 9x system. This difference ensures that scripts made for one type of system are not functional on another type of system. This is a major issue if they are treated the same.
Link to comment
Share on other sites

I still want to learn more.  Thanks jdoe for the links.  Do you have some more? :D

Sorry, it's the only links I kept.

The batch tutorial is good for learning for someone who want to start learning and the Rob van der Woude's Web site is a good reference about scripting in general.

But if you want to learn more, Google "batch tutorial" will bring a lot of good sites.

MHz

A *.cmd file will not execute on a 9x operating system. Commands in a *.cmd file may not work for a 9x system. This difference ensures that scripts made for one type of system are not functional on another type of system.

That make sense :thumbup

Link to comment
Share on other sites

Here A Cmd File That has A Simple Menu System

I use it as a template and thought it would help you.

It has 5 Spot in it

2 just Echo Back A Message

1\ Just Echo out your cd drive

2\ Makes a Folder and Removes The Folder

3\ Output A Text File And Than Open The Text File

and than delet the text file

I have added some notes to it to help. That are in the script

:: Any Text After These :: Are Notes In The script.

Hope This Helps

ECHO off

cls

:: WHERE THE MAIN MENU IS MADE, AND THE RETURN FROM THE OTHER PARTS OF THE CMD. SOFTWARE IS THE NAME THAT CMD LOOKS FOR

:Software

cls

mode con: cols=60 lines=29

COLOR 4e

TITLE MAIN MENU

:: THIS IS THE VBS PART THAT FINDS THE CD DRIVE

> Cd.vbs ECHO Dim ts

>> Cd.vbs ECHO Dim strDriveLetter

>> Cd.vbs ECHO Dim intDriveLetter

>> Cd.vbs ECHO Dim fs 'As Scripting.FileSystemObject

>> Cd.vbs ECHO Const CDROM = 4

>> Cd.vbs ECHO On Error Resume Next

>> Cd.vbs ECHO Set fs = CreateObject("Scripting.FileSystemObject")

>> Cd.vbs ECHO strDriveLetter = ""

>> Cd.vbs ECHO For intDriveLetter = Asc("A") To Asc("Z")

>> Cd.vbs ECHO Err.Clear

>> Cd.vbs ECHO If fs.GetDrive(Chr(intDriveLetter)).DriveType = CDROM Then

>> Cd.vbs ECHO If Err.Number = 0 Then

>> Cd.vbs ECHO strDriveLetter = Chr(intDriveLetter)

>> Cd.vbs ECHO Exit For

>> Cd.vbs ECHO End If

>> Cd.vbs ECHO End If

>> Cd.vbs ECHO Next

>> Cd.vbs ECHO Set ts = fs.CreateTextFile(Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) ^& "cmd", True, False)

>> Cd.vbs ECHO ts.WriteLine "set " ^& Left(WScript.ScriptName, InStr(WScript.ScriptName, ".") - 1) ^& "=" ^& strDriveLetter

>> Cd.vbs ECHO ts.Close

:: TO SET THING LIKE SYSTEMDRIVE TO WHAT YOU WANT

SET SD=%systemdrive%\

SET SCD=%CDROM%Apps\

:: THIS IS TO STOP THIS SCRIPT 1000=1 SECOND

> Rst0.vbs ECHO Wscript.sleep 125

>> Rst0.vbs ECHO Wscript.quit

> Rst1.vbs ECHO Wscript.sleep 875

>> Rst1.vbs ECHO Wscript.quit

> Rst2.vbs ECHO Wscript.sleep 1875

>> Rst2.vbs ECHO Wscript.quit

> Rst3.vbs ECHO Wscript.sleep 2875

>> Rst3.vbs ECHO Wscript.quit

:: VARIBLES TO SAVE TIME FROM RETYPING

SET Size3=mode con: cols=45 lines=3

SET Size5=mode con: cols=55 lines=5

SET Size7=mode con: cols=55 lines=7

SET Size11=mode con: cols=60 lines=11

:: VARIBLES THAT START A ACTION. I AM TO LAZY TO TYPE

SET R0=start /w Rst0.vbs

SET R1=start /w Rst1.vbs

SET R2=start /w Rst2.vbs

SET R3=start /w Rst3.vbs

:: VARIBLES FOR TEXT.

SET TXMSG1=PLACE A INSTALL ORDER HERE

ver

ECHO    ÉÄÍÍÍÍÍ MY DEMO ÍÍÍ MENU  ÍÍÍÍÍÍÍÄ»

ECHO    ºÄÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍĺ

ECHO    ºÄÄÄÄÄÄÄÄÄÄ  Menu 1  ÄÄÄÄÄÄÄÄÄÄÄÄĺ

ECHO    ºÄ TYPE: ALL                    ĺ

ECHO    ºÄÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍĺ

ECHO    ºÄÄÄÄÄÄÄÄÄÄ  Menu 2  ÄÄÄÄÄÄÄÄÄÄÄÄĺ

ECHO    ºÄÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍĺ

ECHO    ºÄ TYPE: CDD  DEMO OF SET CD VAR ĺ This Uses A Vbs File

ECHO    ºÄÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍĺ That Generated

ECHO    ºÄÄÄÄÄÄÄÄÄÄ  Menu 3  ÄÄÄÄÄÄÄÄÄÄÄÄĺ In This Cmd

ECHO    ºÄÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍĺ

ECHO    ºÄ TYPE: MRD  DEMO MKDIR RMDIR  ĺ

ECHO    ºÄÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍĺ

ECHO    ºÄÄÄÄÄÄÄÄÄÄ  Menu 4  ÄÄÄÄÄÄÄÄÄÄÄÄĺ

ECHO    ºÄÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍĺ

ECHO    ºÄ TYPE: OUT  DEMO TEXTOUTPUT  ĺ Simple Message

ECHO    ºÄÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍĺ

ECHO    ºÄÄÄÄÄÄÄÄÄÄ  Menu 5  ÄÄÄÄÄÄÄÄÄÄÄÄĺ

ECHO    ºÄÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍĺ

ECHO    ºÄ TYPE: E                      ĺ

ECHO    ºÄÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍĺ

ECHO    ºÄ  Q. Quit                    ĺ Cleans Up Any Files

ECHO    ÈÄÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍļ  From Script

Time /t

ECHO.

:: SET /P prompts for input and sets the variable

:: to whatever the user types

:: You Can Name Anything After /P To What You Want

:: The Name Must Match What Between >'%UC%'< After The /I

SET UC=

SET /P UC=Type What On The Menu Than Press Enter:

:: To Increase The Amount Of Text To Input

:: '%UC%'=='LEAVE HERE WITH THE AMOUNT SPACE BLANK'

:: BELOW 3 EXAMPLES THE DEFUALT IS SET TO 3 LETTERS

:: SO NOW THE MENU WORKS WITH EITHER 1 2 3 OR 3 LETTERS

:: The syntax in the next line extracts the substring

:: starting at 0 (the beginning) and 1 character long EG A, 1, Z, 4

::IF NOT '%UC%'=='' SET Choice=%UC:~0,1%

:: starting at 0 (the beginning) and 2 character long EG AB, AT, ZZ 

::IF NOT '%UC%'==' ' SET Choice=%UC:~0,2%

:: starting at 0 (the beginning) and 3 character long EG BAT, CAT, MAT ,HAT

IF NOT '%UC%'=='  ' SET Choice=%UC:~0,3%

:: /I makes the IF comparison case-insensitive

IF /I '%UC%'=='All' GOTO Menu1

IF /I '%UC%'=='CDD' GOTO Menu2

IF /I '%UC%'=='MRD' GOTO Menu3

IF /I '%UC%'=='OUT' GOTO Menu4

IF /I '%UC%'=='E' GOTO Menu5

IF /I '%UC%'=='Q' GOTO End

ECHO "%UC%" is not valid. Please try again.

CLS

GOTO %UC%

:: GOTO MEANS IT NEED A PLACE TO GO TO LIKE THE NAME SAYS.

:: Menu1, Menu2, Menu3, Menu4, Menu5 ARE THE SPOTS IN THE BATCH FILE WHERE WE GOTO FROM SOFTWARE

::THAN WE GOTO SOFTWARE FROM Menu1, Menu2, Menu3, Menu4, Menu5

:Menu1

CLS

%Size3%

COLOR 9F

TITLE MENU SELECTION 1

ECHO.

ECHO  %TXMSG1% 1

%R1%

cls

ECHO.

ECHO  Back To Main

%R1%

GOTO Software

:Menu2

CLS

%Size3%

COLOR 9E

TITLE MENU SELECTION 2 Example Varible

ping -n 2 127.0.0.1>nul

start wscript.exe Cd.vbs

ping -n 2 127.0.0.1>nul

del cd.vbs

call cd.cmd

del cd.cmd

cls

%Size3%

color 7b

ECHO Your Cd Is %CD%

ECHO.

SET cdrom=%CD%

%R1%

SET CDS=%CDROM%\Apps\

mode con: cols=55 lines=3

SET D=%CDROM%\Apps\

%R0%

Title Varible 1

ECHO.

ECHO  %CDS%

%R3%

cls

Title Varible 2

ECHO.

ECHO  %D%

ECHO  Completed

%R3%

GOTO Software

:Menu3

CLS

%Size7%

COLOR 2f

TITLE MAKE A DIRECTORY

mkdir %SD%\123temp

ECHO.

ECHO That The Directory That Was just Made

ECHO Cmd    mkdir %SD%123temp

ECHO Using The Mkdir Cmd

ECHO Passing The Varible I Made SD=%systemdrive%

ECHO.

pause

cls

COLOR Cf

TITLE REMOVE THE  DIRECTORY

rmdir /s /q %SD%123temp

ECHO.

ECHO Cmd    rmdir /s /q %SD%123temp

ECHO That Cmd  to Remove The Folder That Was Made

ECHO.

pause

GOTO Software

:Menu4

cls

%Size11%

color f2

TITLE PRODUCE A TEXT FILE

ECHO.

echo off

ECHO THIS IS A TEST > TEST.TXT

ECHO. >> TEST.TXT

date /t>> TEST.TXT

ECHO HELLO THIS IS MESSAGE >> TEST.TXT

ECHO. >> TEST.TXT

time /t>> TEST.TXT

ECHO. >> TEST.TXT

ECHO "THIS IS A TEST > TEST.TXT"

ECHO A Single Arrow Will Start A New File And

ECHO Delete What Was There Before It.

ECHO "THIS IS A TEST >> TEST.TXT"

ECHO A Double Arrow Adds To The File And Leaves What There Un Changed

pause

color 7F

TITLE LETS CHECK THE TEXT FILE

cls

ECHO.

Notepad.exe TEST.TXT

ECHO  Completed

del TEST.TXT

%R0%

echo off

GOTO Software

:: Menu1, Menu2, Menu3, Menu4, Menu5 ARE THE SPOTS IN THE BATCH FILE WHERE WE GOTO FROM SOFTWARE

::THAN WE GOTO SOFTWARE FROM Menu1, Menu2, Menu3, Menu4, Menu5

:Menu5

cls

mode con: cols=40 lines=5

color 5e

TITLE MENU SELECTION 5

ECHO.

ECHO  %TXMSG1% 5

%R1%

cls

ECHO.

ECHO  Back To Main

%R1%

GOTO Software

:end

cls

%Size3%

color a9

TITLE MENU THE END

ECHO.

ECHO Here The End Clean Up Time?

%R3%

ping -n 1 127.0.0.1>nul

del Rst*.vbs

del Cd.vbs

EXIT

Edited by gunsmokingman
Link to comment
Share on other sites

but this script is still no good if you have two or more cdrom drives, this only finds the first one. The find CDROM part must be changed to this and a cd.txt in the root of the CDROM.

> Cd.vbs echo Dim ts 
>> Cd.vbs echo Set fso = CreateObject("Scripting.FileSystemObject")
>> Cd.vbs echo For Each objDrive In fso.Drives
>> Cd.vbs echo If objDrive.DriveType = "4" And objDrive.IsReady Then
>> Cd.vbs echo If fso.FileExists(objDrive ^& "\cd.txt") Then strDriveLetter = objDrive
>> Cd.vbs echo End If
>> Cd.vbs echo Next
>> Cd.vbs echo Set ts = fso.CreateTextFile("cd.cmd", True, False)
>> Cd.vbs echo ts.WriteLine "SET CDDrive = " ^& strDriveLetter
>> Cd.vbs echo ts.Close
>> Cd.vbs echo WScript.Sleep 2000

otherwise a good bit of coding :-)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...