Rename "File" to "File$date"

Hy everybody in the List!

I have a Problem writing a little perl application...

Im a very newbie to programming and need your help :)


The Problem is:

I have a file "dev.txt" and I want to rename it once a day (with a =
cronjob) like "dev13092002.txt"

So i need to rename it with a date variable.

All I have at the moment is:

#!/usr/bin/perl
$d =3D `date`;
$d =3D /pattern1(pattern2)/;
sytem("cp test.txt test`$d`.txt");


Does anyone of you know how to do that?


THANKS in advance!

						=09
| Telematikzentrum D=FCrrwangen GmbH & Co. KG	|
| Im Schloss D=FCrrrwangen				|
| 								|
| Andreas Kauffmann					|
| Tel: 09856/979 48-0	| Fax: -19			|
| andreas.kauffmann@tzdan.de				|
|								|
PS: Besuchen Sie uns doch mal im Web!
=A0
> http://www.tzdan.de <
=A0
Jetzt im neuen Design!

0
andreas
9/13/2002 7:06:09 AM
perl.beginners 29378 articles. 3 followers. Follow

6 Replies
1002 Views

Similar Articles

[PageSpeed] 15

On Friday 13 September 2002 12:06 am, Kauffmann, Andreas wrote:
> Hy everybody in the List!
>
> I have a Problem writing a little perl application...
>
> Im a very newbie to programming and need your help :)
>
>
> The Problem is:
>
> I have a file "dev.txt" and I want to rename it once a day (with a cronjob)
> like "dev13092002.txt"
>
> So i need to rename it with a date variable.
>
> All I have at the moment is:
>
> #!/usr/bin/perl
> $d = `date`;
> $d = /pattern1(pattern2)/;
> sytem("cp test.txt test`$d`.txt");
>
>
> Does anyone of you know how to do that?

How about this? 

nyec

# BEGIN EXAMPLE
#STDERR = //Your syslog or log file//
# You may want/need to set the STDERR/STDOUT if
# this program is set as a cronjob depending on your system.
# Be sure to test logging and error output. 

#Date formatting
my ($day, $month, $year) = (localtime)[3,4,5];
$month++;
if ($month < 10) { 
  $month =~ s/^/0/;   # Keeps date format consistent(ddmmyyyy)
}
$year += 1900;
my $date = join($day,$month,$year);

my $oldfile = "dev.txt";

if (-e $oldfile) {
  my $datefile = $oldfile;
  $datefile =~ s/^dev/dev$date/;
  rename $oldfile, $datefile;   	#Note-> Overwrites any existing file
  if (!-e $datefile) {
     die "'$datefile' was not created: $!";
  }
}  else { 
   die "Can not open '$oldfile': $!";
}

__END__
0
nyec
9/13/2002 8:35:47 AM
Andreas Kauffmann wrote:
> 
> Hy everybody in the List!

Hello,

> I have a Problem writing a little perl application...
> Im a very newbie to programming and need your help :)
> 
> The Problem is:
> I have a file "dev.txt" and I want to rename it once a day (with a cronjob) like "dev13092002.txt"
> So i need to rename it with a date variable.
> All I have at the moment is:
> 
> #!/usr/bin/perl
> $d = `date`;
> $d = /pattern1(pattern2)/;
> sytem("cp test.txt test`$d`.txt");
> 
> Does anyone of you know how to do that?


For something simple like this you don't really need a Perl program

# run five minutes after midnight, every day
5 0 * * *       /bin/cp test.txt test`/bin/date +%d%m%Y`.txt



John
-- 
use Perl;
program
fulfillment
0
krahnj
9/13/2002 12:59:37 PM
Nyec wrote:
> 
> How about this?
> 
> # BEGIN EXAMPLE
> #STDERR = //Your syslog or log file//
> # You may want/need to set the STDERR/STDOUT if
> # this program is set as a cronjob depending on your system.
> # Be sure to test logging and error output.
> 
> #Date formatting
> my ($day, $month, $year) = (localtime)[3,4,5];
> $month++;
> if ($month < 10) {
>   $month =~ s/^/0/;   # Keeps date format consistent(ddmmyyyy)

Or just use sprintf.


> }
> $year += 1900;
> my $date = join($day,$month,$year);
                  ^^^^
