Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: Sweep Directories.cmd - [Next]: Unknown Number of Parameters - uses the shift command
\->Batch Files->Trim() - Leading+Trailing Whitespace

Trim() [Leading+Trailing Whitespace].vbs.cmd

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: "Trim() [Leading+Trailing Whitespace].vbs.cmd"

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

@echo off
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
:: $Header:   C:/DBAREIS/Projects.PVCS/Win32/ScriptingTipsAndTricks/EXAMPLE[cmd].Trim() [Leading+Trailing Whitespace].vbs.cmd.pvcs   1.0   29 Jun 2014 12:51:22   USER "Dennis"  $
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

::--- Initialize ------------------------------------------------------------
set TrimVbsFile=%TEMP%\Trim[Leading+Trailing]Whitespace.%random%.vbs
call :CREATE_VBS_TRIM "%TrimVbsFile%"


::--- Do some trimming ------------------------------------------------------
set LogOnTime=        WorkGroup\Dennis          &rem
set LogOnUser=        6/06/2014 7:19:59 AM      &rem
    call :TRIM "LogOnTime" "%LogOnTime%"
    call :TRIM "LogOnUser" "%LogOnUser%"
echo.
echo RESULT(LogOnTime)="%LogOnTime%"
echo RESULT(LogOnUser)="%LogOnUser%"

::--- Cleanup ---------------------------------------------------------------
del  "%TrimVbsFile%" >nul 2>&1
goto :EOF


:===========================================================================
:TRIM
::    P1 = Name of an Environment Variable to get trimmed results
::    P2 = String to trim, uses VBSCRIPT's trim() to do so
::====================[ v14.157 ]============================================
    set TrimTmpFile=%TEMP%\TRIM-%random%.tmp
    echo %~2 | cscript.exe //NoLogo "%TrimVbsFile%" > "%TrimTmpFile%"
    set /P %~1= < "%TrimTmpFile%"
    del "%TrimTmpFile%" >nul 2>&1
    goto :EOF


::===========================================================================
:CREATE_VBS_TRIM
::  P1 = Full name of the VBS file to create
::===========================================================================
    echo '--- Takes one or more lines PIPED into STDIN and trims them to STDOUT --- > "%~1"
    echo.                                                                          >> "%~1"
    echo do while wscript.stdin.AtEndOfStream ^<^> true                            >> "%~1"
    echo    wscript.stdout.WriteLine trim(wscript.stdin.ReadLine)                  >> "%~1"
    echo loop                                                                      >> "%~1"
    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]: Sweep Directories.cmd - [Next]: Unknown Number of Parameters - uses the shift command


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.