I'm using this code to set some data into array but, i'm stuck on echoing the last one who has this character "?"
So; my question is how can i escape this kind of character "?" to echo the last item like "Hackoo?"
#echo off
setlocal enabledelayedexpansion
set i=0
for %%a in ("1" "2" "223231" "AAA" "Hello^ | World" "(Hello^)(World^)" "Hackoo^?") do (
set /a i+=1
set Line[!i!]="%%~a"
)
set Line
pause
Using FOR without any switch the CMD will try to get a path if your using the special char ?.
A solution is the substitution if you can't use any other switch (like : /F) :
#echo off
setlocal enabledelayedexpansion
set $List="1" "2" "223231" "AAA" "Hello | World" "(Hello)(World)" "Hackoo ?"
set $List=%$List:?=#%
set i=0
for %%a in (%$List%) do (
set /a i+=1
set "$Tmp=%%a"
set "Line[!i!]=!$Tmp:#=?!"
)
set Line
pause
#echo off
for /f "tokens=1-4" %%a in ("? * ; %%") do (
echo %%a
echo %%b
echo %%c
echo %%d
)
pause
Related
If I happened to have a string such as hello, how would I remove the nth character from the string?I tried this, but it didn't seem to work (3 is the position):
#ECHO ON
SET "input=%~1"
CALL :a input
SETLOCAL ENABLEDELAYEDEXPANSION
:a
SET "input=!%1!"
SET /A "position=3"
FOR /L %%a IN (!position!,1,!position!) DO (
SET /A "position2=%%a+1"
FOR /L %%b IN (!position2!,1,!position2!) DO (
ECHO "!input:~0,%%a!!input:~%%b!"
)
)
ENDLOCAL
#ECHO off
SET "input=%~1"
CALL :a input
goto :EOF
:a
SETLOCAL ENABLEDELAYEDEXPANSION
SET "input=!%1!"
set /A "position=3, position2=position+1"
echo "!input:~0,%position%!!input:~%position2%!"
Output example:
C:\Users\Antonio\Documents\Tests> test.bat hello
"helo"
In your original code the string is passed in the first parameter...
I would like write a batch file to count the number of occurrences of a specific character in each line of a text file.
For example, the count of \ in the string "aa\bb\cc\dd\" would be 4.
The find and the findstr show only the number of lines which is contains the exact character.
You might try the following script, providing the input string as (quoted) command line argument:
set "STRING=%~1$"
set STRING="%STRING:\=" "%"
set /A "COUNT=-1"
for %%E in (%STRING%) do set /A "COUNT+=1"
echo Count of `\`: %COUNT%
This replaces every character to be counted by " + SPACE + " and encloses the entire string in between "", so the input string aa\bb\cc\dd\ becomes "aa" "bb" "cc" "dd" "". The resulting string is fed into a for loop that recognises individual items to iterate through -- five in this case. The counter variable COUNT is initialised with a value of -1, so the result is not the number of iterated items but the separators, namely the \ characters present in the original string.
This approach fails if the string contains ? or * characters. It would also fail in case the character to count is one of the following: ", %, =, *, ~.
#echo off
setlocal
set "string=aa\bb\cc\dd\"
set "count=-1"
for %%a in ("%string:\=" "%") do set /A count+=1
echo %count%
This method works correctly as long as the string don't include wild-card characters: *?; if this is required, I would use the same npocmaka's method, but written in a simpler way:
#echo off
setlocal EnableDelayedExpansion
set "string=aa\bb\cc\dd\"
set "str=A%string%Z"
set "count=-1"
for /F "delims=" %%a in (^"!str:\^=^"^
% Do NOT remove this line %
^"!^") do (
set /A count+=1
)
echo %count%
While slow, you can try with this
#echo off
setlocal enableextensions disabledelayedexpansion
set "inputFile=input.txt"
set "searchChar=\"
for /f "delims=" %%a in ('
findstr /n "^" "%inputFile%"
') do for /f "delims=:" %%b in ("%%~a") do (
set "line=%%a"
for /f %%c in ('
cmd /u /v /e /q /c"(echo(!line:*:=!)"^|find /c "%searchChar%"
') do echo Line %%b has %%c characters
)
The input file is readed using findstr /n to get all the lines in the file with a number prefix (both for output "decoration" and to ensure all the lines in the file are processed). Each line is processed inside a pipe, from cmd to find. The cmd instance is started with unicode output (/u) so when the readed line is echoed, the output will be a two bytes sequence for each input character, one of them a 0x0 ASCII character. The find command sees the 0 as a line terminator, so we get each character in the input line as one separated line. Now, the find command counts in how many lines the searched character happens.
#ECHO OFF
SETLOCAL
SET "String=a\b\c\\\\d"
CALL :count "%string%" \
ECHO %tally%
GOTO :EOF
:count
SETLOCAL enabledelayedexpansion
SET /a tally=0
SET "$2=%~1"
:cloop
SET "$1=%$2%"
SET "$2=!$1:*%2=!"
IF "%$1%" neq "%$2%" SET /a tally+=1&GOTO cloop
endlocal&SET tally=%tally%
GOTO :eof
Here's a way to count particular characters in a string. It won't work for the usual suspects.
here's one way:
#echo off
:checkCountOf string countOf [rtnrVar]
:: checks count of a substring in a string
setlocal EnableDelayedExpansion
set "string=aa"
set "string=%~1"
set "checkCountOf=%~2"
if "%~1" equ "" (
if "%~3" neq "" (
endlocal & (
echo 0
set "%~3=0"
exit /b 0
)
) else (
endlocal & (
echo 0
exit /b 0
)
)
)
if "!checkCountOf!" equ "$" (
set "string=#%string%#"
set "string=!string:%checkCountOf%%checkCountOf%=#%checkCountOf%#%checkCountOf%#!"
) else (
set "string=$%string%$"
set "string=!string:%checkCountOf%%checkCountOf%=$%checkCountOf%$%checkCountOf%$!"
)
set LF=^
rem ** Two empty lines are required
set /a counter=0
for %%L in ("!LF!") DO (
for /f "delims=" %%R in ("!checkCountOf!") do (
set "var=!string:%%~R%%~R=%%~L!"
set "var=!var:%%~R=%%~L!"
for /f "tokens=* delims=" %%# in ("!var!") do (
set /a counter=counter+1
)
)
)
if !counter! gtr 0 (
set /a counter=counter-1
)
if "%~3" neq "" (
endlocal & (
echo %counter%
set "%~3=%counter%"
)
) else (
endlocal & (
echo %counter%
)
)
you can call it like:
call ::checkCountOf "/aa/b/c/" "/" slashes
echo %slashes%
exit /b %errorlevel%
wont work with some special characters (",~ and !)
You can also use replacement and the :strlen function
Not tested extensively but works with your example.
#ECHO OFF
SETLOCAL disabledelayedexpansion
SET "String=\a\b\c\\\\d\\"
set "previous=%string%"
set /a count=0
:loop
set "newstg=%previous:*\=%"
IF NOT "%previous%"=="%newstg%" (
set /a count+=1
set "previous=%newstg%"
IF DEFINED previous goto loop
)
echo %count%
pause
GOTO :eof
Here is one more option. I don't think this is bullet proof with poison characters.
#ECHO OFF
SETLOCAL disabledelayedexpansion
SET "String=\\a\b\c\\\\d\\"
set i=0
set "x=%string%"
set "x=%x:\=" & set /A i+=1 & set "x=%"
echo %i%
pause
How do I extract everything between incno and the space ie12345678 in the example batch below and put it into a incno variable?
#echo off
Set mystring=test incno12345678 wo5555 locFred Street
Echo "my string=%mystring%"
Echo incno
Echo wo
Echo loc
The incno can be 8 to 11 digits long but will always be between incno and a space in the string
I am now having trouble assigning the %%d variable to the mystring variable now it is in a for loop. Can anyone help me with this? See Below.
#echo off
SETLOCAL enableDelayedExpansion
color 0B
For /d %%d in ("C:\Users\%Username%\LocalData\*") do (
Echo "Folder = %%d"
Set mystring=%%d
echo "MyString = %mystring%"
pause
REM delete until (including) "incno":
set mystring=%mystring:*incno=%
echo %mystring%
REM delete starting with (including) " ":
set mystring=%mystring: =&rem %
echo "Incident Number = %mystring%"
pause
)
you can do it with substring replacement in two steps:
Set mystring=test incno12345678 wo5555 locFred Street
echo %mystring%
REM delete until (including) "incno":
set mystring=%mystring:*incno=%
echo %mystring%
REM delete starting with (including) " ":
set mystring=%mystring: =&rem %
echo %mystring%
set "mystring=test incno12345678 wo5555 locFred Street"
for /f %%V in ("%mystring:* incno=%") do set "incno=%%V"
Delayed expansion can be used if there is a chance that mystring might contain poison characters plus quotes:
setlocal enableDelayedExpansion
set mystring=test incno12345678 wo5555 loc"4th & Main"
for /f %%V in ("!mystring:* incno=!") do set "incno=%%V"
If the resultant value might contain ! (not an issue in this case) then delayed expansion must be toggled
setlocal disableDelayedExpansion
set mystring=test varABC!XYZ "&)^|<>"
setlocal enableDelayedExpansion
for /f %%V in ("!mystring:* var=!") do (
endlocal
set "var=%%V"
)
My string is "&&";"XMLWrirter";"class";"&&"
I want to spilt this string based on ; delimiter.
And when I do like below , I am getting first word only printed and count as 1.
setlocal ENABLEDELAYEDEXPANSION
set argC=0
for /f "delims=;" %%x in (""&&";"XMLWrirter";"class";"&&"") do (Set /A argC+=1
echo tokenitem %%x)
echo tokencount !argC!
Thanks in advance!!!
Why so complicated? This works just fine:
SETLOCAL ENABLEDELAYEDEXPANSION
SET argC=0
FOR %%x IN ("&&";"XMLWrirter";"class";"&&") DO (
SET /A argC+=1
ECHO tokenitem %%x
)
ECHO tokencount !argC!
I have an input let's say:
DummyString-v1.0.0
and I would like to extract everything after v to get: 1.0.0
How can I achieve this in CMD?
#ECHO OFF
SETLOCAL
SET "string=DummyString-v1.0.0"
SET "string=%string:*v=%"
ECHO %string%
GOTO :EOF
where the :*substring=replacementstring syntax is applied meaning "every character form the start of the variable (string) up to the substring (v) is replaced by the replacement string (nothing).
#echo off
call :split "DummyString-v1.0.0" "v"
echo %last_part%
exit /b 0
:split
set "string=%~1"
set "splitter=%~2"
setlocal EnableDelayedExpansion
set LF=^
rem ** Two empty lines are required
echo off
for %%L in ("!LF!") DO (
for /f "delims=" %%R in ("!splitter!") do (
set "var=!string:%%R=%%L!"
)
)
for /f "delims=" %%P in (""!var!"") DO set "last_part=%%~P"
endlocal &(
set last_part=%last_part%
exit /b 0
)