Introduction: Command Line Commands

Picture of Command Line Commands

I have decided to change the title of this 'ible (if you can call it that). Instead of just having batch files, I'm going to have all different sorts of things, like VBScript and C# (if I can debug them, that is).

There is no difference between a batch file and a C# executable (in terms of invoking it), however, VBScripts need to be run differently, which I will get to.

So, here they are.

Step 1: Index

Instructions
NCOL ~ Have different coloured text and background colours. (.bat)
EMPTY ~ Checks if a file (or multiple files) are empty. (.bat, .vbs)
PROP ~ Displays the attributes of a file. (.bat, .vbs)
TALK ~ Makes your computer speak text. (.bat, .vbs)
HIDE ~ Starts a batch file (haven't tested it on anything else, but it might work) completely hidden. (.bat, .vbs)
ALARM ~ Emits a 'beep' after a timeout and displays a message (optional). (.bat, .vbs)
LEN ~ Returns the length of a phrase. (.vbs)

Step 2: How to Install

Picture of How to Install

Firstly, select a command you would like, and either copy and paste the code, or download the .zip file. If it is a VBScript, save it as whatever.vbs. If it is a batch file, save it as whatever.bat. If you had to download something, extract the .zip file and you're done.

To make them able to run on your batch files, you have many options:

1. Put them in the directory of your batch file (if you change the directory it won't work).
2. Put them into a folder, and add it to your PATH variable via the SETX command.
3. Put them into a pre-existing directory that is part of the PATH variable (such as C:\Windows\System32\).
4. Etc...

To activate a VBScript, you need to use the CSCRIPT command.

Here's the syntax:

CSCRIPT file.vbs "arguments"

NOTE:
When you do that, it spams you a little with the Windows logo, so put:

CSCRIPT /nologo file.vbs "arguments"

instead, as it will stop it from doing that.

NOTE:
I've made the VBScripts quit if you start them wrong.

Step 3: NCOL ~ Different Coloured Text

Picture of NCOL ~ Different Coloured Text
The NCOL command allows multiple colours in batch scripts, and the command prompt.

Remember: NCOL cannot yet process special characters. Such as ~!@#$%^&*()_+ and a lot more.

For a breif description, once installed, type in: 
NCOL /?
in the command prompt

@echo off
setlocal
if "%~1"=="/?" (
echo.
echo    ncol ["Text"] [Colour]
echo.
echo "Text" - The text you want displayed in another colour.
echo          Remember that spaces cannot be added if you don't put the text in
echo          quotation marks (""^).
echo.
echo Colour - The hexadecimal colour code that you want the text to be changed into.
echo          For more information of colour codes, see "color /?"
echo.
exit /b
)
for /f "delims=#" %%i in ('"prompt #$H# &for %%b in (1) do rem"') do set "bs=%%i"
<nul >"%~1.@" set /p "=.%bs%%bs%%bs%%bs%"
findstr /p /a:%2 . "*.@"
endlocal
del "*.@"
@echo on
@exit /b


Usage:
NCOL "Text" colour code

Where text is the output of the function, and colour is the hexadecimal colour code that the file will convert "text" into.

Example:

NCOL "Hello World" 10 Would output:

Hello World


Step 4: EMPTY ~ Checks If a File Contains No Data

Picture of EMPTY ~ Checks If a File Contains No Data

The EMPTY command determines whether or not a file is empty, and if it isn't, so long as the Q switch isn't enabled, it tells you the size of the file.

Now in VBScript!

--BATCH--

@echo off
setlocal

if "%1"=="/?" (
    echo.
    echo EMPTY "filepath"
    echo.
    endlocal
    exit /b
)


call :isEmpty %1

if errorlevel 2 (
    echo The file doesn't exist.
) else if errorlevel 1 (
    echo The file is not empty.
) else (
    echo The file is empty.
)
echo.
goto :eof

:isEmpty
if not exist "%~1" exit /b 2
set "size=%~z1"
echo The size of the file is %size%
if %size% gtr 0 (exit /b 1) else exit /b 0


--VBSCRIPT--

' Test if it has been run correctly.
if wscript.fullname = "C:\Windows\System32\wscript.exe" then
wscript.echo "Please use ""cscript /nologo alarm.vbs [parameters]"" to invoke this script."
wscript.quit
end if


set fso = createobject("Scripting.FileSystemObject") ' Get access to files on the computer.

' Check for missing arguments and non-existing files.
if wscript.arguments.count = 0 then
    usage(1)
else if fso.fileexists(wscript.arguments.item(0)) then
    usage(2)
else if wscript.arguments.item(0) = "/?" then
    usage(0)
end if
end if
end if

' Determining file size, and whether it's empty.
sfile = wscript.arguments.item(0)
set ofile = fso.getfile(sfile)
size = ofile.size
if size = 0 then
    wscript.echo "The file is empty."
else
    wscript.echo "The file is not empty."
end if
wscript.echo "The size of the file is " & size

function usage(errorlevel)
    wscript.echo
    wscript.echo "Determine if a file is empty, and display the size (in bytes)."
    wscript.echo
    wscript.echo "USAGE:"
    wscript.echo "cscript /nologo empty.vbs ""file path"""
    wscript.echo
    wscript.quit(errorlevel)
end function


Once again, you use it like any other command.

Type in EMPTY /? to get the help message.


Step 5: PROP ~ Attribute Checker

Picture of PROP ~ Attribute Checker

This function checks what attributes the file has.

Now in VBScript.

@echo off
setlocal enabledelayedexpansion

if "%1"=="/?" (
    echo.
    echo PROP "filepath"
    echo.
    echo Attributes:
    echo   A - Archived
    echo   R - Read Only
    echo   H - Hidden
    echo   S - System
    echo   C - Compressed
    echo   T - Temporary
    echo.
    exit /b
)

if not exist %1 (
    echo No file of that name exists.
    echo.
    exit /b
)

set "attribs=%~a1"

set attrib1=!attribs:~0,1!
set attrib2=!attribs:~1,1!
set attrib3=!attribs:~2,1!
set attrib4=!attribs:~3,1!
set attrib5=!attribs:~4,1!
set attrib6=!attribs:~5,1!
set attrib7=!attribs:~6,1!
set attrib8=!attribs:~7,1!
set attrib9=!attribs:~8,1!
if %attrib1% equ d echo Directory
if %attrib2% equ r echo Read Only
if %attrib3% equ a echo Archived
if %attrib4% equ h echo Hidden
if %attrib5% equ s echo System File
if %attrib6% equ c echo Compressed File
if %attrib7% equ o echo Offline File
if %attrib8% equ t echo Temporary File
if %attrib9% equ l echo Reparse point

echo.
exit /b


Run it like you would any command.

' Test if it has been run correctly.
if wscript.fullname = "C:\Windows\system32\wscript.exe" then
wscript.echo "Please use ""cscript /nologo prop.vbs [parameters]"" to invoke this script."
wscript.quit
end if

set fso = createobject("scripting.filesystemobject") ' Give access to files.

' Test for invalid arguments, etc...
if wscript.arguments.count = 0 then
usage(1)
else if wscript.arguments.item(0) = "/?" then
usage(0)
else if not fso.fileexists(wscript.arguments.item(0)) then
usage(2)
end if
end if
end if

' Do stuff.
set file = fso.getfile(wscript.arguments.item(0))
If file.Attributes AND 0 Then
    Wscript.Echo "No attributes set."
End If   
If file.Attributes AND 1 Then
    Wscript.Echo "Read-only."
End If   
If file.Attributes AND 2 Then
    Wscript.Echo "Hidden file."
End If   
If file.Attributes AND 4 Then
    Wscript.Echo "System file."
End If   
If file.Attributes AND 32 Then
    Wscript.Echo "Archive bit set."
End If   
If file.Attributes AND 64 Then
    Wscript.Echo "Link or shortcut."
End If   
If file.Attributes AND 2048 Then
    Wscript.Echo "Compressed file."
End If

function usage(errorlevel)
wscript.echo "Return file attributes."
wscript.echo
wscript.echo "USAGE:"
wscript.echo "PROP ""file"""
wscript.echo
wscript.quit(errorlevel)
end function

Step 6: TALK ~ the Computer Now Speaks

Picture of TALK ~ the Computer Now Speaks

The TALK command is useful for things like games, menus and generally kicking ass.

Now in VBScript!

--EDIT-- 
The /f switch has been added to enable the contents of a file to be read.  

--BATCH--

@echo off & setlocal enabledelayedexpansion
if "%~1"=="/?" (
    echo.
    echo TALK "Text" [Parameters]
    echo.
    echo Text - The phrase you want to be spoken.
    echo.
    echo [Parameters]:
    echo              /f - Read the contents of a file. "Text" changes to the file path.
    echo.
    endlocal
    exit /b
)
set text=
if [%2]==[/f] (
    if exist "%~1" (
        for /f "usebackq delims=" %%i in (%1) do set text=!text! %%i
    ) else (
        endlocal
        exit /B
    )
)
if [%2]==[] set text=%~1

echo set speech = Wscript.CreateObject^("SAPI.spVoice"^) > "talk.vbs"
echo speech.speak "%text%" >> "talk.vbs"
cscript //NoLogo //B talk.vbs
del Talk.vbs
endlocal
exit /b


--VBS--

' Test if it has been run correctly.
if wscript.fullname = "C:\Windows\System32\wscript.exe" then
wscript.echo "Please use ""cscript /nologo alarm.vbs [parameters]"" to invoke this script."
wscript.quit
end if


set speech = wscript.createobject("sapi.spvoice") ' Get the speech object.

' Test for invalid parameters, etc...
if wscript.arguments.count = 0 then
usage(1) ' If no parameters are given, return errorlevel of 1.
else if wscript.arguments.item(0) = "/?" then
usage(0)
end if
end if

' Decide where to send you.
if wscript.arguments.count = 2 then
if wscript.arguments.item(1) = "/f" then
  talk_file(wscript.arguments.item(0))
else
  usage(3) ' If second parameter is not "/f", return errorlevel of 3.
end if
else
talk(wscript.arguments.item(0))
end if

' Talking normally.
function talk(text)
speech.speak text
wscript.quit
end function

' Speaking the contents of a file.
function talk_file(file)
set fso = wscript.createobject("scripting.filesystemobject")
if not fso.fileexists(file) then
  usage(2) ' If file doesn't exist, return errorlevel of 2.
end if
set ofile = fso.opentextfile(file,1)
speech.speak ofile.readall()
wscript.quit
end function

' Usage:
function usage(errorlevel)
wscript.echo
wscript.echo "TALK ""Text"" [Parameters]"
    wscript.echo
    wscript.echo "Text - The phrase you want to be spoken."
    wscript.echo
    wscript.echo "[Parameters]:"
    wscript.echo "             /f - Read the contents of a file. ""Text"" changes to the file path."
wscript.echo
wscript.quit(errorlevel)
end function


Once again, use this like you would a real command, because it is.


Step 7: HIDE

After a few spills, I've finally made one that starts a batch file hidden.

I'm sick of explaining the syntax of my commands, just type in hide /? and you'll get it. 

One new thing I should mention, this creates a file in your AppData\Roaming file called Batch. You probably shouldn't touch that, thanks.

Now in VBScript!

--BATCH--

@setlocal enabledelayedexpansion
@echo off
if not exist %appdata%\Batch md Batch
cd %appdata%\batch
if not exist hide.vbs echo CreateObject^("Wscript.Shell"^).Run """" ^& WScript.Arguments^(0^) ^& """", 0, False> "hide.vbs"
if "%~1" equ "" goto usage
if "%~1" equ "/?" (
:usage
echo.
echo Starts a batch file hidden.
echo.
echo HIDE "file_path" "file_path"
echo.
echo Where "file_path" is the path to the file you want run hidden ^(wildcards are not accepted^).
echo.
endlocal
exit /b 1
)
:hide
set file=%~1
if not exist %file% (
echo.
echo File '%file%' not found.
echo.
endlocal
exit /b 2
)
wscript.exe "hide.vbs" "%file%" || endlocal && exit /b 3
shift
if "%~1" neq "" goto hide
endlocal
exit /b 0


--VBS--

on error resume next

' Test if it has been run correctly.
if wscript.fullname = "C:\Windows\System32\wscript.exe" then
wscript.echo "Please use ""cscript /nologo alarm.vbs [parameters]"" to invoke this script."
wscript.quit
end if


set fso = createobject("scripting.filesystemobject") ' Gives access to files.

' Error handling:
if wscript.arguments.count = 0 then
    usage(1)
else if wscript.arguments.item(0) = "/?" then
    usage(0)
else if not fso.fileexists(wscript.arguments.item(0)) then
    usage(2)
end if
end if
end if

' Inner workings:
set shell = createobject("wscript.shell")
for i = 0 to wscript.arguments.count
    if not isempty(wscript.arguments.item(i)) then
        shell.run """" & wscript.arguments.item(i) & """",0,false
    else
        wscript.quit
    end if
next

' Usage:
function usage(errorlevel)
    wscript.echo
    wscript.echo "Starts a batch file hidden."
    wscript.echo
    wscript.echo "HIDE ""file_path"" ""file_path"""
    wscript.echo
    wscript.echo "Where ""file_path"" is the path to the file you want run hidden (wildcards are not accepted)."
    wscript.echo
    wscript.quit(errorlevel)
end function

Step 8: ALARM

This one makes a 'beep' sound after waiting a certain amount of time (also displays a message, which is optional). Pretty self explanatory...

Now in 2 languages:

--BATCH--

@setlocal
@echo off
if "%1" equ "/?" goto usage
if "%1" equ "" (
:usage
echo.
echo ALARM:
echo Pauses execution and emits a tone when done.
echo.
echo USAGE:
echo ALARM time message
echo.
echo time - time in seconds to wait ^(up to 99999^).
echo message - Specifies message to be displayed ^(optional^).
echo.
endlocal
exit /b
)
if "%~2" neq "" echo %~2 & echo.
>nul timeout %1 /NOBREAK
>tmp echo
type tmp
del tmp
endlocal
exit /b


--VBS--

on error resume next

' Test if it has been run correctly.
if wscript.fullname = "C:\Windows\System32\wscript.exe" then
wscript.echo "Please use ""cscript /nologo alarm.vbs [parameters]"" to invoke this script."
wscript.quit
end if


' Tests for bad arguments, no arguments, etc...
if wscript.arguments.count = 0 then
    usage(1)
else if not isnumeric(wscript.arguments.item(0)) then
    usage(2)
else if wscript.arguments.item(0) then
    usage(0)
end if

' Actual Stuff
wscript.echo wscript.arguments.item(1) ' Echo message.
wscript.sleep wscript.arguments.item(0) * 1000 ' Sleep script.
wscript.echo chr(007) ' Emitt beep.

' Usage:
function usage(errorlevel)
    wscript.echo
    wscript.echo "ALARM:"
    wscript.echo "Pauses execution and emits a tone when done."
    wscript.echo
    wscript.echo "USAGE:"
    wscript.echo "ALARM time message"
    wscript.echo
    wscript.echo "time - time in seconds to wait (up to 99999)."
    wscript.echo "message - Specifies message to be displayed (optional)."
    wscript.echo
    wscript.quit(errorlevel)
end function

Step 9: LEN

This one is in VBScript because there's already one for batch made. See the bottom for more information.

This function returns the length of a variable, phrase or anything like that. Note, without the usage and error handling, this would be a one-liner.

LEN.vbs:

' Test if it has been run correctly.
if wscript.fullname = "C:\Windows\System32\wscript.exe" then
wscript.echo "Please use ""cscript /nologo alarm.vbs [parameters]"" to invoke this script."
wscript.quit
end if


if wscript.arguments.count = 0 then
    usage
    wscript.quit(1)
else
    wscript.echo len(wscript.arguments.item(0))
end if

sub usage
    wscript.echo
    wscript.echo "Returns the length of a string."
    wscript.echo "LEN 'string'"
    wscript.echo
end sub


To capture the output into a variable, use this snippet:

for /f %%i in ('cscript /nologo LEN.vbs string') do set stringLength=%%i

For a batch version, see dostips.

Step 10: More

More will be coming soon!

I just need to figure out what to do next.

If you want anything, or see something that could be improved, feel free to leave a comment!

Comments

Batchcc (author)2015-12-31

Are you going to post more codes? I really like your batch ibles

https://www.instructables.com/id/Batch-codes/

More batch codes :)

About This Instructable

5,751views

10favorites

License:

Bio: I enjoy anything to do with computers. I have made some interesting scripts, and I am constantly improving my skill set. One day I hope ... More »
More by Prof. Pickle:How to Create your own Commands with BatchStart a batch file completely hiddenTips on how to find errors in code (focused on DOS)
Add instructable to: