to insert row of columns for conents in csv file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sant
Posts: 5
Joined: 16 Mar 2014 07:20

to insert row of columns for conents in csv file

#1 Post by sant » 16 Mar 2014 08:51

i have csv file having the contents, now i have to insert column name for this contents i.e i need to insert 1st row on top of the contents present in the csv file.
please let me know how this can achived..
At present csv file will be like this:
dany.lessard dany.lessard@cgi.com Dany Lessard Dany Lessard 105960 A


i want this to be look like as below:
Loginid emailid FN LN FullName EMPID Status
dany.lessard dany.lessard@cgi.com Dany Lessard Dany Lessard 105960 A


Thank you...




Regards,
Santosh

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

Re: to insert row of columns for conents in csv file

#2 Post by foxidrive » 16 Mar 2014 09:18

You need to rewrite the entire file.

This renames file.csv and then creates a new file.csv with the header, and then appends the original file into the new file.

Code: Select all

@echo off
ren file.csv file.csv.old
echo Loginid emailid FN LN FullName EMPID Status>file.csv
type file.csv.old >> file.csv
del file.csv.old

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: to insert row of columns for conents in csv file

#3 Post by Squashman » 16 Mar 2014 18:10

Do you enjoy getting spam email. Not a good idea to make your user name for the forums as your email address. Should probably not post any valid email addresses for example data either.

sant
Posts: 5
Joined: 16 Mar 2014 07:20

Re: to insert row of columns for conents in csv file

#4 Post by sant » 16 Mar 2014 21:30

Hi,

Thnks for the reply. below code is just creating the column names in new csv file(without the contents).also its create Loginid emailid FN LN FullName EMPID Status headers in single column
below is the code i have tested.

@echo off
ren file.csv fileold.csv
echo Loginid emailid FN LN FullName EMPID Status>file.csv
type fileold.csv >> file.csv
del fileold.csv


fileold.csv-having only contents without column headers.

Please let me know if i am missing something..

Thank you

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

Re: to insert row of columns for conents in csv file

#5 Post by foxidrive » 16 Mar 2014 21:58

santuraj12@gmail.com wrote:
Thnks for the reply. below code is just creating the column names in new csv file(without the contents).also its create Loginid emailid FN LN FullName EMPID Status headers in single column


fileold.csv-having only contents without column headers.


The code works here. You start with a file called file.csv in the same folder as the batch file, and then launch the batch file.

Ensure that file.csv is not open in another program at the time.

sant
Posts: 5
Joined: 16 Mar 2014 07:20

Re: to insert row of columns for conents in csv file

#6 Post by sant » 16 Mar 2014 22:12

Hi,

whether i need to have only the data in file.csv?

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

Re: to insert row of columns for conents in csv file

#7 Post by foxidrive » 17 Mar 2014 00:57

I don't understand your question.

The code will put a header on the file, that's all it does.

sant
Posts: 5
Joined: 16 Mar 2014 07:20

Re: to insert row of columns for conents in csv file

#8 Post by sant » 18 Mar 2014 11:03

hi,

Thnks for your inputs, i was able to get what i had expected. below is the code i had written

1.csv -> file having only header
2.csv->file having contents/data

batch file code:

type 2.csv >> 1.csv

sant
Posts: 5
Joined: 16 Mar 2014 07:20

Re: to insert row of columns for conents in csv file

#9 Post by sant » 20 Mar 2014 00:39

Hi,

need your inputs once again

now i am having the requirments to split the contents of csv file. ex: if csv file is having 70K rows i need to split that in to 2 files having 35k in each.

how we can implement this through batch file code?

Thank you

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: to insert row of columns for conents in csv file

#10 Post by ShadowThief » 20 Mar 2014 05:10

santuraj12@gmail.com wrote:Hi,

need your inputs once again

now i am having the requirments to split the contents of csv file. ex: if csv file is having 70K rows i need to split that in to 2 files having 35k in each.

how we can implement this through batch file code?

Thank you

Does the csv file have to split regardless of size, or only after the file gets to a certain size?

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

Re: to insert row of columns for conents in csv file

#11 Post by foxidrive » 20 Mar 2014 05:28

This will give you lines 1 to 35000 in newfile1.csv
and lines 35001 to the end in file newfile2.csv

Code: Select all

findrepl /o:1:35000 <file.csv >newfile1.csv
findrepl /o:35001   <file.csv >newfile2.csv


This uses a helper batch file called `findrepl.bat` - download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

Place `findrepl.bat` in the same folder as the batch file or on the path.

Post Reply