LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Creating a .txt file in a specified directory.... (http://www.linuxquestions.org/questions/linux-newbie-8/creating-a-txt-file-in-a-specified-directory-877178/)

msr4 04-26-2011 09:22 AM

Creating a .txt file in a specified directory....
 
I am fresher and would like your help in creating a text file in a specified directory.

I have create a perl script, which create a text file, and it works well.
But i want this to create in a specified directory.

Can anyone please help me out with this.

THe scrip is:
Code:

#!/usr/bin/perl

use POSIX;

#Directory where the text file will be created.
$directory =  '/space/data';

if (!(-d "$directory")) { #check if directory exists -  returns true if doesn't exist.
        printf "No directory\n", $? >> 8;
        exit (1); #exit because directory doesn't exist.
}

my $filename=sprintf("%s_%s.TXT", "prefix", strftime("%y%M%d", localtime));
print "filename is $filename\n";
open(FILE, ">$filename") || die("Couldn't open file");

#print FILE "hello world\n";
print FILE "A,B,C,D,E\n";
close(FILE);
exit(0);


grail 04-26-2011 09:40 AM

So you have presented code and a task, but you have not said in what way it is failing?
Are you getting error messages?

msr4 04-26-2011 09:48 AM

It's not throwing any error, but also its not creating the .TXT file in the specified folder.

I want this script to create the .TXT file with data in it in the folder '/space/data';

Say for example,. I have a unix box and i have created this script, to create a txt file on the base machine (C:/).

How do i proceed with this.

Thanks.

TobiSGD 04-26-2011 10:06 AM

I am not an expert in perl, but as far as I can see you have specified the path in $directory and the filename in $filename. But I can't see where you concatenate these variables. You only specify the filename in your open statement, but not the directory.

grail 04-26-2011 10:09 AM

Quote:

but also its not creating the .TXT file in the specified folder.
Well this would stand to reason ... let me answer you with a question, how would you create the same file using 'touch' in the
directory you have listed?

msr4 04-26-2011 10:26 AM

So how should i create this file in that folder?

Can anyone pls share a sample script which creates a .TXT file in a folder (C:\).

It would be helpful to analyze and work on the existing script.

grail 04-26-2011 10:29 AM

Well being a linux questions site I am unsure how it works on windows, although I am guessing the same.
Why don't you try concatenating your directory with the string in your sprintf and see how you go?

msr4 04-26-2011 10:51 AM

Tried it. Does nothing. No error either.

mstone0802 04-26-2011 11:03 AM

This worked for me:

Code:

#!/usr/bin/perl

use POSIX;

#Directory where the text file will be created.
$directory =  '/home/mstone';

if (!(-d "$directory")) { #check if directory exists -  returns true if doesn't exist.
        printf "No directory\n", $? >> 8;
        exit (1); #exit because directory doesn't exist.
}

$filename="$directory/mstonetest.TXT";
print "filename is $filename\n";
open(FILE, ">$filename") || die("Couldn't open file");

#print FILE "hello world\n";
print FILE "A,B,C,D,E\n";
close(FILE);
exit(0);

It's on Linux, so you might need to make modifications for Windows.

TobiSGD 04-26-2011 11:48 AM

Quote:

Originally Posted by msr4 (Post 4336883)
Tried it. Does nothing. No error either.

Please show us what you have done.

Question, if you are on Windows, why is your $directory /space/data?

msr4 04-26-2011 01:08 PM

Windows was just an example. I am not working on windows. I am creating this perl script on AIX box.

I tried to concatenate the directory path and the file name, and i get this error message;

Operator or semicolon missing before %s_ at ./IVR_TEST.pl line 13.
Ambiguous use of % resolved as operator % at ./IVR_TEST.pl line 13.
Substitution pattern not terminated at ./IVR_TEST.pl line 13.


The script that i am using is;
Code:

#!/usr/bin/perl -w

use POSIX;

#Directory where the text file will be created.
$directory = '/space/data';

if (!(-d "$directory")) { #check if directory exists - returns true if doesn't exist.
printf "No directory\n", $? >> 8;
exit (1); #exit because directory doesn't exist.
}

my $filename = "$directory/sprintf("%s_%s.TXT", "TIN_WALK", strftime("%Y%m%d", localtime));
print "filename is $filename\n";
open (FILE, ">$filename") || die("Couldn't open file and directory");

print FILE "A,B,C,D,E\n";
close(FILE);
exit(0);

It's showing error here at line 13
Code:

my $filename = "$directory/sprintf("%s_%s.TXT", "DME_TIN_XWALK", strftime("%Y%m%d", localtime));

TobiSGD 04-26-2011 01:15 PM

You are doing it wrong. First generate the $filename, like you did before, then do:
Code:

$filename = $directory . $filename
Then continue like before.
Here more info about concatenating strings in Perl.

msr4 04-26-2011 01:35 PM

Ok.

Here's what i am looking for.

Does anyone has or know where i can get a simple perl script which creates a .TXT file in a directory.

I looked at the web and getting pages which shows how to create a txt file, but not in a directory.

Can anyone pass along if they have written a script to create a text file in a specific directory?

I am working on mine, and getting errors.

TobiSGD 04-26-2011 01:37 PM

I already have given you a solution. If that doesn't work, please give us the error messages.

michaelk 04-26-2011 01:49 PM

The OP dies not understand so lets elaborate just a bit. In a nutshell when you open a file for writing regardless of language as filename.txt it will create that file in the current working directory i.e. the directory where you ran your program. If you want to create that file in another location then filename.txt must include the path.

The basic syntax for creating a file for writing is:
open( FILEHANDLE, '>filename')

In your case you want to create a file in /space/data so:
open( FILEHANDLE, '>/space/data/filename')


All times are GMT -5. The time now is 09:39 AM.