473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Writing to a file in a different directory?

I have a simple script that opens a directory, reads through all the file names and then prints the output to a text file in a different directory. It looks something like this.
My Question is how to write to the file using $OutputDir for my path instead of including the entire path in the manner that I'm using now.
Any suggestions on how to do this or how to improve the script are greatly appreciated.
Thanks

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  use strict;
  3.  use warnings;
  4.  
  5. my $InputDir = '/home/files';
  6. my $OutputDir = '/home/output';
  7. opendir (DIR, $InputDir) or die "Couldn't open: $!;
  8.        my $listing = `ls -l $InputDir`;
  9.  
  10. open (MYFILE, '>/home/output/Output.txt') or die "Couldn't open: $!";
  11.  print MYFILE "$listing\n";
  12.  close (MYFILE);
  13. closedir(DIR);
  14.  
Sep 10 '12 #1

✓ answered by RonB

Change:
Expand|Select|Wrap|Line Numbers
  1. open (MYFILE, '>/home/output/Output.txt')
To:
Expand|Select|Wrap|Line Numbers
  1. open (MYFILE, '>', "$OutputDir/Output.txt")

1 6714
RonB
589 Expert Mod 512MB
Change:
Expand|Select|Wrap|Line Numbers
  1. open (MYFILE, '>/home/output/Output.txt')
To:
Expand|Select|Wrap|Line Numbers
  1. open (MYFILE, '>', "$OutputDir/Output.txt")
Sep 11 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: o'seally | last post by:
solaris/linux admins/rookie_developers that battle with this error are probably all frustrated when it happens. i bet you're also somehow frustrated by this seemingly unsolvable error :-) ...take...
6
by: haynesc | last post by:
Hi, I'm having a problem where when trying to open a file in write mode, I get an IOError stating no such file or directory. I'm calling an external program which takes an input file and...
2
by: Abe | last post by:
I have a strange Perl problem I don't understand. I've written the following program to scan different disks on a Windows server to look for directory files. Works fine until it gets to 'e:' when...
4
by: Andy Gilman | last post by:
I want a method basically that tells me if a path is a file or directory, or any other possibilities. What do i need to do? I'm hoping I dont have to manually determine all these things with...
1
by: Lingo | last post by:
Hi all, I have a problem, i am running a batch file which will connect to oracle database via sqlplus. If i set whenever Oserror Exit and then try to connect it fails with the error message...
2
by: blisspikle | last post by:
Is there any way to use the "imports" keyword for your current solution to use code (classes) that were used in a different solution in a different directory without compiling them to a dll? I...
3
by: Okonita | last post by:
Hi all, I am having problem completing this restore operation. "db2 restore database AAMI01 from /pap/data/backups taken at 20071002130554 to /pap/data/db01 into AAMI01 NEWLOGPATH /pap/data/new/...
0
by: DeskUser | last post by:
Hello I have an asp.net application installed in several locations (framework 1.1). One of my clients is having a strange error when trying to access a certain aspx page:
1
by: sandhyabhavani | last post by:
This article is used to zip a file or directory using vb.net. The classes and method to zip a file is availale in java.io, java.util, java.util.zip class library.To import these you have to add a...
0
by: nagashree | last post by:
Hi, i want to compress the file or directory i am able to do that can anybody help me to decompress the file when i double click on the zipped file i am getting the error message as...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.