Wednesday, October 30, 2013

DOS CMD BATCH echo to findstr problem

DOS CMD BATCH echo to findstr problem


As shown below, echo of a string, without surrounding quotes, generates the error “The system cannot find the file specified” but just adding a quote around each side of the echo’d string solves the problem.


C:\testscripts>echo <--cow | findstr /r "<--.*"
The system cannot find the file specified.

C:\testscripts>echo "<--cow" | findstr /r "<--.*"
"<--cow"


Tuesday, October 29, 2013

CMD DOS BATCH – Example of dynamic substring with subroutines

CMD DOS BATCH – Example of dynamic  substring with subroutines


This example illustrates the following:
  1. Substring
    1. Dynamic length based on the remaining length of the string from the start position.
  2. Subroutine
    1. With variable “by-reference
    2. Passing received variable from one subroutine to another
    3. Adding comma to each letter – in this case just using the count of characters. t



Test.cmd:


@echo off
cls
SETLOCAL enabledelayedexpansion
echo This example will pick up characters from 5 (b) to n in moon
set Stuff="The brown cow jumped over the moon"
echo Stuff starting=!Stuff!
call :mysr Stuff


goto:eof


:mysr
SETLOCAL enabledelayedexpansion
set SenSuffix=!%1!
set countTotal=0
call :StrToComSep SenSuffix countTotal
set startIndex=5
set /a length=!countTotal!-!startIndex!
Note -  Do not call the following statement inside a loop or if statement
CALL SET _SenSuffix=!%1:~%startIndex%,%length%!


echo _SenSuffix after modification=!_SenSuffix!
goto:eof


rem This subroutine takes a variable and places a comma after each character
rem and white space to be used as a deliminator
:StrToComSep <StrToDel> <countTotal>
setlocal enabledelayedexpansion
set string=!%1!#
set pos=0
:startLoop
       call set chr=%%string:~%pos%,1%%
       if !chr!==# goto done
       set varRebuild=!varRebuild!!chr!,
       set /a pos+=1
goto startLoop


:done
set varRebuild=!varRebuild:~0,-1!
ENDLOCAL & (SET %1=%varRebuild%
SET %2=%pos%)
goto:eof




Output:


This example will pick up characters from 5 (b) to n in moon
Stuff starting="The brown cow jumped over the moon"
_SenSuffix after modification=brown cow jumped over the moon"






tComma subroutine based on webpage:


Sunday, October 27, 2013

CMD DOS BATCH Example of adding a comma delimiter to each character in a string

CMD DOS BATCH  Example of adding a comma delimiter to each character in a string

This example will take a string and add a comma after each character.  This will mek the variable able to be itterated one character at a time as well as count the number of characters, etc.


SendIndividualCharacters.cmd:

echo off
cls
setlocal EnableDelayedExpansion
set myVar=AStringWithNoDelimiters
echo myVar 1 = !myVar!
call :StrToComSep myVar
echo myVar 2 = !myVar!
goto:eof


:StrToComSep
rem This subroutine takes a variable and places a comma after each character
rem and white space to be used as a deliminator
set string=!%1!#
set pos=0
:startLoop
        call set chr=%%string:~%pos%,1%%
        if !chr!==# goto done
        set varRebuild=!varRebuild!!chr!,
        set /a pos+=1
goto startLoop

:done
set varRebuild=!varRebuild:~0,-1!
set "%1=%varRebuild%"
goto:eof


Output from the above example:


myVar 1 = AStringWithNoDelimiters
myVar 2 = A,S,t,r,i,n,g,W,i,t,h,N,o,D,e,l,i,m,i,t,e,r,s





Note:  This example based on example on http://www.computerhope.com/forum/index.php?topic=112779.0;wap

CMD DOS BATCH – Example of Variable passed to subroutine-Function as reference

CMD DOS BATCH – Example of Variable passed to subroutine-Function  as reference

Quick example of how a variable passed to a DOS subroutine (or function) is by reference.

Steps:
1.       Begin with original value “MyOriginalString
2.       Pass variable with original string
3.       Subroutine appends the value “_MyAppendedValue
4.       Echo out the revised variable in the calling function echos as “MyOriginalString_MyAppendedValue

Test.cmd:

