Sponsored Content
Full Discussion: Change Directory in Perl
Top Forums Shell Programming and Scripting Change Directory in Perl Post 302490884 by SandmanCL on Wednesday 26th of January 2011 03:44:47 AM
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{$_};
}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
DIRECTORY(3)						   BSD Library Functions Manual 					      DIRECTORY(3)

NAME
opendir, fdopendir, readdir, readdir_r, telldir, seekdir, rewinddir, closedir, dirfd -- directory operations LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <dirent.h> DIR * opendir(const char *filename); DIR * fdopendir(int fd); struct dirent * readdir(DIR *dirp); int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result); long telldir(DIR *dirp); void seekdir(DIR *dirp, long loc); void rewinddir(DIR *dirp); int closedir(DIR *dirp); int dirfd(DIR *dirp); DESCRIPTION
The opendir() function opens the directory named by filename, associates a directory stream with it and returns a pointer to be used to iden- tify the directory stream in subsequent operations. The pointer NULL is returned if filename cannot be accessed, or if it cannot malloc(3) enough memory to hold the whole thing, and sets the global variable errno to indicate the error. The fdopendir() function is equivalent to the opendir() function except that the directory is specified by a file descriptor fd rather than by a name. Upon successful return from fdopendir(), the file descriptor is under the control of the system, and if any attempt is made to close the file descriptor, or to modify the state of the associated description other than by means of closedir(), readdir(), readdir_r(), or rewinddir(), the behavior is undefined. Upon calling closedir() the file descriptor is closed. The FD_CLOEXEC flag is set on the file descriptor by a successful call to fdopendir(). The readdir() function returns a pointer to the next directory entry. It returns NULL upon reaching the end of the directory or on error. In the event of an error, errno may be set to any of the values documented for the getdirentries(2) system call. Note that the order of the directory entries vended by readdir() is not specified. Some filesystems may return entries in lexicographic sort order and others may not. The readdir_r() function provides the same functionality as readdir(), but the caller must provide a directory entry buffer to store the results in. If the read succeeds, result is pointed at the entry; upon reaching the end of the directory result is set to NULL. The readdir_r() function returns 0 on success or an error number to indicate failure. The telldir() function returns the current location associated with the named directory stream. Values returned by telldir() are good only for the lifetime of the DIR pointer, dirp, from which they are derived. If the directory is closed and then reopened, prior values returned by telldir() will no longer be valid. The seekdir() function sets the position of the next readdir() operation on the directory stream. The new position reverts to the one asso- ciated with the directory stream when the telldir() operation was performed. The rewinddir() function resets the position of the named directory stream to the beginning of the directory. The closedir() function closes the named directory stream and frees the structure associated with the dirp pointer, returning 0 on success. On failure, -1 is returned and the global variable errno is set to indicate the error. The dirfd() function returns the integer file descriptor associated with the named directory stream, see open(2). On failure, -1 is returned and the global variable errno is set to indicate the error. Sample code which searches a directory for entry ``name'' is: dirp = opendir("."); if (dirp == NULL) return (ERROR); len = strlen(name); while ((dp = readdir(dirp)) != NULL) { if (dp->d_namlen == len && strcmp(dp->d_name, name) == 0) { (void)closedir(dirp); return (FOUND); } } (void)closedir(dirp); return (NOT_FOUND); SEE ALSO
close(2), lseek(2), open(2), read(2), dir(5) HISTORY
The opendir(), readdir(), telldir(), seekdir(), rewinddir(), closedir(), and dirfd() functions appeared in 4.2BSD. The fdopendir() function appeared in FreeBSD 8.0. BSD
April 16, 2008 BSD
All times are GMT -4. The time now is 11:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy