Archive for the ‘Tech Stuff’ Category

Merge all CSV or TXT files in a folder

Wednesday, May 2nd, 2007

 

Found this cool script to merge a bunch of CSVs into one big one. works great after you use my other post on XLS to CSV convertion.

Ron de Bruin (last update 17 March 2006)
 
1) Non VBA example from Dave Peterson 
2) VBA example I made based on Dave's example
 

Note: with a few small changes you can also use this for tx t files

Change *.csv to *.txt in example 1 and 2 and in the code also use Tab = true

 

Example 1

 

Merge all data from the csv files into a text file


1) Windows Start Button | Run
2) Type
cmd and hit enter ("command" in Win 98)
3) Go to the folder with the CSV files (for help enter "help cd")

4) Type copy *.csv all.txt and hit enter to copy all data in the files into all.txt.

5) Type exit and hit enter to close the DOS window

 

Now we must import the text file all.txt into Excel.

 

1) Open Excel

2) When you use File Open to open all.txt the Text Import Wizard will help you import the file

3) Choose Delimited

4) Next

5) Check Comma

6) Finish

 

 

Example 2
 
This code will ask you to browse to the folder with the csv files and after you click OK in this dialog
it merge all data into a txt file and then import and save it into a Excel file for you.
 
If you want to have more control then you can also use code from this page to open the csv files and copy.
http://www.rondebruin.nl/copy3.htm
Change xls in the code to csv if you want to use it.
 
 
Copy the code below into a normal module of a workbook :
 
Alt-F11
Insert>Module
Paste the macro
Alt q to go back to Excel
Alt F8 to open your macro list
Select Merge_CSV_Files and press Run
 
 
' Start Code
 
Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const STILL_ACTIVE = &H103


Public Sub ShellAndWait(ByVal PathName As String, Optional WindowState)
Dim hProg As Long
Dim hProcess As Long, ExitCode As Long
'fill in the missing parameter and execute the program
If IsMissing(WindowState) Then WindowState = 1
hProg = Shell(PathName, WindowState)
'hProg is a "process ID under Win32. To get the process handle:
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, hProg)
Do
'populate Exitcode variable
GetExitCodeProcess hProcess, ExitCode
DoEvents
Loop While ExitCode = STILL_ACTIVE
End Sub
 
 
Sub Merge_CSV_Files()
Dim BatFileName As String
Dim TXTFileName As String
Dim XLSFileName As String
Dim DefPath As String
Dim Wb As Workbook
Dim oApp As Object
Dim oFolder
Dim foldername
 
    'Create two temporary file names
BatFileName = Environ("Temp") & "\CollectCSVData" & Format(Now, "dd-mm-yy-h-mm-ss") & ".bat"
TXTFileName = Environ("Temp") & "\AllCSV" & Format(Now, "dd-mm-yy-h-mm-ss") & ".txt"
 
    ' Create path to xls file
DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If
XLSFileName = DefPath & "MasterCSV " & Format(Now, "dd-mm-yy h-mm-ss") & ".xls"
 
    'Browse to the folder with CSV files
Set oApp = CreateObject("Shell.Application")
Set oFolder = oApp.BrowseForFolder(0, "Select folder with CSV files", 512)
If Not oFolder Is Nothing Then
foldername = oFolder.Self.Path
If Right(foldername, 1) <> "\" Then
foldername = foldername & "\"
End If
 
        'Create the bat file
Open BatFileName For Output As #1
Print #1, "Copy " & Chr(34) & foldername & "*.csv" & Chr(34) & " " & TXTFileName
Close #1
 
        'Run the Bat file to collect all data from the CSV files into a TXT file
ShellAndWait BatFileName, 0
        If Dir(TXTFileName) = "" Then
MsgBox "There are no csv files in this folder"
Kill BatFileName
Exit Sub
End If
 
        'Open the TXT file in Excel
Application.ScreenUpdating = False
Workbooks.OpenText Filename:=TXTFileName, Origin:=xlWindows, StartRow _
:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=True, _
Space:=False, Other:=False
 
        'Save text file as a XLS file
Set Wb = ActiveWorkbook
Application.DisplayAlerts = False
Wb.SaveAs Filename:=XLSFileName, FileFormat:=xlWorkbookNormal
Application.DisplayAlerts = True
 
        Wb.Close savechanges:=False
MsgBox "You find the XLS file here: " & XLSFileName
        'Delete the bat and text file you have create
Kill BatFileName
Kill TXTFileName
 
        Application.ScreenUpdating = True
End If
End Sub

