Scripting

Ramblings and links to interesting snippets


How to compress only new files in a folder

In the shell using tar, you can compress only files newer then a specific date :)  Suuuuuper handy if you’re passing off files added to a large website to someone who already has an old version of that site.  So, pretty easy, you just run this command:

sudo tar -N ‘2016-12-01 00:00:00‘ -jcvf compressed.tar.bz2 folder

  • The Date (example date is December 1st 2016 midnight)
  • Location for output file
  • Folder to copy

Permalink » No comments

How to convert a VirtulBox VM to a bootable drive

This is quite easy so I was surprised that I couldn’t find a walk-through anywhere…so here’s one :)  This is specifically for a VM of Ubuntu, so it’s possible there may be other hurdles in place for different operating systems.

Step 1

Create a VM normally in VirtualBox (or use an existing one), install the operating system, and add any programs or files you want to have readily available on the bootable disk.

Step 2

This is the tricky part, you can use Virtual Box to convert a vdi to an iso, but you have to do it from the terminal.  On my machine it wouldn’t work at all in windows default terminal, but I have Cygwin installed, and was able to use that.  You first need to cd into the directory where Virtual Box is installed, and run the following command.  The folder structure is specific to my machine and will most likely need to be changed on yours.

1
"/cygdrive/p/Programs/VirtualBox/VboxManage.exe" clonehd 'F:/Documents/Virtualbox/Ubuntu_2017/Ubuntu 2017.vdi' 'F:/Documents/Virtualbox/Ubuntu_2017.iso' --format RAW

More simply you need to run something like this:

1
VboxManage.exe clonehd 'vdi_location.vdi' 'where_generated_iso_will_be_location.iso' --format RAW

Step 3

Use rufus to install the ISO as a bootable image on your USB drive

And that’s about all you need to do!  Then you can take a carbon copy of your VM with you wherever you go :)  Great for travelling, which was my need.

Permalink » No comments

Fastest method for Javascript substring search

I have a mobile application that needs to search through a rather large array and I’m experiencing some optimization issues with it.  So, I decided to run some tests to see which function would be the fastest between indexOf, search, and includes.

I first tested all three with a static search value, search performed the best:

test1

But, with a variable, indexOf performed the best:

test2

I was able to perform these tests pretty easily using https://jsperf.com/ :)

Permalink » No comments

nodejs with cygwin

I keep having to redo this, so I’m copying down the trick from this article here, so I can find in the future.  To get NPM and NodeJS to work in cygwin on windows, you just have the alias them using these commands in your .bash_profile:

1
2
alias npm="/cygdrive/p/Programs/nodejs/npm.cmd"
alias node="/cygdrive/p/Programs/nodejs/node.exe"

Permalink » No comments

Email Monthly Spreadsheet Reports from WordPress

I’ve been working on a project where we have a very custom Customer Service form and we wanted to start sending out monthly excel reports with all the issues from the past month.

The form is a bit too custom to switch over to a plugin, so I opted to do the set-up by hand.  At the start the form was only sending out emails. So first I needed to create a custom post type to hold all the responses.  you can create one by adding the following code to your functions.php file.  WordPress has a breakdown of all the arguments in their documentation.

1
2
3
4
5
6
7
8
9
10
11
//Support Content Type
$args = array(
    'label'              => 'Support Request',
    'show_ui'             => true,
    'show_in_admin_bar'   => false,
    'public'              => false,
    'menu_position'       => 25,
    'menu_icon'           => 'dashicons-editor-help',
    'supports'            => array( 'title','editor','custom-fields')
);
register_post_type( 'support_request', $args );

Next, I needed to add in the following after sending the email off on the custom form page.  I’m setting the post title and post message to be identical to the email title and body that’s already been generated by the form.  I’m also adding in two custom fields “name” and “email.”  Those are just for example, you can add as you like and your form will likely dictate those.

1
2
3
4
5
6
7
8
9
//add to support request content type
$postarr = array(
    'post_title' => $title,
    'post_content'=> $message,
    'post_type' => 'support_request'
);
$post_id = wp_insert_post( $postarr );
update_post_meta($post_id,'Name',$_REQUEST['customer-name']);
update_post_meta($post_id,'Email',$_REQUEST['customer-email']);

After that, I created a page to generate the excel file.  I created a custom “generate_customer_report_excel.php” in my theme’s directory so it wouldn’t fatten up my functions.php file.  I’m going to create the filename ($output_filename) in the functions.php file to keep things DRY. If you want a frequency different then 30 days, you’ll need to change the “‘after’ => ‘- 30 days‘” line. You will also need to create a “support_requests” folder in your uploads folder. Here’s an example of what that page looks like:

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
//build your query 
$args = array(
    'post_type' => 'support_request',
    'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit'),
    'posts_per_page' => -1,
    'date_query'    => array(
        'column'  => 'post_date',
        'after'   => '- 30 days'
    )
);
 
query_posts($args);
 
// Prepare our csv download
$output_handle = fopen(WP_CONTENT_DIR . '/uploads/support_requests/'.$output_filename ,"w");
 
// Insert header row
fputcsv( $output_handle, ['Name','Email','Message']);
 
// The Loop
while ( have_posts() ) : the_post();
    $id = get_the_ID();
    $row = array(
        get_post_meta( $id, 'Name', false )[0],
        get_post_meta( $id, 'Email', false )[0],
        get_the_content()
    );
    fputcsv( $output_handle, $row);
endwhile;
if(!have_posts()){fputcsv( $output_handle, ['No Support Requests']);}
 
// Close output file stream
fclose( $output_handle );

Finally, I want to use WP Cron to set a monthly cron job to send out the email with the CSV attached.  To change the interval you’ll need to change the “‘interval’ => 2592000,”  and “$DownloadReportFrom = date(‘m_d_Y’,strtotime(“-1 month”));” to be the time span you want. Also, you’ll definaly need to adjust the to and from emails within the support_monthly_report function. This is the code to go in the functions.php file:

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
//Support Question reports
function my_cron_schedules($schedules){
    if(!isset($schedules["monthly"])){
        $schedules["monthly"] = array(
            'interval' => 2592000,
            'display' => __('Once a month'));
    }
    return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');
 
add_action('init','support_monthly_report_schedule');
add_action('support_monthly_report_job','support_monthly_report');
 
function support_monthly_report_schedule(){
    if (!wp_next_scheduled ( 'support_monthly_report_job' )) {
        wp_schedule_event(time(), 'monthly', 'support_monthly_report_job');
    }
}
 
function support_monthly_report(){
    $email = 'TOEMAIL';
    $subject = 'Monthly Report';
    $message = 'EMAILBODY';
 
    $DownloadReportFrom = date('m_d_Y',strtotime("-1 month"));
    $DownloadReportTo = date('m_d_Y',strtotime("now"));
    $output_filename = 'Support_report-' . $DownloadReportFrom .'-'. $DownloadReportTo  . '.csv';
 
    include('generate_customer_report_excel.php');
    usleep(300000);
    $attachments = array( WP_CONTENT_DIR . '/uploads/support_requests/'.$output_filename );
    $headers = 'From: FROMNAME ' . "\r\n";
 
    wp_mail($email,$subject, $message, $headers, $attachments );
}

And that should do it! You’ll be sending snazzy spreadsheets out in no time 😀

Permalink » No comments