split csv-file by in-file criteria

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ajaekel
Posts: 2
Joined: 16 Oct 2013 02:00

split csv-file by in-file criteria

#1 Post by ajaekel » 16 Oct 2013 02:08

Hello everyone,

I would like to split an existing csv-file into multiple files by an in-file criteria - let's call it filename.

I already found a script for doing this by amount of lines in this forum - but as I am not a coder, I am unfortunately not able to translate this into my purposes.

Does anyone know how to solve this?

Thanks & regards,
Achim

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: split csv-file by in-file criteria

#2 Post by foxidrive » 16 Oct 2013 06:50

Describe your problem with examples - someone here can figure out the most appropriate and robust method to solve it.

ajaekel
Posts: 2
Joined: 16 Oct 2013 02:00

Re: split csv-file by in-file criteria

#3 Post by ajaekel » 18 Oct 2013 04:21

Hi again,

for example, my csv-file contains the following rows:

record1;this is a test;filename1
record2;this is another test;filename2
record3;this is a 3rd test;filename1
record4;this is a 4th test;filename3
...

What I would like to get are 3 file containing the following records and named like the column filename (3rd column of file):

filename1:
record1;this is a test;filename1
record3;this is a 3rd test;filename1

filename2:
record2;this is another test;filename2

filename3:
record4;this is a 4th test;filename3

Is this somehow possible?

Thanks in advance & kind regards,
Achim

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: split csv-file by in-file criteria

#4 Post by foxidrive » 18 Oct 2013 12:21

Your request doesn't give enough information about which lines go into which files, unless your CSV file is really only 4 lines long.

pumi
Posts: 15
Joined: 05 Oct 2013 06:48
Location: Germany, BW

Re: split csv-file by in-file criteria

#5 Post by pumi » 18 Oct 2013 23:51

try this

Code: Select all

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /F "tokens=1,2,3 delims=;" %%A in (test.csv) do @echo %%A;%%B;%%C >> %%C

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: split csv-file by in-file criteria

#6 Post by foxidrive » 19 Oct 2013 00:53

That splits every record (and adds a space to the end).

The OP wants something a little different...

Post Reply