The first argument to join() is the string to join the rest of the
arguments with.  So if $day is '05' and $month is '11' and $year is
'2002' the value of $date will be '11052002' but the OP wants
'05112002'.


> my $oldfile = "dev.txt";
> 
> if (-e $oldfile) {
>   my $datefile = $oldfile;
>   $datefile =~ s/^dev/dev$date/;
>   rename $oldfile, $datefile;           #Note-> Overwrites any existing file

Instead of using -e to determine if an error occured use the return
value of rename.


>   if (!-e $datefile) {
>      die "'$datefile' was not created: $!";
>   }
> }  else {
>    die "Can not open '$oldfile': $!";
          ^^^^^^^^^^^^             ^^
Neither -e nor rename actually opens the file and $! won't be set if -e
fails.

> }
> 
> __END__



John
-- 
use Perl;
program
fulfillment
0
krahnj
9/13/2002 1:13:12 PM
Andreas Kauffmann wrote:

> Hy everybody in the List!
> 
> I have a Problem writing a little perl application...
> 
> Im a very newbie to programming and need your help :)
> 
> 
> The Problem is:
> 
> I have a file "dev.txt" and I want to rename it once a day (with a
> cronjob) like "dev13092002.txt"
> 
> So i need to rename it with a date variable.
> 
> All I have at the moment is:
> 
> #!/usr/bin/perl
> $d = `date`;

after this line, the $d varaible will contain whatever your `data` command 
prints to STDOUT. in unix, this won't be MMDDYYYY. so you probably don't 
want to use that.

> $d = /pattern1(pattern2)/;

this line means search the $_ variable for 'pattern1' follow by 'pattern2' 
and stick the whatever the return value(true or false) into the variable 
'$d'. in fact, this will overwrite whatever value is already in '$d' from 
your `date` command. you don't want that too.

> sytem("cp test.txt test`$d`.txt");
>

this does a copy so it won't work again.
for a simply rename, you probably want to try something like:

#!/usr/bin/perl -w
use strict;

my($day,$month,$year) = (localtime)[3..5];

$day = sprintf("%02d",$day);
$month = sprintf("%02d",$month+1);
$year  += 1900;

rename('dev.txt',"dev$day$month$year.txt") || die $!;

__END__

david
0
dzhuo
9/13/2002 5:46:30 PM
--=_Boundary_19058LmaxCQNkjROMTgJ
Content-Type: text/plain; charset="us-ascii"

Silly as it sounds, I find doing the %Y first as it makes finding the 
document  / file / log you're looking for far easier in the log run. 
Year/Month/Date :)

Just my 2c.
Pieter




"John W. Krahn" <krahnj@acm.org>
2002/09/13 02:59 PM

 
        To:     beginners@perl.org
        cc: 
        Subject:        Re: Rename "File" to "File$date"


Andreas Kauffmann wrote:
>
> Hy everybody in the List!

Hello,

> I have a Problem writing a little perl application...
> Im a very newbie to programming and need your help :)
>
> The Problem is:
> I have a file "dev.txt" and I want to rename it once a day (with a 
cronjob) like "dev13092002.txt"
> So i need to rename it with a date variable.
> All I have at the moment is:
>
> #!/usr/bin/perl
> $d = `date`;
> $d = /pattern1(pattern2)/;
> sytem("cp test.txt test`$d`.txt");
>
> Does anyone of you know how to do that?


For something simple like this you don't really need a Perl program

# run five minutes after midnight, every day
5 0 * * *       /bin/cp test.txt test`/bin/date +%d%m%Y`.txt



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org



--=_Boundary_19058LmaxCQNkjROMTgJ--
0
pblaauw
9/13/2002 5:47:21 PM
On Fri, 13 Sep 2002, Kauffmann, Andreas wrote:

> Hy everybody in the List!
> 
> I have a Problem writing a little perl application...
> 
> Im a very newbie to programming and need your help :)
> 
> 
> The Problem is:
> 
> I have a file "dev.txt" and I want to rename it once a day (with a cronjob) like "dev13092002.txt"
> 
> So i need to rename it with a date variable.
> 
> All I have at the moment is:
> 
> #!/usr/bin/perl

Enable warnings and use strict, will save you a lot of
debugging time.

> $d = `date`;

Read through perldoc -f localtime

my ($day, $month, $year) = (localtime)[3,4,5];
$month++;
$year += 1900;


