Didn't notice, you changed the code, not sure if it'll work, but you need to escape symbols with direct expansion !
Code: Select all
set "$match=pause ^>nul "
Moderator: DosItHelp
Code: Select all
set "$match=pause ^>nul "
Code: Select all
for /f "tokens=1,*" %%a in ('findstr /b /i "printline" "file.txt"') do (
echo echo %%~b
)
Code: Select all
for /f "tokens=1,* usebackq" %%a in ("file.txt") do (
if /i "%%~a"=="printline" echo echo %%b
)
Code: Select all
@echo off &setlocal enableDelayedExpansion
set "$file=input.TXT"
set "$match=BBB"
for /f "usebackq tokens=*" %%? in (
"!$file!"
) do set "$$=%%~?" &set "$$=!$$:%$match%=!" &if /i ["!$$!"] neq ["%%~?"] (
::
echo. '!$match!' appears in line:
echo. '%%~?'
echo. $$=!$$!_
echo. the part where I define what to do when it finds the "BBB" string
)
Code: Select all
@echo off
setlocal enabledelayedexpansion
set file=input.TXT
set match=BBB
for /f "tokens=* usebackq" %%a in ("%file%") do (
set $$=%%~a
set $$=!$$:%$match%=!
if /i "!$$!" neq "%%~a" (
echo '%match%' appears in line:
echo '%%~a'
echo $$=!$$!
echo the part where I define what to do when it finds the "%match%" string
)
)
I cannot agree, the use of ?,! or $ was chosen intentionally. Tokens a-Z should be kept free for real delimited input and not for delimit-less input. This is normally not a problem for simple scripting. But you are probably right, I am tooComplex.paultomasi wrote:Furthermore, the proper use of FOR is with a..z and A..Z. There is nothing clever about using '?' or some other non-alphabetic character. Your code serves only to confuse readers.
paultomasi wrote:Furthermore, the proper use of FOR is with a..z and A..Z. There is nothing clever about using '?' or some other non-alphabetic character.
Code: Select all
@echo off
set append=abcd
for /F "delims=" %%a in ("hello") do (
echo %%~a%append%
)
Code: Select all
@echo off
set append=abcd
for /F "delims=" %%a in ("hello") do (
echo %%~a%append%
)
Code: Select all
@echo off
set append=abcd
for %%a in ("hello") do (
echo %%~a%append%
)
Code: Select all
@echo off &setlocal enableDelayedExpansion
set "$file=input.TXT"
set "$match=BBB"
for /f "usebackq tokens=*" %%? in (
"!$file!"
) do set "$$=%%~?" &set "$$=!$$:%$match%=!" &if /i ["!$$!"] neq ["%%~?"] (
::
echo. '!$match!' appears in line:
echo. '%%~?'
echo. $$=!$$!_
echo. the part where I define what to do when it finds the "BBB" string
)
If %%a includes double-quotes as part of it's value then %%~a suppresses them. This is of particular concern when we are adding double-quotes around %%a. If it's value already includes them then we end up with two lots of double-quotes at both ends when %%a is expanded by DOS so, instead of ending up with something like: "text", we end up with: ""text"".QUESTION 1:
What is the difference between "%%~a" and "%%a"?
QUESTION 2:
How can I delimit the !$$! variable by its spaces (if it contains them) in this code:
Code: Select all
ECHO !$$:~ =!
Code: Select all
SET $$=!$$:~ =!
Code: Select all
for /f "tokens=1-26" %%a in ("%$$%") do (
echo %%a
echo %%b
echo %%c
:etc...
)
Code: Select all
printline /a Hello world.
Code: Select all
@echo off &setlocal enableDelayedExpansion
set "$file=input.TXT"
set "$match=printline "
for /f "usebackq tokens=*" %%? in (
"!$file!"
) do set "$$=%%~?" &set "$$=!$$:%$match%=!" &if /i ["!$$!"] neq ["%%~?"] (
::
echo !$$!
)
Code: Select all
@echo off &setlocal enableDelayedExpansion
set "$file=input.TXT"
set "$match=printline "
for /f "usebackq tokens=*" %%? in (
"!$file!"
) do set "$$=%%~?" &set "$$=!$$:%$match%=!" &if /i ["!$$!"] neq ["%%~?"] (
::
echo %%b
)
Code: Select all
@echo off &setlocal enableDelayedExpansion
set "$file=input.TXT"
set "$match=printline "
for /f "usebackq tokens=*" %%? in (
"!$file!"
) do set "$$=%%~?" &set "$$=!$$:%$match%=!" &if /i ["!$$!"] neq ["%%~?"] (
::
REM The /a part could be made to do something other than if there is a normal occurrence of "printline":
echo "Printline" exists in the file "input.txt"
REM The above is the alternate action taken when there is a value in the variable "%%a"
)
paultomasi wrote:Ed Dyreen
I've looked at your code and in my years of experience I can state, if it's your intention to help people then why attempt to make your code obscure and unconventional?
Code: Select all
@echo off&setlocal enabledelayedexpansion
set "file=input.txt"
set "match=printline "
for /f "usebackq tokens=1*" %%a in ("%file%") do (
set "check=%%~a"
set "check=!check:%match%=!"
if /i "!check!" NEQ "%%~a" (
echo:Found %match%. Put code here.
set "myvar=%%b"
))