Thursday, July 25, 2013

Get folder file names present into a file

Ever wanted to get the names of the files in a folder into a CSV file so that you can then do some processing with the file name. The easiest way to do that is to open a Command Prompt window in the directory where all the files are located and then type the following command:

dir /b>files.csv

The command above will create a file called “files.csv”. To get other details about the file into the files.csv, you can check other parameters of dir command using dir /?

Tuesday, June 18, 2013

Apex - CSV File Reader

This code also includes way of handling double quotes that are part of CSV file. Although I myself have not used it but looking at the code, this seems to work pretty well. I know there are certain limitations of file max size and all those limitations are applied here also. This code just gives you a ready code to include in your implementation. All comments are welcome.

PS: I do not take any credit / responsibility of the code.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    /**
     * Returns a List containing Lists of Strings that represents
     * the values contained in the CSV file.
     *
     * Each element in the outer list represents a row in the CSV file.
     * Each element in the inner list is the value in the field specified
     * by the row-column combination. 
     * contents - contains file contents retrieved using <apex:inputFile value="{!contents}" />
     */
    public static  List<List<String>> parseCSV(String contents,String sep,Boolean skipHeaders) {  
    
        List<List<String>> allFields = new List<List<String>>();  
    
         // we are not attempting to handle fields with a newline inside of them  
         // so, split on newline to get the spreadsheet rows  
        List<String> lines = new List<String>();  
        try {  
            // TODO: Need to check for \r\n too
            lines = contents.split('\n');  
        } catch (System.ListException e) {  
             System.debug('Limits exceeded?' + e.getMessage());  
        }  
    
        Integer num = 0;  
        for(String line : lines) {  
    
            // check for blank CSV lines (only commas)  
            if (line.replaceAll(',','').trim().length() == 0) continue;  
            if(line.trim().length()==0) continue;
            
            //Replace the Separator with ','
            line = line.replace(sep,',');
            
            List<String> fields = line.split(',');      
            List<String> cleanFields = new List<String>();  
    
            String compositeField;  
            Boolean makeCompositeField = false;  
    
            for(String field : fields) {  
                field = field.trim();
                if (field.startsWith('"') && field.endsWith('"')) {  
                    cleanFields.add(field.replaceAll('"',''));  
                    makeCompositeField = false;
             } else if (field.startsWith('"')) {  
                    compositeField += ',' + field;  
                    makeCompositeField = true;
                    compositeField = field;  
                } else if (field.endsWith('"')) {  
                    compositeField += ',' + field;  
                    cleanFields.add(compositeField.replaceAll('"',''));  
                    makeCompositeField = false;  
                } else if (makeCompositeField) {  
                     compositeField +=  ',' + field;  
                } else {  
                     cleanFields.add(field.replaceAll('"',''));  
                }  
      
            }  
            allFields.add(cleanFields);  
         }  
         if (skipHeaders) allFields.remove(0);  
         return allFields;         
     }

Friday, February 15, 2013

MySQL - Loading data into Date Fields from Input File

While working on one of the Salesforce Data Migration project, I was using MySQL to upload data to do all kinds of processing on the data. However I faced some issues while loading date and datetime fields.

I tried using LOAD DATA INFILE command

LOAD DATA INFILE '
InputFile.csv' INTO TABLE StageTask
  FIELDS TERMINATED
BY ',' ENCLOSED BY '"'
  
LINES TERMINATED BY '\n'
  
IGNORE 1 LINES;


The error I got was something like this

SQL Error (1292): Incorrect date value: '7/2/2006' for column 'ACTIVITYDATE' at row 1

To fix this issue, I used certain variables and then used them to actual columns after applying STR_TO_DATE function.



LOAD DATA INFILE 'InputFile.csv' INTO TABLE StageTask
  FIELDS TERMINATED
BY ',' ENCLOSED BY '"'
  
LINES TERMINATED BY '\n'
  
IGNORE 1 LINES
  
(ID, RECORDTYPEID, SUBJECT, @varACTIVITYDATE, STATUS,  

    @varCREATEDDATE, CREATEDBYID, @varLASTMODIFIEDDATE, LASTMODIFIEDBYID
    LEGACYACTIVITYID__C, ACTIVITY_TYPE__C, AUTOFOLLOWUP__C, @varCOMPLETED_DATE__C, 
    ORIGINALTASK__C, FOLLOWUPCREATED__C, PRODUCT_LINE__C, CreatedDateOnly
    LastModifiedDateOnly, @varActivityDateOriginal, @dummy)
  
SET ACTIVITYDATE = STR_TO_DATE(@varACTIVITYDATE, "%m/%d/%Y")

    CREATEDDATE = STR_TO_DATE(@varCREATEDDATE, "%Y-%m-%dT%H:%i:%s.000Z"),  
    LASTMODIFIEDDATE = STR_TO_DATE(@varLASTMODIFIEDDATE, "%Y-%m-%dT%H:%i:%s.000Z"),  
    COMPLETED_DATE__C = STR_TO_DATE(@varCOMPLETED_DATE__C, "%m/%d/%Y"),  
    ActivityDateOriginal = STR_TO_DATE(@varActivityDateOriginal, "%m/%d/%Y");


I created variables for each of Date field (see @var) that was not in yyyy-MM-dd format, changed the format using STR_TO_DATE function and BINGO!! it worked flawlessly.

For other date time formats refer to
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

For information on loading data from input file refer to
http://dev.mysql.com/doc/refman/5.1/en/load-data.html

Friday, January 25, 2013

Apex Code Templates

Today Josh Birk posted a blog on Developer Force regarding Apex Template i.e. code template for some of the common tasks and he started with the code template for Asynchronous Apex tasks. Here is the link - http://blogs.developerforce.com/developer-relations/2013/01/apex-template-asynchronous-apex.html