> $d = /pattern1(pattern2)/;
> sytem("cp test.txt test`$d`.txt");

Why are you copying when you actually want to rename the file.
perldoc -f rename

> 
> 
> Does anyone of you know how to do that?
> 
> 
> THANKS in advance!

0
sraghav
9/14/2002 10:59:28 AM
Reply:

Similar Artilces:

"File" to "File$date"
Okay... Thanks for all your answers! From all, I combinded the perfext solution: rename("test.txt", "test" . &heute() . ".txt"); sub heute { return (localtime(time))[3] . (localtime(time))[4] . = ((localtime(time))[5] 1900); } Thanks ;) Andreas -----Urspr=FCngliche Nachricht----- Von: Sudarshan Raghavan [mailto:sraghav@india.hp.com]=20 Gesendet: Samstag, 14. September 2002 15:21 An: Perl beginners Betreff: Re: AW: Rename "File" to "File$date" On Sat, 14 Sep 2002, Sudarshan Raghavan wrote: >=20 > #!/...

Convert "xls" file to "txt" file
Hi, I hope someone can help. I need to convert an Excel spreadsheet to an ASCI text file, in order to import data into a datawindow. I want to try and avoid asking users to save their files as text files and there are issues surrounding the use of ODBC to link to Excel. I am currently able to view my Excel data by way of an OLE object but if I try "Getdata", only the first 72 rows are imported to a string or blob and then subsequently to the datawindow. So I am looking for an OLE function which will "save as" my file. Help would be greatly appeciated. ...

.ALLCOL("%COLUMN%", " ", ", ", ", ")
Do you know anyway for me to exclude a subset of columns returned by this function. We have two columns (rec_user and rec_datetime) which are in all of our tables, but when generating triggers I want automatically generate a script which does not include those two columns but does include all other columns in that table. Bruce I should add that I am using PD 9.0.0.580. Bruce "Bruce Lamb" <lamb.bruce@mayo.edu> wrote in message news:6HgI315nCHA.155@forums.sybase.com... > Do you know anyway for me to exclude a subset of columns returned by this > function. ...

Precedence of "where" ("of", "is", "will")?
Nobody on #perl6 today could answer this one. Is: Str | Int where { $_ } the same as: (Str | Int) where { $_ } or: Str | (Int where { $_ }) ? Followup questions, Mr. President: What kind of operators are "where", "of", "is", and "will"? Is there a reason that S03 doesn't list them? What are their precedence(s)? -- Chip Salzenberg - a.k.a. - <chip@pobox.com> Open Source is not an excuse to write fun code then leave the actual work to others. Chip Salzenberg writes: &...

How to rename the "File", "Edit", (etc) menus?
Hi, I'm trying to rename the "File", "Edit" and other top level Firefox menus to have shorter names so as to save screen space. At present there does not seem to be an extension that will allow me to do this with Firefox 2 (and only an experimental extension for Firefox 3): https://addons.mozilla.org/en-US/firefox/addon/9480 I have installed the userChrome.js extension to run custom snippets of JavaScript: http://mozilla.zeniko.ch/userchrome.js.html I'm hoping I can rename the menus with a just few lines of JavaScript, but I do not know what JavaS...

Visual Studio 2005. "Quick Find", "Quick Replace", "Find in Files", "Replace in Files" functionality no longer works
Hello, I just run into problem that find functionality is no longer working in Visual Studio 2005. "Quick Find", "Quick Replace", "Find in Files", "Replace in Files" functionality no longer responds. These are necessary functions when build any website. Any solutions on how to fix this problem?Improving lifestyles for luxury pets: TheFabulousPet In Menu bar click Window -> Reset Window Layout. It solves the problem.Improving lifestyles for luxury pets: TheFabulousPet...

Shortcut Key for "File" -> "Save As File"
Hi all, We can save as DataWindow Object into .XML file through "File-> Save As File" option (after opening DataWindow Object in PB). Here I am unable to identify shortcut key for "Save As File", can any one tell me the shortcut key is available or not? if yes, could you please tell me the shortcut key for "Save As File" menu item. Advanced thanks for your help. -Siva On Feb 4, 7:45=A0pm, Siva Prasad <sivamc...@gmail.com> wrote: > Hi all, > > We can save as DataWindow Object into .XML file through "File-> Save > As...

