Handy Code

The Handy Code section of the site is where I keep notes on code lines that I find useful but don't use enough to remember. I also have a javascript millisecond to date converter, date calculator, etc. that comes in handy for cookie timing or, sadly, just planning my calendar.

Handy MySQL Commands

To reset an auto_increment field to 1:

ALTER TABLE tbl_name AUTO_INCREMENT = 1;
To load a local csv file into an empty existing table:

LOAD DATA LOCAL INFILE '/completePathToLocalData.csv' INTO TABLE tableName FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES;

This assumes you've already created your empty table with the same number of fields that your csv file has.

JavaScript Tips

Special Characters in Javascript

Sometimes you need to write special characters in javascript, and html character codes can't be used, because they don't get interpreted by javascript. You need to use unicode character codes instead.

HTML character code, wrong:

formElement.value = "Dirección";

You get: Dirección

Unicode character code, correct:

formElement.value = "Direcci\u00F3n";

You get: Dirección

Unicode character charts are located here: http://www.unicode.org/charts/. The chart you will most likely need is the Latin-1 chart. Character codes must be preceded by \u. That is, character code 00F3 is written in JavaScript as \u00F3.

Hope this helps.

UNIX/AWK Tricks

How to copy a long list of files into a directory

  1. Make your list of files, with complete paths, call it "imgList.txt". Put every file on a new line.
  2. Write a small awk script with the following 1 line:
    { printf("cp %s images/\n",$0);}
  3. Save the script as "copyList.awk"
  4. Do the following command:
    awk -f copyList.awk imgList.txt | sh

Brief explanation of the awk script:

  • %s means you're putting a string in that spot of your output,
  • $0 is the string you're putting there
  • $0 is a match for each line in the file "imgList.txt"
  • images/ is the directory to copy your files to (in this example relative to where the command will be run from - can be absolute, of course)
  • \n stands for new line

brief explanation of the command:

  • awk executes the awk code found in "copyList.awk" on the lines found in "imgList.txt" and pipes the output to the shell ( | sh).

Enjoy.

-Colin


Leave a Reply