Replacing angle brackets in variables - windows

I have a plain text file with one url per line, enclosed with <link></link> tags. ECHO-ing the variablee (including the tags) works fine but now I'd like to remove the tags. escaping the angle brackets with one or multiple ^ does not work.
here's the code
FOR /F "tokens=* USEBACKQ" %%F IN (`findstr "<link>" test.txt`) DO (
SET what=%%F
SET result=%what:<link>=%
ECHO %result%
)
is there another way of doing it?

You need delayed expansion and quotes when you are using > or <:
#echo off
setlocal enableDelayedExpansion
FOR /F "tokens=* USEBACKQ" %%F IN (`findstr "<link>" test.txt`) DO (
SET "what=%%F"
SET "result=!what:<link>=!"
ECHO !result!
)
endlocal

Related

Exclamation mark removed as for variable assignment side effect

I'm trying to process property file and replace some of the properties.
While I managed to implement it with
#echo OFF
set "file=my.properties"
(for /F "tokens=1* delims=]" %%A in ('type "%file%" ^| find /V /N ""') do (
echo.%%B
)) > output.txt
as a side effect all exclamation marks (!) were removed from result file. Is this a bug in windows batch or am I missing something?
You could solve the problem with the exclamation marks with a setlocal DisableDelayedExpansion.
Because with enabled delayed expansion, the expansion of %%B drops the exclamation marks.
But there are some more problems.
Using echo. is slow and fails with some content in your file like \..\windows\system32\calc.exe.
delims=] removes the line number from FIND /N but it will also remove all ] at the beginning of lines.
A better technic is to toggle the delayed expansion mode.
#echo OFF
set "file=my.properties"
setlocal DisableDelayedExpansion
(for /F "tokens=* delims=" %%A in ('findstr /n "^" "%file%"') do (
set "line=%%A"
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
(echo(!line!)
endlocal
)) > output.txt

Batch script skipping blank entries in a .CSV when delim is ','

I have a .CSV that I am trying to sort through to create another file from the data, but when I run it through, it skips blank entries. For example, if a line is
value,value,value,,,value
and I try to get the 4th column, it would spit out 6th. Presumably because it is the next valid value. I don't want it to skip the blank entry as it can mess up the tables I'm trying to make. Anyone know how to resolve this? (Any tips are welcome as I suck at batch scripts)
Here is my script:
FOR /F "tokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14 delims=," %%a in (file.csv) DO (
echo %%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n
)
pause
This is the standard behaviour of the FOR/F loop, consecutive delims only used as one delimiter.
But you can use a workaround with a second FOR/F.
Prefix each column with another character, split the line at the delim and remove the prefix.
setlocal EnableDelayedExpansion
FOR /F "delims=" %%L in (test.bat) DO (
set "line=%%L,,,,,,,,"
set "line=#!line:,=,#!"
FOR /F "tokens=1,2,3,4 delims=," %%a in ("!line!") DO (
set "param1=%%a"
set "param2=%%b"
set "param3=%%c"
set "param4=%%d"
set "param1=!param1:~1!"
set "param2=!param2:~1!"
set "param3=!param3:~1!"
set "param4=!param4:~1!"
echo !param1! !param2! !param3! !param4!
)
)
As jeb already mentiones in his answer, for /F treats consecutive delimiters as one. To avoid that, you could replace each delimiter , by "," and enclose each full line by "", so each field appears as being enclosed within "", which can easily be removed finally by the ~ modifier of the for /F variable; so there is no need to do any more string manipulations (like sub-string expansion) later on:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "usebackq delims=" %%# in ("file.csv") do (
set "LINE=%%#"
setlocal EnableDelayedExpansion
for /F "tokens=1-4 delims=," %%A in (^""!LINE:,="^,"!"^") do (
endlocal
echo Field 1: %%~A
echo Field 2: %%~B
echo Field 3: %%~C
echo Field 4: %%~D
setlocal EnableDelayedExpansion
)
endlocal
)
endlocal
This might not work properly if the data contain , characters by themselves (remember that , is not considered as delimiter in case a field is enclosed within "" in the original CSV file).
The toggling of delayed expansion is done to not lose any exclamation marks in the data.

Windows CMD FOR loop

