Monday, June 30, 2014

Extract quoted data from a text file using dos batch script

In some cases,  you may need to remove the quote that contains the value you want to extract. Here's the small piece of script I used to "clean up" the value.

--start of script ---
:DeQuote
for /f "delims=" %%A in ('echo %%%1%%') do set %1=%%~A
goto :eof
---end of script----

Usage:
Call :DeQuote <variable name>

Here's an example. 

------------value.txt-------------
"Gas"
"Labels"
"Schedule"
"Location"
"Options"
---------end of value.txt----------

Here's the script to clean up the files

-------------start of script-------------

SETLOCAL EnableDelayedExpansion

del /q out.txt

for /f %%i in (value.txt) do (
set tee=%%i
call :DeQuote tee
echo !tee! >> out.txt)

goto :eof

:DeQuote
for /f "delims=" %%A in ('echo %%%1%%') do set %1=%%~A
echo %1
goto :eof

----------end of script-----------------

You will be able to get out.txt as below

------out.txt--------------
Gas 
Labels 
Schedule 
Location 
Options 
---------------------------