@echo off
cls
setlocal enabledelayedexpansion
set myVar=MyOriginalString
echo myVar 1 =!myVar!
call :mysub myVar
echo myVar 2 =!myVar!
goto:eof

:mysub
setlocal enabledelayedexpansion
echo local is set here
echo The NAME of the passed argument is:%1
echo The VALUE of the passed argument is:!%1!
set "holdValue=!%1!"
echo local is ended here
endlocal & set "%1=%holdValue%_MyAppendedValue"
goto:eof


Output example:

myVar 1 =MyOriginalString
local is set here
The NAME of the passed argument is:myVar
The VALUE of the passed argument is:MyOriginalString
local is ended here
myVar 2 =MyOriginalString_MyAppendedValue



Thursday, October 24, 2013

CMD DOS SET with colon and tilde for SUBSTRING

CMD DOS SET with colon and tilde for SUBSTRING


This example shows the meaning of the syntax of a SET statement with a tilde to specify starting and ending position to get a substring.  


Test7.cmd = regular expansion
Test8.cmd = delayed expansion



Test7.cmd:


@echo off
SET start=4
SET length=9
rem            123456789
rem        123456789abcdef
SET string=The quick brown fox jumps over the lazy dog
rem            |        |
echo %string%
rem note that the set variable must be different
CALL SET substring=%%string:~%start%,%length%%%
ECHO %substring%
goto:eof



Output:


C:\testscripts>test7
The quick brown fox jumps over the lazy dog
quick bro


C:\testscripts>


original source:  ss64.com - example
ss64.com had an example but did not explain it - this tries to explain it.


test8.cmd:
SETLOCAL ENABLEDDELAYEDEXPANSION version


This example takes text from position 2 and picks up 5 characters


@echo off
cls
SETLOCAL enabledelayedexpansion
set vwb_suffix=Flightoffancy
echo vwb_suffix 1 = !vwb_suffix!
set startIndex=2
set length=5
rem note that the set variable must be different
CALL SET _vwb_suffix=!vwb_suffix:~%startIndex%,%length%!
echo _vwb_suffix 2 = !_vwb_suffix!
goto:eof




Output:



vwb_suffix 1 = Flightoffancy
_vwb_suffix 2 = ighto


Friday, October 18, 2013

CMD DOS Example of taking QUOTES and SPACES out of variable with special symbols

CMD DOS Example of taking QUOTES and SPACES out of variable with special symbols

This example will take all the quotes and spaces out of a string that has special characters “<” and “>” by utilizing DELAYED EXPANSION.   Delay Expansion means that variables are not determined when the cmd file is parsed but at run time.  This means that special characters “<” and “>” are not treated as redirection characters.

Test.cmd:

@echo off
cls
setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
set myvar="<add key="MyKeyNameInQuotes" value="https://MySSLSite.MyDomainName.com/WebServices"/>"
echo myvar ORIGINAL VALUE = !myvar!
set myvar=!myvar:"=!
echo myvar ALL QUOTES TAKEN OUT = !myvar!
set myvar=!myvar: =!
echo myvar ALL SPACES TAKEN OUT = !myvar!
goto:eof


OUTPUT:

myvar ORIGINAL VALUE = "<add key="MyKeyNameInQuotes" value="https://MySSLSite.MyDomainName.com/WebServices"/>"
myvar ALL QUOTES TAKEN OUT = <add key=MyKeyNameInQuotes value=https://MySSLSite.MyDomainName.com/WebServices/>
myvar ALL SPACES TAKEN OUT = <addkey=MyKeyNameInQuotesvalue=https://MySSLSite.MyDomainName.com/WebServices/>


Monday, October 14, 2013

CMD DOS How to compare two files in command line and suppress prompt

CMD DOS How to compare two files in command line and suppress prompt


This example uses DOS “COMP” to make a comparison of two text files and return:

1.       Matches = Files compare OK
2.       Difference(s) found = Files are different sizes.


This example pipes in an “N” for no to the prompt

C:\TestComp>echo N | comp File1.txt File2.txt
Comparing File1.txt and File2.txt...
Files compare OK

Compare more files (Y/N) ?
C:\TestComp>echo N | comp File1.txt File2.txt
Comparing File1.txt and File2.txt...
Files are different sizes.

Compare more files (Y/N) ?
C:\TestComp>