I'm trying to make a code which will get first words from all lines of HELP's output to a variable and echo this variable. Here is my code:
#echo off
set a=
for /F "tokens=1,*" %%i in ('help') do (
set a=%a% %%i
)
echo %a%
But it returns first word from only last line. Why?
Bali C solved your problem as stated, but it looks to me like you are trying to get a list of commands found in HELP.
Some of the commands appear on multiple lines, so you get some extraneous words. Also there is a leading and trailing line beginning with "For" on an English machine that is not wanted.
Here is a short script for an English machine that will build a list of commands. The FINDSTR command will have to change for different languages.
#echo off
setlocal enableDelayedExpansion
set "cmds="
for /f "eol= delims=." %%A in ('help^|findstr /bv "For"') do (
for /f %%B in ("%%A") do set "cmds=!cmds! %%B"
)
set "cmds=%cmds:~1%"
echo %cmds%
EDIT
Ansgar Wiechers came up with a more efficient algorithm to extract just the command names at https://stackoverflow.com/a/12733642/1012053 that I believe should work with all languages. I've used his idea to simplify the code below.
#echo off
setlocal enableDelayedExpansion
set "cmds="
for /f %%A in ('help^|findstr /brc:"[A-Z][A-Z]* "') do set "cmds=!cmds! %%A"
set "cmds=%cmds:~1%"
echo %cmds%
You need to use delayed expansion in your for loop
#echo off
setlocal enabledelayedexpansion
set a=
for /F "tokens=1,*" %%i in ('help') do (
set a=!a! %%i
)
echo %a%
Instead of using %'s around the a variable, you use !'s to use delayed expansion.
Because the echo is outside the do ( ...... )
#echo off
for /F "tokens=1,*" %%i in ('help') do (
echo %%i
)
and no need to print a, you can use directly %%i.
Another very simple example could be a batch like this saved as help1.cmd
#echo off
for /F "tokens=1,*" %%i in ('help') do (
if /I "%%i" EQU "%1" echo %%j
)
and you call this batch like
help1 MKDIR
to get the short help text for the MKDIR command

Windows batch: delayed expansion in a for loop

I want to modify a few specific lines of numbers of text files, and I wrote a batch file as follows:
#echo off
set n=0
set n1=10
set n2=40
cd.>output.txt
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set "var=%%i"
setlocal enabledelayedexpansion
set /a n=!n!+1
echo.!n!
set var=!var:*:=!
rem if !n!=%n1% ...
rem if !n!=%n2% ...
(echo.!var!)>>output.txt
endlocal
)
start output.txt
However, this doesn't work as expected.
After some tests, I think the !n! expansion is not normally delayed. That is very strange, because !var! expansion is normally delayed.
By the way, the setlocal enabledelayedexpansion and endlocal commands are put in the for loop, because otherwise the special character ! will be abandoned.
I suppose the problem what you see is that n will never increase.
But that isn't a problem of the delayed expansion, it's an effefct of the setlocal/endlocal block inside the loop.
As #panda-34 mentioned you should use the extended syntax of set/a and move the statement outside the setlocal/endlocal block.
#echo off
set n=0
set n1=10
set n2=40
(
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set "var=%%i"
set /a n+=1
setlocal enabledelayedexpansion
echo !n!
set var=!var:*:=!
rem if !n!=%n1% ...
rem if !n!=%n2% ...
(echo(!var!)
endlocal
)
) >output.txt
start output.txt

Read stdin stream in a batch file

Is it possible to use a piped stdin stream inside a batch file?
I want to be able to redirect the output of one command into my batch file process.bat list so:
C:\>someOtherProgram.exe | process.bat
My first attempt looked like:
echo OFF
setlocal
:again
set /p inputLine=""
echo.%inputLine%
if not (%inputLine%)==() goto again
endlocal
:End
When I test it with type testFile.txt | process.bat it prints out the first line repeatedly.
Is there another way?
set /p doesn't work with pipes, it takes one (randomly) line from the input.
But you can use more inside of an for-loop.
#echo off
setlocal
for /F "tokens=*" %%a in ('more') do (
echo #%%a
)
But this fails with lines beginning with a semicolon (as the FOR-LOOP-standard of eol is ;).
And it can't read empty lines.
But with findstr you can solve this too, it prefix each line with the linenumber, so you never get empty lines.
And then the prefix is removed to the first colon.
#echo off
setlocal DisableDelayedExpansion
for /F "tokens=*" %%a in ('findstr /n "^"') do (
set "line=%%a"
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
echo(!line!
endlocal
)
Alternatively, on some environments (like WinRE) that don't include findstr, an alternative with find.exe might suffice. find will accept a null search string "", and allows search inversion. This would allow something like this:
#echo off
setlocal DisableDelayedExpansion
for /F "tokens=*" %%a in ('find /v ""') do (
...
The set "line=!line:*:=!" syntax is:
set requires one parameter that is a=b.
If a contains a space or something, you'll have to use the quotation marks around this parameter. Here I don't see any
!line:*:=!
For this syntax, you can type 'set /?' to see the official description on using variables.
!var! is like %var%, to get the value. But !var! means delayed expansion.
line var name
the first : variable modification mark.
**:= **:=(empty), replace the string in the variable's value matches "*:"(virtually from the string start to first : occurence) with (empty), i.e. delete the substring from start to first colon.
FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
> CON ECHO.%%B
>> %File% ECHO.%%B
)
Source here: http://www.robvanderwoude.com/unixports.php#TEE
Alternatively, on some environments (like WinRE) that don't include findstr, an alternative with find.exe might suffice. find will accept a null search string "", and allows search inversion. This would allow something like this:
#echo off
setlocal DisableDelayedExpansion
for /F "tokens=*" %%a in ('find /v ""') do (
set "line=%%a"
echo(!line!
)

Resources