Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: Set Iteration - process each item in a set[Next]: SleepSeconds() - delay processing for the specified number of seconds
\->Batch Files->SetChar() - sets up BEEP, TAB characters etc

SetChar() [sets up BEEP, TAB characters etc].vbs.cmd

This batch file shows how you can make use of difficult characters such as beep and tab codes. Beeps can be used when outputting an error message and tabs can be used if you want to products a ".tsv" (tab seperated value file) which can be read in directly by Excel (for ease of use I recommend setting up an association for this as Microsoft don't do so for some strange reason).

While some text editors allow you to enter a tab, some convert them to spaces and most won't visually indicate that there is a tab making it dangerous to simply do somthing like the following (where you press the TAB key after the "="):

set TAB=
If you want to risk it then I recommend doing something along the lines of:
set TAB=   &REM There is one TAB character to the left of this

Its then clearly commented and also protects it from editors make may trim trailing whitespace.

There are basically 3 samples below one using VBS and two that don't (one uses my "filter$.exe" tool and the other uses "ForFiles.exe"). You could well ask why I created non vbscript versions, the reason is I needed to use it in a script that detects if VBSCRIPT is correct on a remote computer (which is where its running) so I was not able to correctly build a spreadsheet when VBSCRIPT was flakey :-(

One of the routines below uses "ForFiles.exe" this is a Microsoft tool and like too many of these they make arbitary changes making different versions incompatible, so unless you either provide your own copy or test well in the destination OS then I'd avoid this tool.

The batch file sample below contains VBSCRIPT (is created at run time) imbedded into it.

The "VBS4CMD.VBS" script was used to turn the tested VBSCRIPT into the batch file code to create it. That code was further tweeked.

If you want to change the code its probably easiest to let the batch file create the code and comment out the deletion. You will then be able to test and improve the code before running it through "VBS4CMD.VBS" again.

Depending on your code you may want to create the VBS once and use many times or create each time.

[anchor]

The Code for: "SetChar() [sets up BEEP, TAB characters etc].vbs.cmd"

This is the example, a shortcut was installed to this code :

@echo off
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
:: $Header:   C:/DBAREIS/Projects.PVCS/Win32/ScriptingTipsAndTricks/EXAMPLE[cmd].SetChar() [sets up BEEP, TAB characters etc].vbs.cmd.pvcs   1.0   29 Jun 2014 12:51:22   USER "Dennis"  $
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

setlocal ENABLEEXTENSIONS

::--- Call the routine to set up the beep and tab ---------------------------
echo Call the routine to set up the beep and tab
call :SetChar            BEEP 7
call :SetCharWithFilter$ TAB  9     &rem Will avoid VBS if possible

::--- Now use those characters to prove it worked :-) -----------------------
echo.
echo Used the created characters...
echo    * Tab%TAB%Tab%TAB%Tab%TAB%Tab%TAB%Tab
echo    * BEEP: "%BEEP%"
echo    * BEEP: "%BEEP%"

goto :EOF




::**************[v08.178]******************
:SetChar
::*****************************************
    set VbsFile=%TEMP%\SetChar-%random%.vbs
    set CmdFile=%TEMP%\SetChar-%random%.cmd
    echo if Wscript.Arguments.Count ^<^> 2 then                                                                        > "%VbsFile%"
    echo    wscript.echo "REM Invalid parameters, expected 2 (1=EnvVar 2=AsciiCode), got " ^& Wscript.Arguments.Count >> "%VbsFile%"
    echo else                                                                                                         >> "%VbsFile%"
    echo    wscript.echo "@echo off"                                                                                  >> "%VbsFile%"
    echo    wscript.echo "SET " ^& Wscript.Arguments(0) ^& "=" ^& chr(Wscript.Arguments(1))                           >> "%VbsFile%"
    echo end if                                                                                                       >> "%VbsFile%"
    cscript.exe //NoLogo "%VbsFile%" "%~1" "%~2"                                                                       > "%CmdFile%"
    call "%CmdFile%"
    del "%VbsFile%" >nul 2>&1
    del "%CmdFile%" >nul 2>&1
    goto :EOF


@REM **************[v14.234]******************
:SetCharWithFilter$
:: Uses "Filter$.exe" if its available (http://dennisbareis.com/freew32.htm#filter$)
@REM *****************************************
    ::--- If the EXE not available fall back to VBSCRIPT --------------------
    set FilterExe=
    for %%E in ("filter$.exe") do set FilterExe=%%~$PATH:E
    if "%ForFileExe%" == "" goto SetChar

    ::--- Filter$.exe found so use it, if errors detected fall back to VBSCRIPT ---
    set HexCode=00%~2
    set HexCode=%HexCode:~-2%
    set   TmpHexAsText=%TEMP%\SetChar-TEXT-%HexCode%-%random%.tmp
    set TmpHexAsBinary=%TEMP%\SetChar-BINARY-%HexCode%-%random%.tmp
        echo 5B %HexCode% 5D 0D 0A> "%TmpHexAsText%"
        filter$.exe -TxtToBin < "%TmpHexAsText%" > "%TmpHexAsBinary%" 2>nul
        echo [?] Just in case >> "%TmpHexAsBinary%"
        set /p HexChar=< "%TmpHexAsBinary%"
        if "%HexChar%" == ""  goto SetChar
        set    HexChar=%HexChar:~1,1%
    del "%TmpHexAsText%" >nul 2>&1
    del "%TmpHexAsBinary%" >nul 2>&1
    if "%HexChar%" == "?" goto SetChar
    set %~1=%HexChar%
    goto :EOF



@REM **************[v14.234]******************
:SetCharWithFORFILES
:: Uses (older WIN2000/WINXP) "FORFILES.exe" if its available
@REM *****************************************
    set ForFileExe=
    for %%E in ("ForFiles.Exe") do set ForFileExe=%%~$PATH:E
    if "%ForFileExe%" == "" goto SetChar
    set HexCode=00%~2
    set HexCode=0x%HexCode:~-2%
    set TmpHexFileSN=SetChar-%HexCode%-%random%.vbs
    set   TmpHexFile=%TEMP%\%TmpHexFileSN%
       set WinXpCMD=FORFILES.exe -m. -c"cmd.exe /c echo [%HexCode%]Hex character between the brackets :-)"& rem WIN7 version has different command line and is buggy also...
       set CMD=%WinXpCMD%
       %CMD%                  > "%TmpHexFile%" 2>nul
       echo [?] Just in case >> "%TmpHexFile%"
       set /p HexChar=< "%TmpHexFile%"
       echo HexChar="%HexChar%"
       if "%HexChar%" == "?" goto SetChar
       set    HexChar=%HexChar:~1,1%
    del "%TmpHexFile%" >nul 2>&1
    if "%HexChar%" == "?" goto SetChar
    set %~1=%HexChar%
    goto :EOF

Please note that that I am not trying to show how great I am by producing batch files 9,000 characters long on one line that no one will understand or be able to debug when they go wrong. I am going out of my way to comment the code and make it verbose so beginners and advanced users will both benefit. I don't claim to be an expert that knows everything, if I'm wrong or make a mistake then please contact me and let me know :-)


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: Set Iteration - process each item in a set[Next]: SleepSeconds() - delay processing for the specified number of seconds


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.