Change Directory in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change Directory in Perl
# 1  
Old 01-25-2011
Change Directory in Perl

Hi

Can any one please support:

From Windows, I am running perl script located in C:/scripts directory and need to run a command "ls | sort | uniq" on the files in D:/temp directory.

Code:
 
#!c:\perl\bin\perl.exe

$file_dir="D:\\temp";
$command1="cd";
$command2="ls | sort | uniq";
$cmd=`"$command1" "$file_dir"`;
$uniq_file=`"$command2" "$cmd"`;
print "$last_file";

can any one please correct I get the following error:
'"cd"' is not recognized as an internal or external command
and secondly the script perform the the task on the present location and does not go to D:/temp.

thanks in advance.

Last edited by pludi; 01-26-2011 at 08:44 AM..
# 2  
Old 01-26-2011
I don't know DOS well at all but I doubt "uniq" is there either. But then again, why would you need that ? Each filename has to be unique. Try to avoid system() and backtick calls to have your script portable. This might work:

Code:
#!/usr/local/bin/perl

use strict;

opendir (DIR, "D:\\TEMP\");

while (my $file = readdir(DIR)) {
   print $file;
}

If for some reason I'm missing something and you indeed have multiple files with the same name, maintain a list of unique ones by using a hash with the filename as a key:

Code:
my %filehash;

while (my $file = readdir(DIR)) {
  ++$filehash{$file};
}

foreach (keys %filehash) {
  print $filehash{$_};
}

# 3  
Old 01-26-2011
'cd' isn't a command, but a built-in of cmd.exe. If you want to code Perl, use chdir instead.

As for the rest: Sandman already told the important stuff.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change Directory

Hi All, There is a code like below in my script ############################################### ###Create Directories and Sub-Directories ############################################### dpdir=DP_FROM_${from}_TO_${to} mkdir $dpdir cd $dpdir mkdir AWQM WFCONTROLLER PROVCO PRISM ... (1 Reply)
Discussion started by: pvmanikandan
1 Replies

2. UNIX for Dummies Questions & Answers

Change permission to a directory

Hi, How do i change the permission to read/write to a windows directory? (1 Reply)
Discussion started by: lg123
1 Replies

3. Shell Programming and Scripting

Change to directory and search some file in that directory in single command

I am trying to do the following task : export ENV=aaa export ENV_PATH=$(cd /apps | ls | grep $ENV) However, it's not working. What's the way to change to directory and search some file in that directory in single command Please help. (2 Replies)
Discussion started by: saurau
2 Replies

4. UNIX for Dummies Questions & Answers

How to change database directory to another directory?

Hi, I Installed mysql on my CentOS 6.2 Server. But when I tried to change the location of /var/lib/mysql to another directory. I can't start the mysql. Below is what I've done yum install mysql mysql-server mysql-devel mkdir /path/to/new/ cp -R /var/lib/mysql /path/to/new chown -R... (1 Reply)
Discussion started by: ganitolngyundre
1 Replies

5. Shell Programming and Scripting

change directory if available

I have a simple shell script that prompts the user to enter a directory to navigate to. What i want it to do and i don't know how to do this is if the directory is invalid automatically navigate to the home directory. echo "enter a directory to navigate to:" read directory cd $directory... (6 Replies)
Discussion started by: icelated
6 Replies

6. Shell Programming and Scripting

How to change a directory on windows via perl script

How to change a directory on windows via perl script. I wanna mount a share on windows via perl script. I have used:- system("dir"); gives the list of directories in the drive i am.This command works well. Now i want to move from c drive to z drive via perl script.I am using:- ... (2 Replies)
Discussion started by: TRUPTI
2 Replies

7. UNIX for Dummies Questions & Answers

Change Directory

I have a directory that is existing under my root dir of the FTP server. The DIR name is 'Software Patch'. I want to move in to that DIR to download some patches. But, when I issued a command 'cd SOftware Patch', the system said that it cannot find the dir 'Software'. I tried all possible ways like... (2 Replies)
Discussion started by: vskr72
2 Replies

8. Shell Programming and Scripting

Change of directory thru script

:confused: Hi All, This script is not working. I want to change the directory as per users selection in current shell. Looks like it is spawning sub-shell internally. I have used . changedir.sh source changedir.sh ./changedire.sh But did not work. Currently shell directory remain the... (4 Replies)
Discussion started by: shiningram
4 Replies

9. Shell Programming and Scripting

change directory

hi, Iam in directory A. I run a script from there. inside the script i have a command cd B. When i come out of the script directory is A only. Even when i come out scrip i want the directory to be B How to achieve (2 Replies)
Discussion started by: mkan
2 Replies

10. Shell Programming and Scripting

change directory

Hi all, I'm trying to wirte a small shell script in Linux. My script has the flow like, cmd1 cmd2 cd testdata cmd3 After exiting the program, the CWD remains the same as where I execute the program. I need it to be changed to the latest updated directory in the program. How can I do... (1 Reply)
Discussion started by: vadivel
1 Replies
Login or Register to Ask a Question