
Patrick B. answered 10/12/20
Math and computer tutor/teacher
You did not specify it had to be in java....
You STILL aren't comparing the pathnames!!!
It does not matter how the files were created. EVENTUALLY
you have to Compare the STRINGS that contain the pathnames
to the files.
The whole problem boils down to comparing these two strings..
Granted, if you're on another Unix/Linux system, the slashes
will face the other way, which is a MINOR change..
the LOGIC is still the same, AND I left you comments
Here's your code
//*****************************************************************//
class SameFolder
{
public int SameFolder( String pathname1, String pathname2)
{
int iReturn=-1;
int N1=pathname1.length();
int N2=pathname2.length();
if (N1==N2) //must be the same length
{
//points at end of the strings
int iCur1 = N1-1;
int iCur2 = N2-1;
//walks backwards until SLASH
char chChar1='?';
//change the slash direction to '/' if necessary
while (chChar1 != '\\') { chChar1 = pathname1.charAt(iCur1--); }
char chChar2='?';
while (chChar2 != '\\') { chChar2 = pathname2.charAt(iCur2--); }
String substr1 = pathname1.substring(0,iCur1);
String substr2 = pathname2.substring(0,iCur2);
return( substr1.compareTo(substr2));
}
return(iReturn);
}
public static void main( String args[])
{
//test by changing these path names: drive letter, folder names in path
String filename1 = "C:\\users\\yourusername\\desktop\\file1.dat";
String filename2 = "C:\\users\\yourusername\\desktop\\file2.dat";
SameFolder x = new SameFolder();
if (x.SameFolder(filename1,filename2)==0)
{
System.out.println("Same folder ");
}
else
{
System.out.println("Different folders");
}
}
}