Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i create a file name based on a variable?

Status
Not open for further replies.

networkwoes

Technical User
Aug 17, 2001
13
US
I am trying to create a file called CSCH.CSV on a daily bases and want to include the date in the file name so that it would look like CSCH 20011129.csv but have been unsuccesfull in doing so. I have the date stored as a variable and am able to print it fine. However when I try to create a file using the variable as part of the file name, the file that is created uses the variables name rather than its value. Here is the code i am useing:

($ss, $mm, $hh, $DD, $MM, $YYYY, $wday, $yday, $isdst) = localtime(time);

$Year = $YYYY + 1900;
$Month = $MM + 1;
$DateStamp = "$Year$Month$DD";
print "DateStamp\n";

open( OUTFILE, '>G:\Automation\Daily CSCH Report\csch$DateStamp.csv') or die "Can't open the csch(date) out file $!\n";


Also I am getting an warning line that like this:

Name "main::ss" used only once

for each of the variables that I invoked for the localtime, but did not need for anything... any ideas how to fix these things? all help would be greatly appreciated!!!



 
When you want to have a variable interpolated(or evaluated) in a string, you can't use single quotes to enclose the string - instead you have to use double quotes, since double quotes allow variables to be interpolated. So, in you open, change the single quotes around

'>G:\Automation\Daily CSCH Report\csch$DateStamp.csv'

to double quotes, and I think you'll be all set.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
It looks like you're using perl -w (very good!). The warning you're getting it telling you that you're assigning variables that you're not using later. This should fix it for you:

$DateStamp = sprintf ("%4.4d%2.2d%2.2d",
(localtime)[5] +1900,
(localtime)[4] + 1,
(localtime)[3]
);

Hope this helps.
 
ok, this will create a file with the name I want it too, but it is in the directory that the perl script is in:


($ss, $mm, $hh, $DD, $MM, $YYYY, $wday, $yday, $isdst) = localtime(time);

$Year = $YYYY + 1900;
$Month = $MM + 1;

open( OUTFILE, ">csch $Year $Month $DD.csv") or die "Can't open the csch(date) out file $!\n";

and this will create a file in the directory I want one created in but uses the variable name instead of its value (results in a file name of 'CSCH $Year $Month $DD.csv'):

($ss, $mm, $hh, $DD, $MM, $YYYY, $wday, $yday, $isdst) = localtime(time);

$Year = $YYYY + 1900;
$Month = $MM + 1;

open( OUTFILE, '>G:\Automation\Daily CSCH Report\csch $Year $Month $DD.csv') or die "Can't open the csch(date) out file $!\n";

and changeing the single quoates to double quoates like this:

($ss, $mm, $hh, $DD, $MM, $YYYY, $wday, $yday, $isdst) = localtime(time);

$Year = $YYYY + 1900;
$Month = $MM + 1;

open( OUTFILE, ">G:\Automation\Daily CSCH Report\csch $Year $Month $DD.csv") or die "Can't open the csch(date) out file $!\n";

comes back with "Cant open the csch(date) out file Invalid argument"

So I can either create the filename I want, or create the file where I want. Any ideas why I cant do both?? Does that fall under "Having my cake and eating it too"???
 
When you open the file, add the path to where you'd like to place the file before the filename. Normally, I do this by defining a directory variable in the beginning of the script and just reuse that over and over again whenever I'm reading or writing files. This also helps when you move the location of where you want to write the files as you're only changing your code in one place and the entire script is changed.

For instance:

$directory = "c:/outputfiles";

open (FILE, <&quot;$directory/$file.txt&quot;) or die &quot;Can't open $file: $!&quot;;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top