473,404 Members | 2,174 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,404 software developers and data experts.

Copy and change file extension

I am trying to convert a unix shell script to perl, and I have having a
problem with the portion that copies files based on extension and renaming
them in the process. I only trying to copy files with a non-zero length.
Here is what I have so far.

$target is a command line parameter
$source is a command line parameter

opendir DH, "/$target";
foreach $file (readdir DH)
{ printf " the file name is %s\n", $file;
next unless $file =~ \/.abc$/ and !-z $name;
my $newfile = /$target//$file;
$newfile =~ s/\.abc$/.xyz/;
$filecount += 1;
}
Jul 19 '05 #1
4 21177
In article <ttf9e.2474$c24.1712@attbi_s72>, b.milbrandt
<b.*********@insightbb.com> wrote:
I am trying to convert a unix shell script to perl, and I have having a
problem with the portion that copies files based on extension and renaming
them in the process. I only trying to copy files with a non-zero length.
Here is what I have so far.

$target is a command line parameter
$source is a command line parameter

opendir DH, "/$target";
foreach $file (readdir DH)
{ printf " the file name is %s\n", $file;
next unless $file =~ \/.abc$/ and !-z $name;
my $newfile = /$target//$file;
$newfile =~ s/\.abc$/.xyz/;
$filecount += 1;
}


Is this the actual program you are trying to run? You are missing
characters and using the wrong characters in some places. You do not
say what this program is doing wrong. How can anybody help you?

Please post a complete, working, short-as-possible, cut-and-pasted
program that demonstrates the problem you are having, but do it in
comp.lang.perl.misc because this newsgroup is defunct.

Thanks.
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Jul 19 '05 #2
Here is the whole program

#!/bin/perl
# Check to be sure exactly 2 arguments passed to script
die "Must pass exactly 2 arguments to script" if @ARGV != 2;

# assign meaningfull names to variables
$source = @ARGV[0];
$target = @ARGV[1];

# Check to see if First argument is a directory
# if not exit
die "$source is not a directory \n" if !-d $source;
mkdir $target, 0755;

# if !-d $target;
#{ # Create Target Directory
# mkdir $target, 0755;
#}

#copy all files with a .abc extension to the target directory
# renaming the extension to xyz
# chdir $source;
# rename -f

$filecount = 0;

opendir DH, "//$source";
foreach $file (readdir DH)
{ printf " the file name is %s\n", $file;
next unless $file =~ \/.abc$/ and !-z $name;
my $newfile = /$target//$file;
$newfile =~ s/\.abc$/.xyz/;
$filecount += 1;
}

# Print the number of files copied
printf "The number of files copied is: %d\n", $filecount;

The program runs but I get 0 files copied and no files are copied or
renamed.

Brian

"Jim Gibson" <jg*****@mail.arc.nasa.gov> wrote in message
news:190420051608396664%jg*****@mail.arc.nasa.gov. ..
In article <ttf9e.2474$c24.1712@attbi_s72>, b.milbrandt
<b.*********@insightbb.com> wrote:
I am trying to convert a unix shell script to perl, and I have having a
problem with the portion that copies files based on extension and
renaming
them in the process. I only trying to copy files with a non-zero length.
Here is what I have so far.

$target is a command line parameter
$source is a command line parameter

opendir DH, "/$target";
foreach $file (readdir DH)
{ printf " the file name is %s\n", $file;
next unless $file =~ \/.abc$/ and !-z $name;
my $newfile = /$target//$file;
$newfile =~ s/\.abc$/.xyz/;
$filecount += 1;
}


Is this the actual program you are trying to run? You are missing
characters and using the wrong characters in some places. You do not
say what this program is doing wrong. How can anybody help you?

Please post a complete, working, short-as-possible, cut-and-pasted
program that demonstrates the problem you are having, but do it in
comp.lang.perl.misc because this newsgroup is defunct.

Thanks.
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet
News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000
Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---

Jul 19 '05 #3
In article <Alg9e.2409$WI3.2174@attbi_s71>, b.milbrandt
<b.*********@insightbb.com> wrote:

[top posting fixed]

"Jim Gibson" <jg*****@mail.arc.nasa.gov> wrote in message
news:190420051608396664%jg*****@mail.arc.nasa.gov. ..
In article <ttf9e.2474$c24.1712@attbi_s72>, b.milbrandt
<b.*********@insightbb.com> wrote:

[ original program with bugs snipped]
Is this the actual program you are trying to run? You are missing
characters and using the wrong characters in some places. You do not
say what this program is doing wrong. How can anybody help you?

Please post a complete, working, short-as-possible, cut-and-pasted
program that demonstrates the problem you are having, but do it in
comp.lang.perl.misc because this newsgroup is defunct.

Thanks.


You have ignored my advice to post your question to
comp.lang.perl.misc, I see. The only additional piece of advice I will
give you is to recommend you put the following:

use strict;
use warnings;

at the beginning of your program. You will then need to put 'my' in
front of your variable declarations. After doing that, you should be
able to find the source of your problems.

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 19 '05 #4
b.milbrandt wrote:
opendir DH, "/$target";
foreach $file (readdir DH)
{ printf " the file name is %s\n", $file;


That's wrong.

foreach my $entry (readdir DH) {
my $file = "/$target/$entry";
print " the file name is $file\n";
}

You've got another error where you use @ARGV[0] instead of $ARGV[0].
A better way would be to change
$source = @ARGV[0];
$target = @ARGV[1];
to
die "Usage: ..." unless @ARGV == 2;
my $source = shift;
my $target = shift;

Any other questions should be posted to comp.lang.perl.misc and
not this newsgroup (comp.lang.perl). But don't post anything
until after you've read and understood the "Posting Guidelines for
comp.lang.perl.misc".
-Joe
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Els | last post by:
If I use <? include "file.html"; ?> in the html of my document, do I _have_ to change the extension of that document to .php, or would it still work and be valid if I let it remain .html? --...
1
by: POnfri | last post by:
Hi, I have a problem in a peace of code were i'm doing a file copy using File.Copy. The Source is local and the target is a remote machine. Example: File.Copy(C:\temp\hi.txt,...
5
by: Winston | last post by:
Hi Group, in my aspx page there is an Response.Redirect to a file called myfile.csv. The file consists of plain text with comma separated values. I want that the browser opens the save as dialog...
1
by: Nicky | last post by:
when we change the extension of a file in windows, its icon also changes..... how does windows do tht. in my application i require a similar approach. i want the icon to be dependent on the...
3
by: smnoff | last post by:
I have a file, say test.c and I want to compile is and I happen to look and it appears to be compiled like a C++ file. So does it matter is the file extension where to written as test.cpp
4
by: foker500 | last post by:
I'm a newbie so please excuse my ingnorance. I have serveral hunderd files in a folder and need to change the file extension from '.pnt' to '.txt'. Any suggestions? Thanks
38
by: ted | last post by:
I have an old link that was widely distributed. I would now like to put a link on that old page that will go to a new page without displaying anything.
4
jamesd0142
by: jamesd0142 | last post by:
Ok here's one for the experts... I want to create a text file "test.text" that stores different values on each line. Is it possible for me to go to the directory its saved and change the...
2
by: ketenshi | last post by:
Hello, I would like to copy a file with vbscript to the assigned destination (pretty straight forward by using a filesystemobject and CopyFile). However, the catch is the new copy needs to have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.