LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-26-2011, 09:22 AM   #1
msr4
LQ Newbie
 
Registered: Apr 2011
Posts: 8

Rep: Reputation: 0
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);
 
Old 04-26-2011, 09:40 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So you have presented code and a task, but you have not said in what way it is failing?
Are you getting error messages?
 
Old 04-26-2011, 09:48 AM   #3
msr4
LQ Newbie
 
Registered: Apr 2011
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 04-26-2011, 10:06 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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.
 
Old 04-26-2011, 10:09 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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?
 
Old 04-26-2011, 10:26 AM   #6
msr4
LQ Newbie
 
Registered: Apr 2011
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 04-26-2011, 10:29 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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?
 
Old 04-26-2011, 10:51 AM   #8
msr4
LQ Newbie
 
Registered: Apr 2011
Posts: 8

Original Poster
Rep: Reputation: 0
Unhappy

Tried it. Does nothing. No error either.
 
Old 04-26-2011, 11:03 AM   #9
mstone0802
LQ Newbie
 
Registered: Apr 2011
Location: Phoenix, AZ
Distribution: Ubuntu, Mint, CentOS
Posts: 7

Rep: Reputation: 0
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.
 
Old 04-26-2011, 11:48 AM   #10
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by msr4 View Post
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?
 
Old 04-26-2011, 01:08 PM   #11
msr4
LQ Newbie
 
Registered: Apr 2011
Posts: 8

Original Poster
Rep: Reputation: 0
Unhappy

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));
 
Old 04-26-2011, 01:15 PM   #12
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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.
 
Old 04-26-2011, 01:35 PM   #13
msr4
LQ Newbie
 
Registered: Apr 2011
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 04-26-2011, 01:37 PM   #14
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
I already have given you a solution. If that doesn't work, please give us the error messages.
 
Old 04-26-2011, 01:49 PM   #15
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897
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')
 
1 members found this post helpful.
  


Reply

Tags
perl



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How upload a txt file to a domain's base directory? JuJamEI Linux - Newbie 1 11-16-2010 12:17 PM
How do you add a .txt file to a domains base directory Trebore General 2 10-14-2009 02:47 PM
Creating a cron job to email me a txt file/format? NewShockerGuy Linux - General 7 05-16-2008 09:23 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration