Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7985101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:28:24+00:00 2026-06-04T11:28:24+00:00

The program I’m writing is suppose to open two directories and read through the

  • 0

The program I’m writing is suppose to open two directories and read through the files in them to compare the contents of those files. Then the functions that have changed in the files should be printed to a file. This program will mainly be checking .cpp files and .h files.

Currently I am trying to go through the directory and open the current file I am at to print the functions that have changed. However, I keep getting an error that states that the file is not a file and can’t be opened.

Here is part of my current code that I am using

use strict;
use warnings;
use diagnostics -verbose;
use File::Compare;
use Text::Diff;

my $newDir = 'C:\Users\kkahla\Documents\Perl\TestFiles2';
my $oldDir = 'C:\Users\kkahla\Documents\Perl\TestFiles';

chomp $newDir;
$newDir =~ s#/$##;
chomp $oldDir;
$oldDir =~ s#/$##;

# Checks to make sure they are directories
unless(-d $newDir or -d $oldDir) {
    print STDERR "Invalid directory path for one of the directories";
    exit(0);
}

# Makes a directory for the outputs to go to unless one already exists
mkdir "Outputs", 0777 unless -d "Outputs";

# opens output file
open (OUTPUTFILE, ">Outputs\\diffDirectoriesOutput.txt");
print OUTPUTFILE "Output statistics for comparing two directories\n\n";

# opens both directories
opendir newDir, $newDir;
my @allNewFiles = grep { $_ ne '.' and $_ ne '..'} readdir newDir;
closedir newDir;

opendir oldDir, $oldDir;
my @allOldFiles = grep { $_ ne '.' and $_ ne '..'} readdir oldDir;
closedir oldDir

Here is where I want to open the files to read through them:

elsif((File::Compare::compare("$newDir/$_", "$oldDir/$_") == 1)) {
    print OUTPUTFILE "File: $_ has been update. Please check marked functions for differences\n\n";
    diff "$newDir/$_", "$oldDir/$_", { STYLE => "Table" , OUTPUT => \*OUTPUTFILE};
    #Here is where I want to open the file but when I try it throws an error
    #Here are the two opens I have tried:
    open (FILE, "<$newDir/$_") or die "Can't open file"; #first attempt
    open (FILE, "<$_") or die "Can't open file"; #second attempt to see if it worked
}

I tried adding the flags

my @allNewFiles = grep { $_ ne '.' and $_ ne '..' && -e $_} readdir newDir;
my @allNewFiles = grep { $_ ne '.' and $_ ne '..' && -f $_} readdir newDir;

But that would simply remove all files that weren’t .pl file extensions. I tested that on some simple directories I have that have two copies of .txt, .cpp, .h, .c, .py, and .pl file extensions and it would only show that the .pl files were files.

I am new to perl and any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:28:25+00:00Added an answer on June 4, 2026 at 11:28 am

    -f is returning undef with $! set to “No such file or directory” because you are passing a file name to -f instead of a path to the file.

    Change

    -f $_
    

    to

    -f "$newDir/$_"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program generates two strings and I want them compared by the external diff
This program so far has one purpose, take in two integers(the size of the
My program has two kernels and the second kernel should use the already uploaded
Program : List all C files in the current folder using execlp() system call:
This program takes 2 numbers from user input, asks them whether they'd like to
Program works fine (with random crashes) and Memory Validator reports Uninitialized read problem in
My program need to occupy some resources. I want it to release those resources
The program I'm working on crashes sometimes trying to read data at the address
My program receives an executable binary file through a TCP socket. I need to
My program needs to receive strings through the serialport. Problem is that the program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.