bobdole has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I am trying to run a perl script on my local computer that reads a list of files and matches them with a folder on our server and moves them to a diffrent folder on our server.
The script it self works fine when i run it on my local computer to access my local computer files but when I try to run it on my local to access the server folders it gives me an access denied error in the command prompt. I have administrator rights on the server and I am logged into the server as admin from my client computer at the time I run my script but I still get the message. Is there something I can put in the perl script i.e a username and password that will stop giving me this error. And I know I could just run the script on the server but I really dont want to install perl on the server itself.
My client is a win xp pro and the server is win server 2003

Thanks

Edited by Arunbear: Changed title from 'Access denied', as per Monastery guidelines

  • Comment on Server access denied when checking for files

Replies are listed 'Best First'.
Re: Server access denied when checking for files
by jhourcle (Prior) on Apr 25, 2005 at 23:36 UTC

    'Access denied' could mean any of a number of things:

    • You can not run the perl executable
    • You cannot read (or execute, depending on how it was called) the Perl script.
    • You cannot read from the directory in which you're trying to get the file listing
    • You cannot write to the directory that you are trying to move the files to.
    • The script is not mapped to the correct executable

    Each of these errors may result in different things needing to be done to fix them. It would be useful for you post the exact error message, and the script that you're trying to use (as the error messages may be generated from the script and not the OS, and/or refer to line numbers within the script)

      I know the paths to the folders are correct becuase if I take out the part where it actually moves the folders it works fine.

      Here is the command line:
      C:\Documents and Settings\Tim\My Documents\test1>perl readdir.plx
      Access is denied.

      Here is the code
      #!/usr/bin/perl $dirtoget="\\\\Server\\Bankpro"; #directory to +read folders opendir(IMD, $dirtoget) || die("Cannot open directory"); #o +pen directory @thefiles= readdir(IMD); #store all files a +nd folders in directory into an array closedir(IMD); #close directory open (filenames, "dissmissed2.txt") || die ("Could not open file. <br> + $!"); #open list of file names $text = <filenames>; #read in one line +from txt file $defaultdir = "\\\\Server\\Bankpro"; # Directo +ry containing original folders $destinationdir = "\\\\Server\\BPdismis"; # Direct +ory to copy folders to foreach $f (@thefiles) #for each file i +n the directory { unless ( ($f eq ".") || ($f eq "..") ) #dont move + unless its a file of folder { while ($text){ #while there is a li +ne of text $text =~ s/\s+$//; #strip off whitesp +ace, newline and tabs system qq(move /y "$defaultdir/$text" "$destinationdir"); +#move files to diffrent directory $text = <filenames>; #read in next +line from file } } } close (filenames); #close list of files

        Then from the sounds of it, you don't have permission to write to the directories in which you're trying to move the files. There's also a slight chance that you don't have permission to modify the directory that you're attempting to move the files from (as you're effectively removing an item from that directory)

        Can you move files from the command line, without using perl? If so, is the Perl script running under any different permissions than your normal permissions?

        I was trying to understand what you are attempting to do, but don't at all. Is this being run as a CGI; if not, why does the the error message for the first open have <br>? (It won't print to the browser because there is no content type line.) What does the comment "#dont move unless its a file of folder" mean? What do the files listed in dissmissed2.txt have to do with the entries in @thefiles? I don't understand what your last foreach loop is doing; it looks like for each $f in @thefiles (except . and ..), you are trying to do the same operations on the files listed in dismissed2.txt (but it looks like it will only be successful the first time.); isn't this latter file always the same independent of the value of $f?
        If I'm just being dense, please excuse...it's getting to be past my bedtime; but I've been looking at this for a while, and I can't make any sense of it.
        chas
        You don't check every operation for success. You should check that readdir worked:
        @thefiles= readdir(IMD) or die "can't readdir: $!";
        And the most important part : check that you can actually "move" the files:
        system qq(move /y "$defaultdir/$text" "$destinationdir") or die "can't + move the file: $!";
        to understand where exactly the script fails.
Re: Server access denied when checking for files
by bobdole (Beadle) on Apr 26, 2005 at 04:45 UTC
    Well I am pretty new with perl so I copy and paste alot of code from other snippets of code from other scripts, the <br> doesnt need to be there it would just print out <br> if it couldnt open the directory, I just over looked it.

    As far as the foreach I am not completly sure the neccessity of the 'unless' but it seems to work fine when its ran on my client machine. As I understand it @thefiles contains the file and folder names from the bankpro directory. It then enters the while loop which takes a line of text from the dismissed2.txt, which contains the names of the folders and files we want to move to the new directory, and appends it to the defaultdir in the system qq(move...) statement and if the file or folder exsists then it will move it to the destination directory. Then it sets $text to the next line of input in the dismissed2.txt file and keeps looping until all the lines in the file have been read.

    Once all the lines of text have been read in the dismissed2.txt then it takes another element from @thefiles and goes through the process again.

    Again I am definatly a novice at this and this is what it appears to be doing to me. heh it does work though, just not on the server where I need it to.
Re: Server access denied when checking for files
by bobdole (Beadle) on Apr 26, 2005 at 14:58 UTC
    I changed the code to check for success on those lines

    #!/usr/bin/perl $dirtoget="\\\\Server\\Bankpro"; #directory to +read folders opendir(IMD, $dirtoget) || die("Cannot open directory"); #o +pen directory @thefiles= readdir(IMD) or die "can't readdir: $!"; #st +ore all files and folders in directory into an array closedir(IMD); #close directory open (filenames, "dissmissed2.txt") || die ("Could not open file. $!") +; #open list of file names $text = <filenames>; #read in one line +from txt file $defaultdir = "\\\\Server\\Bankpro"; # Directo +ry containing original folders $destinationdir = "\\\\Server\\BPdismis"; # Direct +ory to copy folders to foreach $f (@thefiles) #for each file i +n the directory { unless ( ($f eq ".") || ($f eq "..") ) { while ($text){ #whi +le there is a line of text $text =~ s/\s+$//; #s +trip off whitespace, newline and tabs system qq(move /y "$defaultdir/$text" "$destinationdir") or di +e "can't move the file: $!"; #move files to diffrent directory $text = <filenames>; + #read in next line from file } } } close (filenames); #close list of files


    And I still get the same response in the command prompt

    C:\Documents and Settings\Tim\My Documents\test1>perl readdir.plx
    Access is denied.

    I just tried to move a test folder from the command prompt and it gave me the same access denied

    C:\Documents and Settings\Tim\My Documents\test1>MOVE \\Server\Bankpro\test "\\S erver\BPdismis"
    Access is denied.

    So I guess it has something to do with permissions, maybe its more of a windows question then perl then. I just dont understand if I can view and move the folders on the server from my client why I cant do it from the command line.

    Thanks
      On my machine, a command like "C:\Documents and Settings\Tim\My Documents\test1>perl readdir.plx" doesn't work (produces a "Bad Command" error.)
      Try "perl readdir.plx < C:\Documents and Settings\Tim\My Documents\test1" (or maybe a pipe instead of the ">").
      That may be unrelated to your problem, though. As I mentioned in a previous reply, I think your last foreach loop is logically garbled. Why are you operating on the files in dismissed2.txt for each $f in @thefiles? And won't that just work for the first $f? (You will have reached end of file in dismissed2.txt after that.) (That may also be unrelated to the specific error you mention, though...)
      chas