‘ End code

 

Batch convert XLS to CSV

Wednesday, May 2nd, 2007

Batch convert XLS to CSV.

This worked for me in Excel 2007.  Works great.  I saw software that does this for $19.99 up to $120.99.

After this you can Merge the CSVs into one file. 

Originally posted to a google group by Dave Peterson.


 

Option Explicit
Sub testme01()

    Application.ScreenUpdating = False

    Dim myFiles() As String
    Dim fCtr As Long
    Dim myFile As String
    Dim myPath As String
    Dim tempWkbk As Workbook
    Dim logWks As Worksheet
    Dim tempName As String
    Dim wks As Worksheet
    Dim oRow As Long

    ‘change to point at the folder to check
    myPath = "c:\my documents\excel\test"
    If Right(myPath, 1) <> "\" Then
        myPath = myPath & "\"
    End If

    myFile = Dir(myPath & "*.xls")
    If myFile = "" Then
        MsgBox "no files found"
        Exit Sub
    End If

    Set logWks = Workbooks.Add(1).Worksheets(1)
    logWks.Range("a1").Resize(1, 3).Value _
        = Array("WkbkName", "WkSheetName", "CSV Name")

    ‘get the list of files
    fCtr = 0
    Do While myFile <> ""
        fCtr = fCtr + 1
        ReDim Preserve myFiles(1 To fCtr)
        myFiles(fCtr) = myFile
        myFile = Dir()
    Loop

    If fCtr > 0 Then
        oRow = 1
        For fCtr = LBound(myFiles) To UBound(myFiles)
            Set tempWkbk = Nothing
            On Error Resume Next
            Set tempWkbk = Workbooks.Open(Filename:=myPath & myFiles(fCtr))
            On Error GoTo 0
            If tempWkbk Is Nothing Then
                logWks.Cells(oRow, "A").Value = "Error Opening: " _
                                                      & myFiles(fCtr)
                oRow = oRow + 1
            Else
                For Each wks In tempWkbk.Worksheets
                    With wks
                        If Application.CountA(.UsedRange) = 0 Then
                            ‘do nothing
                        Else
                            .Copy ‘to a new workbook
                            tempName = myPath & Trim(.Name) & ".csv"
                            Do
                                If Dir(tempName) = "" Then
                                    Exit Do
                                Else
                                    tempName = myPath & Trim(.Name) & "_" _
                                          & Format(Time, "hhmmss") & ".csv"
                                End If
                            Loop
                            oRow = oRow + 1
                            With ActiveWorkbook
                                .SaveAs Filename:=tempName, FileFormat:=xlCSV
                                .Close savechanges:=False
                            End With
                            logWks.Cells(oRow, "A").Value = myFiles(fCtr)
                            logWks.Cells(oRow, "b").Value = .Name
                            logWks.Cells(oRow, "C").Value = tempName
                        End If
                    End With
                Next wks
                tempWkbk.Close savechanges:=False
            End If
        Next fCtr
    End If

    With logWks.UsedRange
        .AutoFilter
        .Columns.AutoFit
    End With

    Application.ScreenUpdating = True

End Sub

If you’re new to macros, you may want to read David McRitchie’s intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Short course:
Start a new workbook
Hit alt-f11 to get to the VBE (where macros/UDF’s live)
hit ctrl-R to view the project explorer
Find your workbook.
should look like:   VBAProject (yourfilename.xls)  

right click on the project name
Insert, then Module
You should see the code window pop up on the right hand side

Paste the code in there.  

Modify the folder that contains the .xls files.
myPath = "c:\my documents\excel\test"
to what you need.

Now go back to excel.
Save your workbook (so you can run the macro)

When you want to run or rerun:
Open this workbook
(fix the folder name in the code if it changed)

click on Tools|macro|macros…
click on the macro name (testme01, but you can rename it!)
and then click run.

Backup Data from Windows using Knoppix Part 2

Thursday, April 5th, 2007
Q: I want to copy files to a windows box over the network

A:

  • Second Method

I boot with Knoppix from the CD drive. Once it has fully loaded to the KDE desktop, I open a konsole command window. I assume the the hda1 (C: drive) is a FAT32 drive, so I mount it with the command:

sudo mount -t vfat -o ro,users /dev/hda1 /mnt/hda1

If instead the the hda1 (C: drive) is a NTFS drive, the I mount it with the command:

sudo mount -t ntfs -o ro,users /dev/hda1 /mnt/hda1

Next I open up the GUI file manager (called konqueror).

sudo konqueror

