Jump to content

CSV files combine into one


Recommended Posts

hi,

Post the code you have so far so we can offer more relevant help. :)

If you have no code yet, look in the help-file for _FileListToArray(), FileOpen(), FileRead(), FileWrite(), FileClose(), and try putting something together, if you run into difficulty post back with your code, we'll be eager to help ;)

Good luck,

-smartee

Link to comment
Share on other sites

hi,

Post the code you have so far so we can offer more relevant help. :)

If you have no code yet, look in the help-file for _FileListToArray(), FileOpen(), FileRead(), FileWrite(), FileClose(), and try putting something together, if you run into difficulty post back with your code, we'll be eager to help ;)

Good luck,

-smartee

Thanks. Here is my non- working code:

$FileList=_FileListToArray(@scriptdir)

_ArrayDisplay($FileList,"$FileList")
$newfile=FileOpen (@scriptDir & "Newfile.csv")

If $newfile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


for $i=0 to 3


$open= FileOpen(@scriptdir & "\" & $FileList[$i])
$read= fileread(@scriptdir & "\" & $FileList[$i])

filewrite($newfile,$read)
fileclose($newfile)
Next

Here's whats wrong with this: It is not even creating the new file, even though it claims it opened it and wrote to it. Also, I want it to only loop through the csv files, and not any files. One more thing, the number of files are unknown, so how do I define the i? Thanks a lot!

Link to comment
Share on other sites

  • Developers

Looks like you really need to start mastering some debugging skills.

Check what the exact name is you are trying to open.

Also your whole Loop logic is looking flawed. The fileOpen, Fileread and Fileclose are not correct.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Looks like you really need to start mastering some debugging skills.

Check what the exact name is you are trying to open.

Also your whole Loop logic is looking flawed. The fileOpen, Fileread and Fileclose are not correct.

Jos

I think the fileopen is good, since it is returning 1.. I was hoping Id get some help with the other 2 items..
Link to comment
Share on other sites

  • Developers

I think the fileopen is good, since it is returning 1.. I was hoping Id get some help with the other 2 items..

Sure its good, but it doesn't create the file you want... again what do you think the actual string is in the FileOpen() statement?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Sure its good, but it doesn't create the file you want... again what do you think the actual string is in the FileOpen() statement?

Got you, okay i fixed that, now it creates the file. It doesnt write any data to that file though..

$FileList=_FileListToArray(@scriptdir)

_ArrayDisplay($FileList,"$FileList")
_FileCreate(@scriptDir & "\Newfile.csv")
$newfile=FileOpen (@scriptDir & "\Newfile.csv")

If $newfile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


for $i=0 to 3


$open= FileOpen(@scriptdir & "\" & $FileList[$i])
$read= fileread(@scriptdir & "\" & $FileList[$i])

filewrite($newfile,$read)
fileclose($newfile)
Next
Edited by richietheprogrammer
Link to comment
Share on other sites

  • Developers

Already told you that the logic was flawed.

Look at helpfile for the fileopen and the Handle returned how to use that.

Also, you close the $Newfile in the first cycle!

So, take a little time to review the proper syntax and think your logic through.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Okay, here is my code. It writes the data of the file "1.csv" to the fil "Newfile.csv". My question now is, how can I loop through all the existing csv files in that folder, and writes them to Newfile.csv? Thanks again!

#Include <File.au3>
#Include <Array.au3>

$FileList=_FileListToArray(@scriptdir)
$ReadFile = (@scriptdir & "\" & "1.csv")
$TempFILE = @scriptDir & "\Newfile.csv"
If FileExists($TempFILE) Then FileDelete($TempFILE)
    If FileExists($ReadFile) Then
        $ReadFile = FileGetShortName( $ReadFile)
        $FILE = FileRead($ReadFile)
        FileWrite($TempFILE, $FILE)
    Else
        MsgBox(0x0, "Sorry", "The following file was not found     " & @CRLF  & $ReadFile & "    ")
EndIf
Edited by richietheprogrammer
Link to comment
Share on other sites

  • Developers

Ok... here is something to look at (not really tested)

#include<file.au3>
$FileList = _FileListToArray(@ScriptDir)
$newfile = FileOpen(@ScriptDir & "\Newfile.csv",2)
If $newfile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
; Loop through all files in the array
For $i = 1 To $FileList[0]-1
    $read = FileRead(@ScriptDir & "\" & $FileList[$i])
    FileWrite($newfile, $read)
Next
; Close output file
FileClose($newfile)
Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@Jos: I think you meant Ubound($FileList)-1 or $FileList[0] in your for loop

@richietheprogrammer: Now that we know you are not lazy, let the schooling begin ;)

Peruse this: :)

#include <File.au3> ; Required for _FileListToArray
#include <Array.au3> ; Required for _ArrayDisplay

; Lets specify our paths with no trailing slashes
Local $sSearchPath = @ScriptDir
Local $sPathOfMasterCSV = @ScriptDir & "\Master.csv"

; Declare variables for the file handles
Local $hMasterCSVFileHandle, $hEachCSVFileHandle

; Declare a variable to temporarily store each CSV file's contents
Local $sEachCSVFileContents

; $FileList=_FileListToArray(@scriptdir)
; The previous line would list all files and directories in @ScriptDir however, we are only interested in CSV files
; so we use the filter parameter of the function, we also only want files so we also specify 1 as the third parameter
Local $avCSVFiles = _FileListToArray($sSearchPath, "*.csv", 1)

; Wait, what does av stand for in the variable name above?
; It means Array variant, so at a glance I can tell what type of data my variable contains
; see http://www.autoitscript.com/autoit3/udfs/UDF_Standards.htm for some healthy coding conventions

; _FileListToArray sets @error in certain cases so lets check for that and exit if no files have been found
If @error = 4 Then
    MsgBox(0, "Error", "No Files Found.")
    Exit
EndIf

; Now that we know we found some files lets see what we got
_ArrayDisplay($avCSVFiles, "Result of _FileListToArray($sSearchPath, ""*.csv"", 1)")

; Open the Master CSV for appending
$hMasterCSVFileHandle = FileOpen($sPathOfMasterCSV, 1)

; Since $avCSVFiles[0] = Number of Files\Folders returned we can loop through the array like this
For $i = 1 To $avCSVFiles[0]

    ; Open each CSV for reading
    $hEachCSVFileHandle = FileOpen($sSearchPath & "\" & $avCSVFiles[$i], 0)

    ; Check if file opened for reading OK
    If $hEachCSVFileHandle = -1 Then
        MsgBox(0, "Error", "Unable to open file: """ & $sSearchPath & "\" & $avCSVFiles[$i] & """ for reading.")
        Exit
    EndIf

    ; Read each CSV
    $sEachCSVFileContents = FileRead($hEachCSVFileHandle)

    ; Write to Master CSV
    FileWrite($hMasterCSVFileHandle, $sEachCSVFileContents)

    ; Close each CSV
    FileClose($hEachCSVFileHandle)

Next

; Close Master CSV
FileClose($hMasterCSVFileHandle)

Hope this helps ;)

-smartee

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...