quotes, quotes, quotes...
I am getting this error and I know what is causing it, but I have no idea how to fix it, any help would be great. The script steps through the /var/log/messages file on a linux server and puts The entries into a mysql database. However when it gets to the 'hlt' line in the messages file it just barfs. The single quotes are freaking it out. I know about quotes but not how to use in this situation. Thanks, Paul Error: May 27 17:53:00 localhost kernel: Checking 'hlt' instruction... OK. <----- doesn't like this in the messages file DBD::mysql::st exec...

How do I get the "created on" and "modified on" dates from an uploaded file?
I have a web application that allows users to upload and download various kinds of files.  This is accomplished using the usual methods:   The FileUpload control for uploading...   Response.BinaryWrite for the downloads...On upload, I would like to be able read the file's "created on" and "modified on" dates, and then store them in a database along with the file. Likewise, when a user downloads a file - I would like to output the file with it's associated "created" and "modified" dates.I cannot find any information on how to do this. When you execute a "PostedFile.Sa...

"404
Hello I moved my application from IIS6 to IIS7 on windows server 2008.   And now, when a user try to download, from my web application , a file with a filename that contains the "+" sign, I get the "404 - File or directory not found." error message.For example "20-requirement+result.doc" would generate an error.It uses to work fine on windows server 2003 with IIS6.thanks in advance for your help Look in the IIS config file Its located in the System32 Directory - ex.   C:\Windows\system32\inetsrv\config\applicationHost.config L...

File write error on file "acct" in module "GWEACCT"
Getting this on GWIA 6.02 scrolling across the screen. Runs on a Win2ksp3 box. Searching TID's...... JG I'd probably whack most of the subdirs under gwia and let gwia rebuild. -- Michael J. Bell Novell Support Connection Volunteer Sysop Author of Guinevere (http://www.openhandhome.com) PLEASE: Do not e-mail me privately unless specifically asked. I'm a volunteer, not a Novell employee! All opinions and advice provided are MINE alone and NOT Novell's unless specifically identified as such. Novell does not officially monitor these forums. Enhance...

File write error on file "acct" in module "GWEACCT".
--____HHJZYVOOFXDLYDIWHUSF____ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: base64 Content-Disposition: inline; modification-date="Tue, 24 Sep 2007 10:15:42 -0400" VGhlIG1vcm5pbmcgb2YgV2VkLiBTZXB0IDE5dGguIHdlIHN3aXRjaGVkIG91ciBHV0lBIG92ZXIg dG8gdmVyc2lvbiBHVzYuNSBTUDYgZnJvbSB2ZXJzaW9uIEdXNS41LiAgVGhlIG5leHQgbW9ybmlu ZyB3ZSBzdGFydGVkIGdldHRpbmcgdGhlIGZvbGxvd2luZyBlcnJvciBpbiBvdXIgR1dJQSBsb2cg ZmlsZTsNCg0KMDktMjAtMDcgMDg6MjI6MTAgMCBVbmtub3duLCBlcnJvciBjb2RlID0gODIxMA0K MDktMjAtMDcgMDg6MjI6MjEgMCBNU0cgNDAwIERlZmVycmVkIGRlbGl2ZXJ5IGZpbGUgbWVtb3J5 IGVy...

Is there a way to write Atlas "text/xml-script" script Into single file like ".js" or ".css" file
I have tried the following code: <script type="text/xml-script" src="Default.aspx.ajax"><script type="text/xml-script" src="Default.aspx.js"><script type="text/xml-script" src="Default.xml"> but they dit dot work. thanks.msn & email:lidong6@gmail.com I believe it does not work as the XML-Script processor takes the content within the script tags to do processing and ignores the src tag. I think this will be a nice feature to add in the next release....

renaming "grep" to "where"
Putting aside legacy issues for the moment, I suggest that it might be appropriate to rename the .grep list operator to .where, so we can say, for example: @filtered = @originals.where:{ .foo eq $bar }; We already have a "where" keyword in the language, which is used for very similar things, and I think it would be reasonable that users would see it used in other situations and think they can use it for filtering a list too. Moreover, to people who don't come from the Unix command line, or in general, the term ".where" is more descriptive to wha...

Web resources about - Rename "File" to "File$date" - perl.beginners

Resources last updated: 12/14/2015 6:33:40 PM