Advertisement

01.07.2004 at 09:20PM PST, ID: 20844403
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

move and rename files with current date appended to file name

Tags: perl, file, date, rename, move
I have the following perl code that copies and renames files with the current date appended to the file name.  I need to modify it to move  and rename the files with the date appended to the file name.  I took a stab at it thinking move.pm would work but not luck.  Can someone help me make this modification?

#!/usr/bin/perl -w

use strict ;
use POSIX ;
use File::Copy ;
use File::Basename ;

chdir "C:\\work\\bus_cov_data_in\\bus2111\\trans_cidX"
   or die "Can't chdir to C:\\work\\bus_cov_data_in\\bus2111\\trans_cidX $!\n" ;

for my $file (<*.xml>)
  { my ($name,$path,$suffix) = fileparse($file,"\.xml") ;
    my $datestamp = strftime(".%Y%m%d.%H%M%S",localtime) ;
    copy $file,"C:\\work\\bkups\\bkup_bus_cov_data_in\\bus2111\\trans_cidX\\$name$datestamp$suffix"
      or warn "Cannot copy $file $!\n" ;
  }

 
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: luke_airig
Solution Provided By: BioI
Participating Experts: 2
Solution Grade: A
Views: 236
Translate:
Loading Advertisement...
01.07.2004 at 09:30PM PST, ID: 10068565

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.08.2004 at 01:21AM PST, ID: 10069346

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.08.2004 at 08:12AM PST, ID: 10072473

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.08.2004 at 08:18AM PST, ID: 10072525

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.08.2004 at 08:24AM PST, ID: 10072577

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.08.2004 at 02:59PM PST, ID: 10076074

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
01.07.2004 at 09:30PM PST, ID: 10068565

Rank: Genius

What do you want to modify?
Do you want to use move instead of copy?
Assisted Solution
 
01.08.2004 at 01:21AM PST, ID: 10069346
The way I interprete your problem is: you want to make backups of a certain directory and you want add a datestamp into the filename.  Untill now, you used the current datestamp [the moment when you copy your file], but now you want to use the datestamp that is indicating when the file was modified for the last time.  Correct?

### option 1 ###
If you just want to remember the datestamp [i.e. *not* explicitily mention it in your filename] and you want to *move* and *not* copy the files, you can use "rename $file "C:\\......" because when you use rename, the datestamp of the original file is maintained [in contrast with copy, where the datestamp of the new file will correspond to the current time].
 
### option 2 ###
you want to copy the files to your backupdirectory AND you want to save the datestamp of the original file.  In that case you can use the "stat" -function in perl.  This stat-function gives you a lot of information concerning the file of interest.  You have to add "use File::stat".  This is what i added to your script:

my $info = stat($file);
my $datestamp = strftime(".%Y%m%d.%H%M%S", localtime($info->mtime));

you can print the datestamp of your file via:
print "date file last modified --> ", localtime($info->mtime), "\n";  
this will give you something like: Thu  Jan  8  09:21:28  2004

when you do
my $datestamp = strftime(".%Y%m%d.%H%M%S", localtime($info->mtime));
this will give you: 20040108.092128 [equivalent to the date mentioned above Thu  Jan  8  09:21:28  2004]

This time is the time when the file was last *MODIFIED* because you use "$info->mtime".  When you want to have the date the file was last accessed, you have to use "$info->atime".


--- this would be your script ---

#!/usr/bin/perl -w

use strict ;
use POSIX ;
use File::Copy ;
use File::Basename ;
use File::stat

chdir "C:\\work\\bus_cov_data_in\\bus2111\\trans_cidX"
   or die "Can't chdir to C:\\work\\bus_cov_data_in\\bus2111\\trans_cidX $!\n" ;

for my $file (<*.xml>)
  { my ($name,$path,$suffix) = fileparse($file,"\.xml") ;
   my $info = stat($file);
    my $datestamp = strftime(".%Y%m%d.%H%M%S", localtime($info->mtime));
    copy $file,"C:\\work\\bkups\\bkup_bus_cov_data_in\\bus2111\\trans_cidX\\$name$datestamp$suffix"
      or warn "Cannot copy $file $!\n" ;
  }

--- end of script ---

Good luck!
 
Accepted Solution
 
01.08.2004 at 08:12AM PST, ID: 10072473
ozo,

Sorry for confusing you guys.  Yeah, basically I just want to change from copy
to move.  Along with the moving each file, I want to append the current date.

Hopes this helps clarify.
 
01.08.2004 at 08:18AM PST, ID: 10072525
change the "copy" part in your script to the "rename"
rename $file,"C:\\work\\bkups\\bkup_bus_cov_data_in\\bus2111\\trans_cidX\\$name$datestamp$suffix"

and you still want to use the current time when you copy it, or do you want the datestamp the file was last modified?  I interpreted "rename the files with the date appended to the file name" that way...  
If you don't want the latter one, just change copy to rename in your original script...

 
01.08.2004 at 08:24AM PST, ID: 10072577

Rank: Genius

#does
move $file,"C:\\work\\bkups\\bkup_bus_cov_data_in\\bus2111\\trans_cidX\\$name$datestamp$suffix"
#not work for you?
 
01.08.2004 at 02:59PM PST, ID: 10076074
ozo,

I'm an idiot :).  I swear to god I tried that yesterday.  Today it works.

Thanks for your help.
 
 
20080236-EE-VQP-29