unzip file (.gz) with date in name and copy to folder

it was suggested at another forum that i ask in the php section, here is my question:


I'm not a programmer by any means, but have a server with an ftp process that runs on a daily basis.
i have a folder named C:\..\Daily that saves files in the following format:

YYYYMMDD_filename.csv.gz

There is a file that gets created every day.

I would like to write a script (in whatever way is possible) that will extract this (and only this file) based on the date and then copy it to a share that exists on a different server  (\\servername\Export)  retaining the filename (with date).

I'm in need of this pretty quickly so if there is anything missing let me know.!
bwllcAsked:
Who is Participating?
 
aminerdCommented:
I apologize, I changed routes mid-post and forgot to compensate. Here it is corrected, and, as an added bonus, it should also handle directories in the tar file.

<?php
      
      // location of the tar file
      define('DIR_SOURCE', '/home/andrewm/temp/');
      
      // temporary location to unzip files to
      define('DIR_TEMP', '/tmp/');
      
      // destination directory for the files
      define('DIR_DEST', '/tmp/test/');
      
      //$file = DIR_SOURCE.date('Ymd').'_filename.cvs.gz';
      $file = DIR_SOURCE.'wordpress-2.0.2.tar.gz';
      
      if (($file = realpath($file)) !== FALSE)
      {
            
            $cmd = 'tar -xzvf '.escapeshellarg($file).' -C '.escapeshellarg(DIR_TEMP);
            exec($cmd, $out, $return);
            
            if (count($out))
            {
                  
                  foreach ($out as $file)
                  {
                        
                        $source = DIR_TEMP.$file;
                        $dest = DIR_DEST.$file;
                        
                        // is this a directory?
                        if (!is_dir($source))
                        {
                              // copy the file
                              copy($source, $dest);
                        }
                        else
                        {
                              // create it at the destination location
                              mkdir(DIR_DEST.$file);
                        }
                        
                  }
                  
            }
            else
            {
                  die('Couldn\'t untar -- invalid archive?');
            }
            
      }
      else
      {
            die('File doesn\'t exist.');
      }
      
?>
0
 
aminerdCommented:
Here's a try to get you started:

<?php
      
      // location of the tar file
      define('DIR_SOURCE', '/files/');
      
      // temporary location to unzip files to
      define('DIR_TEMP', '/tmp/');
      
      // destination directory for the files
      define('DIR_DEST', '/share/');
      
      $file = DIR_SOURCE.date('Ymd').'_filename.cvs.gz';
      
      if (($file = realpath($file)) !== FALSE)
      {
            
            $cmd = 'tar -xzvf '.escapeshellarg($file).' -C '.escapeshellarg(DIR_TEMP);
            exec($cmd, $out, $return);
            
            if (($return === 0) && preg_match_all('/^.*[^\/]$/m', $out, $files))
            {
                  
                  foreach ($files as $file)
                  {
                        
                        // actual names
                        $source = DIR_TEMP.$file;
                        $dest = DIR_DEST.$file;
                        
                        // move the file
                        rename($source, $dest);
                        
                  }
                  
            }
            else
            {
                  die('Couldn\'t untar -- invalid archive?');
            }
            
      }
      else
      {
            die('File doesn\'t exist.');
      }
      
?>
0
 
bwllcAuthor Commented:
Would i have to install PHP on the box that i am running this on? or can i run this remotely?
another thing... how does the script above reference the date of the file?  I need to pull in the most recent file which is also the name of the file.

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
aminerdCommented:
- You would have to install PHP to run this script.
- It bases the filename on the current date.
0
 
aminerdCommented:
If you're running BASH, here's an alternative method that doesn't rely on PHP. Again, it uses the current date for the filename:

#!/bin/bash

# destination directory
DEST='/tmp/test/'

# filename
DATE=`date +%Y%m%d`
TAR="$DATE-filename.csv.gz"

# untar it
FILES=`tar -xzvf $TAR`

for FILE in $FILES
do
        # is this a directory?
        if [[ -d $FILE ]]
        then
                mkdir "$DEST/$FILE"
        else
                cp "./$FILE" "$DEST/$FILE"
        fi
done

exit 0
0
 
dr_dedoCommented:
sorry for my question, but ur folder seems to be on drive C: ?? !!!

your server, is it windows or *inux ??
0
 
bwllcAuthor Commented:
it is a Windows Box.
I could arrange to have cygwin installed to run the bash script
or i could install php on the machine.( but i believe that will require a web server)
0
 
dr_dedoCommented:
well, i guess aminerd  code would work great, but on a linux box. you need to copy the file first to the linux box and extract it after
wait, i'll think of some code and post again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.