Solved Extract from text file not delimiting properly

Dell / Latitude e6420
February 19, 2014 at 07:54:18
Specs: Win 7 Pro x64, I7 Sandy Bridge 2.201 GHz / 8072 MB
Hello all,
This question is similar to one I posted a few days ago. Please forgive my noobishness. I'm a hardware guy.

I have created a text file that has network paths for a network share. ( IE Q:\Folder1\Folder2\etc..)

What I want to do is export that information to a csv file formatted like the following: Column1=Q, Column2=Folder 1, Column3=Folder2, etc. I believe this can be done by setting the delimiter to \

Here's what I came up with:
@echo off
setlocal enableextensions enabledelayedexpansion
for /f "usebackq tokens=* delims=\" %%G in (
"myfile.txt") do echo %%G, ::>> extract.csv

When the csv is generated it is not separating properly. There are only column A and column B. Can anyone please tell me what is wrong with my for loop?

~winipcfg

ASCII Question, Get an ANSI


See More: Extract from text file not delimiting properly

Report •

✔ Best Answer
February 19, 2014 at 14:41:15
I tend to agree with Razor's analysis overall, but batch does have it's strong points, if you take the time to learn of its subtle deceptions and quirks, which are many. I still use it quite often for quick-and-dirty, mostly for what it's meant for: file-system processing using or applying CMD verbs (commands) iteratively. The intra-file text processing, on the other hand, is trickier and for that I prefer vbscript. That said, so you won't bash ss64.com too hard, this would work, but it's clunky and inflexible:

for /f "usebackq tokens=1-3 delims=\:" %%G in ("myfile.txt") do echo %%G,%%H,%%I ::>> extract.csv

Or, for easier variation of column number (determined by var MAX):
set MAX=3
:: you can make this ever how long you might think is needed
set xx=%%G,%%H,%%I,%%J,%%K,%%L
:: this is good example of quirks in batch: the dbl percents treat as one
set /a L=MAX*3-1
set xx=!xx:~0,%L%!
for /f "usebackq tokens=1-%max% delims=\:" %%G in ("myfile.txt") do echo %xx%>>result.csv


As you can see, it is restricted to 'n' no. of columns based on whatever "tokens" has as its argument (1-3 is 3 columns max), but you can add more by changing the 3 to some other value, then adjusting the no. of letters (G,H,I...)
Obviously, Ivo's method is more elegant and more flexible.


message edited by nbrane



#1
February 19, 2014 at 08:20:40
@echo off & setlocal EnableDelayedExpansion
for /F "delims=" %%i in ('type "myfile.txt"') do (
  set row=%%i
  set row=!row::=!
  set row=!row:\=,!
  echo.!row!
) >> "extract.csv"


Report •

#2
February 19, 2014 at 08:24:24
Thanks Ivo. Can you please let me know why my batch didn't work like I expected it to?

~winipcfg

ASCII Question, Get an ANSI


Report •

#3
February 19, 2014 at 08:42:35
First of all try the solution I propose to see if it fits your need, then as you can see the code I posted is absolutely different from what you devised.

To explain in detail would need to teach how FOR /F loop works. In short words you need to read the source file and translate the backslash into comma; this is done by coding a SET statement while you use the FOR to parse the input file (and the parsing format is totally incorrect).

Sorry for my short comment, but the full explanation would require a huge post.


Report •

Related Solutions

#4
February 19, 2014 at 08:48:02
I'm trying it now. The text file has nearly 3 million lines so it's going to take awhile. I've been learning batch from ss64.com. Maybe there's something better to learn from. I dunno.

~winipcfg

ASCII Question, Get an ANSI


Report •

#5
February 19, 2014 at 08:51:36
winipcfg, you seem to have a problem. You want to know what your script is doing, but you keep turning ECHO off. Don't. Leave it on, and study the output. Also, open a command prompt, and type:
for /?
Read all of it.

EDIT: If you're just picking up scripting now, I suggest you pick a better language. VBScript / JScript will both be around for years to come, and support every major version of Windows since NT4, including the Win9x line. PowerShell is the hot new language, and would be able to do what you seem to want faster and easier.

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#6
February 19, 2014 at 10:44:49
Pretty sure I do not want anything to do with a full-fledged programming language. Took enough of that in college. It's not for me. I just want to get good at batch scripting, as I can occasionally use it to simplify things. I don't need programming logic too often, but when I do it's a real time saver. The problem is any books I've found on batch are antiquated. I did buy a new book but it was more of a command-line reference rather than teaching me batch.

~winipcfg

ASCII Question, Get an ANSI


Report •

#7
February 19, 2014 at 11:45:37
I respect that, but the trap of batch is that it seems simple at first blush. If you limit yourself to simple file system manipulation, it is.

The quicker you move away from that, such as manipulating a text file, the more gotcha's you'll encounter. You'll end up spending more time / effort getting your batch to a level resembling working than you would with any other language. You'll feel invested in batch, and the sunk cost fallacy claims another one.

How To Ask Questions The Smart Way


Report •

#8
February 19, 2014 at 14:41:15
✔ Best Answer
I tend to agree with Razor's analysis overall, but batch does have it's strong points, if you take the time to learn of its subtle deceptions and quirks, which are many. I still use it quite often for quick-and-dirty, mostly for what it's meant for: file-system processing using or applying CMD verbs (commands) iteratively. The intra-file text processing, on the other hand, is trickier and for that I prefer vbscript. That said, so you won't bash ss64.com too hard, this would work, but it's clunky and inflexible:

for /f "usebackq tokens=1-3 delims=\:" %%G in ("myfile.txt") do echo %%G,%%H,%%I ::>> extract.csv

Or, for easier variation of column number (determined by var MAX):
set MAX=3
:: you can make this ever how long you might think is needed
set xx=%%G,%%H,%%I,%%J,%%K,%%L
:: this is good example of quirks in batch: the dbl percents treat as one
set /a L=MAX*3-1
set xx=!xx:~0,%L%!
for /f "usebackq tokens=1-%max% delims=\:" %%G in ("myfile.txt") do echo %xx%>>result.csv


As you can see, it is restricted to 'n' no. of columns based on whatever "tokens" has as its argument (1-3 is 3 columns max), but you can add more by changing the 3 to some other value, then adjusting the no. of letters (G,H,I...)
Obviously, Ivo's method is more elegant and more flexible.


message edited by nbrane


Report •

Ask Question