Solved How to remove multiple tabs while reading file (in batch)

April 2, 2012 at 09:36:48
Specs: Windows XP
Can somebody help me to finish this script?

0 down vote favorite

I have file like this: http://www.crocko.com/9A60004ABE604... (Please click on the right bottom. There is timer but I did not find upload server without timer. In the file are tabulators, thats why I uploaded it on server)

and this is script to read it:

@echo off
setlocal EnableDelayedExpansion

if "%1"=="" (
IF EXIST "..\_install\activate all regions.txt" (
SET FILE="..\_INSTALL\activate all regions.txt"
echo Setting !FILE! as your configuration file
) ELSE (
echo File not found. First argument is name of configuration file .txt.
pause
)
) else ( SET "FILE=%1")

for /F "skip=4 delims=pR tokens=1,2" %%a in ( 'reg query hkcu\environment /v temp' ) do set TAB=%%b

FOR /F "usebackq delims=!TAB! tokens=1-3" %%L IN (!FILE!) DO (
for /F "delims=!TAB! tokens=1-3" %%a in ("%%L") do echo A:"%%a"B:"%%b"C:"%%c"
REM echo %%L
)

I want to get output like:

A:"Directory name 1"B:"1"C:"Description to this line"
A:"Directory name 2"B:"1"C:"Description to this line"
A:"Directory name xxx"B:"1"C:"Description to this line"

The goal is to filter the tabs that are repeated (tab)(tab)(tab)(tab) and replace to one (tab). So the first step needs to result in columnA(tab)columnB(tab)columnC(tab) and then in the second step to parse it to get the columns into three variables.


See More: How to remove multiple tabs while reading file (in batch)

Report •


✔ Best Answer
April 2, 2012 at 13:11:29
It shouldn't CMD collapses all delims together, so [TAB][TAB] is perceived as just a single [TAB]
@for /f "usebackq tokens=1,2* delims=	" %%a in ("..\_INSTALL\activate all regions.txt") do @echo A:%%a B:%%b C:%%c

How To Ask Questions The Smart Way



#1
April 2, 2012 at 09:56:22
Your file did not download for me. Try using PasteBin if the file is small enough. I'd actually rather have it posted here, but very few people seem to know how to use the PRE tags.

How To Ask Questions The Smart Way


Report •

#2
April 2, 2012 at 11:26:12
Thanks for the link. Here is the file:
http://pastebin.com/66ucy5Mh

The text format is txt written for notepad. Originaly windows1250 and all numbers are well-leveled in one column. Between number and other text are tabulators


Report •

#3
April 2, 2012 at 11:43:36
So you're trying to consolidate tabs, so each column is separated by a single tab? Will there ever be more than one tab between column 2 and 3?

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
April 2, 2012 at 11:50:47
I do not really need tabs in the output but I need to process the columns like the tabs were consolidated. So there will not be any more tabs between col 2 and col 3. What I really need is to get col 1 , col 2 and col 3 .... the only problem is that the tabs in this situation causes incorrect values to be printed.

Report •

#5
April 2, 2012 at 13:11:29
✔ Best Answer
It shouldn't CMD collapses all delims together, so [TAB][TAB] is perceived as just a single [TAB]
@for /f "usebackq tokens=1,2* delims=	" %%a in ("..\_INSTALL\activate all regions.txt") do @echo A:%%a B:%%b C:%%c

How To Ask Questions The Smart Way


Report •

#6
April 2, 2012 at 14:26:27
I have already the answer. Problem is in !TAB! instead %TAB%:

So this is correct solution for the for loop:

FOR /F "usebackq delims=%TAB% tokens=1-3" %%A IN (!FILE!) DO (
 echo A:"%%A"B:"%%B"C:"%%C"
)


Report •

#7
April 2, 2012 at 15:00:17
You know you can just use the tab character, right? You don't need to hide it in a variable.

How To Ask Questions The Smart Way


Report •

#8
April 2, 2012 at 15:01:36
Incidentally, I clicked on wrong button and evaluated it down instead up. Sorry.

Report •


Ask Question