Computing.Net > Forums > Programming > Extracting rows from multiple csv files using

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Extracting rows from multiple csv files using

Reply to Message Icon

Name: wodysgreen
Date: October 27, 2009 at 13:49:00 Pacific
OS: Windows 7
Subcategory: General
Comment:

Guys

I'm looking for a bit of code which would allow me to extract rows 3 to 34

from multiple csv files (over 100) and place the rows into one large csv for parsing.The csv files are exactly the same although different filenames and content. They reside in the same folder and it will always be rows 3 to 34 i'll want to extract. vbscript would be best as I want to add it another vbscript I have.

Any help is much appreciated.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: October 28, 2009 at 02:28:12 Pacific
Reply:

I dunno vbs [some helpers here do] but here's a bat fil;e:

===================================

@echo off > newfile & setLocal enableDELAYedexpansion

for %%c in (*.csv) do (
set N=
for /f "tokens=* delims= " %%a in (%%c) do (
set /a N+=1
if !N! geq 3 if !N! leq 34 (
>> newfile echo %%a
)
)
)


=====================================
Helping others achieve escape felicity

M2


0

Response Number 2
Name: wodysgreen
Date: October 28, 2009 at 11:27:03 Pacific
Reply:

Thanks for that, the only problem I have is that there is a blank row between data sets ie row 32 has data, row 33 is blank, row 34 has data which i don't need. Can the batch be run to extract rows from row 3 until it gets to a blank row.

Thanks for the solution.


0

Response Number 3
Name: Mechanix2Go
Date: October 28, 2009 at 11:32:42 Pacific
Reply:

Just do 3 to 32.


=====================================
Helping others achieve escape felicity

M2


0

Response Number 4
Name: wodysgreen
Date: October 28, 2009 at 11:48:26 Pacific
Reply:

Sorry Mechanix2Go, the csv files holds monthly data and each row of each file contains a days worth of data. Some files will hold 28, 30 or 31 rows of data. There is always a blank row after the last days data. Hence do until blank. Should have made this clear as I have appreciated your advise on other issues. Does this seem clear or would you like me to give an example of the file.

Thanks


0

Response Number 5
Name: Mechanix2Go
Date: October 28, 2009 at 12:20:16 Pacific
Reply:

Not clear yet. Is there anything after the blank row?


=====================================
Helping others achieve escape felicity

M2


0

Related Posts

See More



Response Number 6
Name: wodysgreen
Date: October 28, 2009 at 12:40:16 Pacific
Reply:

Yes, the data after the blank row is not needed.
The data required to be extracted always starts at row 3 and will end depending on the of number of days in the month, ie 28, 30 or 31.
So it will either extract rows 3 to 28, 3 to 30 or 3 to 31. Rows 1,2 and everything after the blank row is not needed.

Thanks for your persistence.


0

Response Number 7
Name: Razor2.3
Date: October 29, 2009 at 08:56:36 Pacific
Reply:

multiple csv files
Picked how? Just do everything in the directory that isn't the output.csv?


0

Response Number 8
Name: nbrane
Date: October 30, 2009 at 02:23:38 Pacific
Reply:

bscript, might work... you will know what needs to be
adapted to your situation.

dim fso
Path1=".\"
Set fso = createobject("Scripting.FileSystemObject")
Set Folder = fso.GetFolder(Path1)
File="test"
Dir=Path1
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set oFile = fso.OpenTextFile (Dir&File, ForAppending, True)
for each file in folder.files
fil=lcase(file.name)
p=instr(fil,".cvs")
if p>0 then
Set iFile = fso.OpenTextFile (Dir & file.name, ForReading)
for i=1 to 3
x=iFile.Readline
next
x="a"
do while iFile.AtEndOfStream=false and len(x)>0
x=ltrim(iFile.readline)
if ltrim(x)<>"" then
oFile.Writeline x
else
exit do
end if
loop
next

Wscript.Quit

this just a hack, not optimized, prob'ly a lot of redundant
code and not fully tested. I'm new to this stuff (vbscript.
javscript) myself so don't be offended if it (my code) sucks.


0

Response Number 9
Name: wodysgreen
Date: October 30, 2009 at 11:10:55 Pacific
Reply:

nbrane, never write 'don't be offended', its people like you who give guys like me a steer in the right direction, which is very much appreciated. I'll try the code later as I'm also new to all this. I did modify the scrpt from Mechanix2Go and I got round the issues of the different number of lines by getting the user to enter the number of days. It works but not ideal. It is me that should be saying 'don't be offended by my humble offerings of scripting'. Thanks to all who took the time to respond.

@echo off & setLocal enableDELAYedexpansion

del /Q K:\HFD\combined.csv
del /Q K:\HFD\TEAMImport.txt

set /P days="Enter the number of days in the month:"
set /A days+=2
set location=K:\HFD\%days%\

for %%c in (%location%*.csv) do (
set N=
for /f "tokens=* delims= " %%a in (%%c) do (
set /a N+=1
if !N! geq 3 if !N! leq %days% (
>> combined.csv echo %%a
)
)
)

move /Y "%location%*.*" "K:\HFD\Archive\"

cscript TEAMImport.vbs


0

Response Number 10
Name: Mechanix2Go
Date: November 12, 2009 at 00:13:39 Pacific
Reply:

This will find the blank for you:

=================================
@echo off & setLocal EnableDELAYedExpansion

set B=
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< my.csv') do (
set /a B+=1
if "%%b" equ "" goto :done
)
:done

echo firat blank at !B!


=====================================
Helping others achieve escape felicity

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Extracting rows from multiple csv files using

Extracting rows from pipe delimited file www.computing.net/answers/programming/extracting-rows-from-pipe-delimited-file/19338.html

delete x number of lines from a CSV file www.computing.net/answers/programming/delete-x-number-of-lines-from-a-csv-file/19820.html

batch to extract rows from files www.computing.net/answers/programming/batch-to-extract-rows-from-files/15625.html