user3087510
user3087510

Reputation: 11

Batch file: How to read and output a line from text file with special characters

I've got a batch file that reads lines from a text file and if they don't contain a string writes them out to another text file. However, I am getting an error:

My code:

:Read
setlocal EnableDelayedExpansion enableextensions
for /F "tokens=* delims=" %%i in ('findstr /n $ C:\Preview.txt') do set "str1=%%i"

echo !str1!

if not x%str1:"^/DATA^/"=%==x%str1% echo "!str1!" >> !_MERGEREPORTNAME! 2>&1

endlocal

I'm guessing it works fine up to the echo !str1! since it does echo the string i.e.

4:merge, edit: $/DEVSRC/2013/REL/13-REL-10/Databases/Product_Catalog/DATA/StateS cripts/RatePlan-WI-1/MLQ-StateScripts/s_Coverage.sql;C981~C981 -> $/DEVSRC/2013/ PCH/13-REL-10-P2/Databases/Product_Catalog/DATA/StateScripts/RatePlan-WI-1/MLQ-S tateScripts/s_Coverage.sql;C313

but then it errors out with "edit: was unexpected at this time."

Any help ideas would be appreciated.

EDIT: here is my text file which changes all of the time

Conflict (merge, edit): $/DEVSRC/2013/REL/13-REL-10/Databases/Product_Catalog/StateScripts/RatePlan-WI-1/MLQAgent-StateScripts/s_Coverage.sql;C981~C981 -> $/DEVSRC/2013/PCH/13-REL-10-P2/Databases/Product_Catalog/StateScripts/RatePlan-WI-1/MLQAgent-StateScripts/s_Coverage.sql;C655
merge, edit: $/DEVSRC/2013/REL/13-REL-10/Databases/Product_Catalog/DATA/BaseScripts/b_App_Fabric_Cache_Reset.sql;C981~C981 -> $/DEVSRC/2013/PCH/13-REL-10-P2/Databases/Product_Catalog/DATA/BaseScripts/b_App_Fabric_Cache_Reset.sql;C313
merge, edit: $/DEVSRC/2013/REL/13-REL-10/Databases/Product_Catalog/DATA/BaseScripts/b_Document_Type.sql;C981~C981 -> $/DEVSRC/2013/PCH/13-REL-10-P2/Databases/Product_Catalog/DATA/BaseScripts/b_Document_Type.sql;C313
merge, edit: $/DEVSRC/2013/REL/13-REL-10/Databases/Product_Catalog/StateScripts/RatePlan-WI-1/MLQ-StateScripts/s_Coverage.sql;C981~C981 -> $/DEVSRC/2013/PCH/13-REL-10-P2/Databases/Product_Catalog/StateScripts/RatePlan-WI-1/MLQ-StateScripts/s_Coverage.sql;C313

Can't answer my own question yet, but this worked:

this worked. thanks to foxidrive for the right direction to look

setlocal DisableDelayedExpansion enableextensions
for /F "tokens=* delims=" %%i in ('findstr /n $ C:\Preview.txt') do (set "str1=%%i"
setlocal EnableDelayedExpansion

echo !str1!

set "search=/DATA/"

echo "!str1!"|find "!search!">nul && (
echo found
echo "/DATA/ files excluded" >> !_MERGEREPORTNAME! 2>&1
)||(
echo not FOUND
echo "!str1!" >> !_MERGEREPORTNAME! 2>&1
)

)
endlocal

Upvotes: 1

Views: 568

Answers (2)

foxidrive
foxidrive

Reputation: 41234

This will echo the line if it doesn't /DATA/

echo "!str1!"|find "/DATA/">nul || echo "!str1!" >> !_MERGEREPORTNAME! 2>&1

Upvotes: 1

Magoo
Magoo

Reputation: 79983

I'd suggest

if not "x%str1:/DATA/=%"=="x%str1%" echo "!str1!" >> !_MERGEREPORTNAME! 2>&1

Upvotes: 0

Related Questions