Discussion Locked

Convert file to Comma-Delimited format

0 helpful
14 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm looking for a way to convert a file to a comma delimited format *without* user intervention. I have a script that downloads a file from an AS/400 to a PC.

The problem is the file is delimited by spaces, and I need it delimited by commas (customer requirement). It's a flat file with about 12-15 fields. The only spaces in the file are delimiters. Any way to convert it using a script/program/macro?

Thank you.

14 replies

by jay eckles

This is what Perl is made for! It should be super-easy to write a quick and dirty Perl script to open the file, read each line, substitute spaces with commas, and save the new line to a new file with a .csv extension.

It might look something like this (bear with me; I'm working from memory here - no book):

#!/usr/local/bin/perl

open( SPACEFILE, "/path/to/spacedelimited.file" ) ;
open( CSV, ">/path/to/commaseparated.csv" ) ;
while( $line = ){
$line =~ s/ +/,/g ;
print CSV $line ;
}
close( SPACEFILE ) ;
close( CSV ) ;

I haven't tried running this, so I'm not sure if it will work perfectly, but it gives you the general idea. Also, I'm not sure about the regexp - it's been a while ;-). Finally, I want topoint out that while Perl works great for this, there's no reason you couldn't implement this program in any program language that is convenient to you.

Good luck.

Jay Eckles
www.jayeckles.com

by shanghai sam

I don't like to say "unacceptable", but I'm looking for a different option. I've never worked with Perl, not sure how to make this run. I really appreciate the detail in your answer! This would need to run on several PC's, as a client based tool. Perl is suited towards to server-based apps, right?

by spankil

Using Replace Command of VB Script you can achieve this.

Syntax.

Replace(Str, " ", ",")
this replaces Space with Comma.

by spankil

Using Replace Command of VB Script you can achieve this.

Syntax.

Replace(Str, " ", ",")
this replaces Space with Comma.

by shanghai sam

That sounds good...But I don't know how to build the rest of it. I'm sure it's correct, I just don't quite know how to carry it out right now. I'm a Network Administrator at an AS/400 shop (No Windows programmers). My programming/scripting knowledge is limited to some Javascript and some basic HTML and used on the intranet. I'm trying to learn as much as I can in my free time to integrate our systems using Windows tools instead of Client Access. I appreciate the suggestion, but my VB skills are just too minimal (non-existant) to script the FSO right now. I think I'll just be stuck w/ Client Access tools for now until I can improve my programming skills. If it's pretty easy to write the rest of the script, I'd love to learn more. Thanks for your time.

by rrv

For each character in the file you can replace space with comma using Replace function.
But first and foremost thing is you are downloading from AS400. if you are using Client access express you have transfer file from 400 to PC where you can set to format options. Or download it into Excel and use the field separator as comma instead.
All the best
Raja venkatesh

by joshua1

I'm trying to avoid being dependent on Client Access tools. Once I learn these, I will be able to port them to server based scripts for the intranet. Thanks for the suggestion.

by andrew

Here's a complete script to do it:

Set oFso = CreateObject("Scripting.FileSystemObject")
Set oTsIn = oFso.OpenTextFile("z:\spaces.txt", 1, False)
Set oTsOut = oFso.OpenTextFile("z:\commas.txt", 2, True)

Do
oTsOut.WriteLine Replace(oTsIn.ReadLine, " ", ",")
Loop Until oTsIn.AtEndOfStream

oTsIn.Close
oTsOut.Close
MsgBox "Done"

by articulated mandible

You say you have a script that downloads the file to a PC. I have a small (20k) executable that seems to fit the bill. When it is run, it finds every file in the directory where it resides with a "txt" extension, goes through each, and replaces every instance of the space character with a comma. Then it saves the file with a "csv" extension.

Unfortunately, your requirement of "without user intervention" is a bit tricky - the executable has to be run in order to work, and once it is finished, it ends and will need to be run again next time. I suppose that you could run it via whatever script you are using to download the file to the PC.

If you want to take a look at it, let me know, its freeware, and i'll be glad to email you a copy.

by joshua1

Sounds like that'll work great. I'd like to try it: joshua.mathews@ampad.com

This discussion is closed to new replies.