Once that comes up, in the location bar, I use the following to authenticate to a Windows domain & then access a windows networking share.In the location bar:

smb:/domain\username@IP.address.of.machine

If you do not need to authenticate to a domain, the form is:

smb:/hostname

or

smb:/IP.address.of.machine

This allows me to access windows shares. I navigate to where I want to save the data, then hit the keys CTRL-T to open a tnew tabbed window. Then in that location bar type:

/mnt/hda1/

Now I see the hda1 (C: drive) contents. Just like in windows explorer, select the file & folders you want to copy, CTRL-C or right-click & copy them. Use the mouse & move over to the other tab with the smb connection, then CTRL-V or right-click paste to initiate the transfer. When you are done copying files, close the konqueror window, click the KDE K menu and choose to reboot/shutdown the PC. TO shutdown the PC by command:

sudo init 0
  • Other method, in which both directions are possible. Either start SAMBA or mount the remote share.
    • "sudo /etc/init.d/samba start", add users with "smbpasswd -a knoppix". After this, the home directory of user knoppix will be shared as readonly. If you need other shares, edit /etc/samba/smb.conf and restart samba (replace "start" with "restart").
    • "mkdir tmp/share; sudo mount -t smbfs -o username=Administrator //otherbox/share /tmp/share" (maybe start samba too to get rid of the warnings, especialy when using "nmblookup")
  • Or you can obtain a lissetup.sh ("wget http://users.volja.net/zejnovi/lissetup.sh" in console). After doing that, in console, type: "chmod +x lissetup.sh", and then "./lissetup.sh". After doing this, you can use the LAN browser in Konqueror.

Ajax Inline Editor with PHP and MySQL

Friday, March 30th, 2007

I found lots of scripts that do inline editing but I couldnt find one that updated the database.  This little beauty will, thanks to the Javascript from the link below.

My code is fairly sloppy but you get the idea from it.

Ok so using the js from the page below:
http://www.yvoschaap.com/index.php/weblog/ajax_inline_instant_update_text_20/

assuming you have a table called tblInLineEdit with fields id, name,descr

**ajaxtest.php**
<?
$db_user = "";
$db_pass = ""; // password here
$db = "";
$host = "";
$link = mysql_connect($host,$db_user,$db_pass) or die("Database connection broken");
mysql_select_db($db,$link) or die("Database connection unavailable – ".mysql_error() );
$result = mysql_query("SELECT * FROM tblInLineEdit");
//$row = mysql_fetch_assoc($result);
?>
<script type="text/javascript" src="instantedit.js"></script>
<table>
<? while ($row = mysql_fetch_assoc($result)) { ?>
<tr>
<td><span id="name-|||-<?php echo $row['id']; ?>" class="editText"><? echo $row['name']; ?></span></td>
<td><span id="descr-|||-<?php echo $row['id']; ?>" class="editText"><? echo $row['descr']; ?></span></td>
</tr>
<?php } ?>
</table>



**update.php**
<?
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0

$db_user = "";
$db_pass = "";
$db = "";
$host = "";
$link = mysql_connect($host,$db_user,$db_pass) or die("Database connection broken");
mysql_select_db($db,$link) or die("Database connection unavailable – ".mysql_error() );
$content = $_GET['content'];
list($fieldname, $id) = explode("-|||-",$_GET['fieldname']);
mysql_query("UPDATE tblInLineEdit SET $fieldname = ‘$content’ WHERE id = $id ") or die("blah failure – ".mysql_error() );
$result = mysql_query("SELECT * FROM tblInLineEdit WHERE id = $id");
$row = mysql_fetch_assoc($result);
echo $row["{$fieldname}"];
?>

Converting to MS Office Accounting 2007 Pro with Business Contact Manager 2007

Monday, February 5th, 2007

Well this years action pack came from MS.  We’ve upgraded all the office computers to Office Enterprises 2007.

Next step is to convert from Quickbooks 2005 and Commence as our mish mash of CRM and Billing systems to a more stream lined Microsoft solution.  Since my SBS is not R2 and uses SQL2000 I’m going to host the BCM/Acounting database on a workstation until I upgrade the server. 

So here;s the step by step:

  1. Install Office 2007 Enterprise edition.
  2. Pick the "main" workstation where the DB is going to sit.
  3. Make sure you are an administrator on the machine you are using.
  4. Copy the QB datafile on to the local computer.
  5. install Office Accounting 2007
  6. Select import Quickbooks data
  7. sit and wait.  Its hour 4 now and it hasnt finished a 64MB .qbw