Validating uppercase/lowercase of user input with perl compared to unix folders

Hi,

I need to copy files from a source directory to a destination directory in unix.
I'm using the file::copy for the actual copy.

The problem is that the source and dest directories are supplied by different users, who might type the name of the directories in various combinations of lower case and upper case. This would be easy to solve, except no one kept here a naming standard, so the actual directories in unix can also be different combinations of uc and lc.

I was told that in most paths, only the two last directories would be in random case (that is: /Bla/Bla/Bla/random case/random case). I got around that by splitting the paths, taking the last two vars, then going over the directories with opendir and comparing everything under the directory to a non-case regex of the var I need (:wall:):

$slash = "/";
@split_path = split($slash,$dstpath);
$last = $split_path[$#arr];
$before_last = $split_path[$#arr-1];
pop @split_path;
pop @split_path;
$path = join($slash,@split_path);
opendir (DIR,$path);
while ($dir_name = readdir(DIR)) {
     $path = $path.$slash.$dir_name if ($dir_name =~ /^$before_last$/i);
}
closedir (DIR);
opendir (DIR,$path);
while ($dir_name = readdir(DIR)) {
     $path = $path.$slash.$dir_name if ($dir_name =~ /^$last$/i);
}

And this works... but what do I do in the case the folders before those two are also random case? Is there a simple recursive way to tackle this?

Also would this check:

&& (-d $dir_name)

work to make sure I found a directory and not a file?

Many thanks :slight_smile:

I don't think this can be rigorously solved. UNIX will allow them to create many folders with the same name in different casing. How would users differentiate between them now?

How I'd handle it would be going through their folders and renaming them all lower-case for them.

If you're stuck with this mess, loop through whole folders converting everything to lowercase before comparing. If they match as lowercase, it's "correct", so use the unconverted mixed-case foldername.

1 Like

Hi Furou,

Strange issue if I understood correctly. Names in Unix (most of them at least) are case sensitive, so users should provide the correct name and not leave you the job of searching for the correct one.

What about if the user gives you 'myFolder' and there are two of them, one 'myfolder' and other MYFOLDER?

I would use the grep() function instead open each dir and read all its contents.

Regards,
Birei

1 Like

The paths end with /enviornment/application_code.

So it could be /..../Prod/XX, /PROD/xx, /prod/Xx, whatever whoever created the folders felt like naming them. Also beneath them are Main or main or MAIN, and beneath that too - Scr, SCR, scr....

Then I might be supplied those same folder names as parameters in different combos.. and even if not, even if I always get the parameters in the same case, I still need to find out in what case exactly that folder is written in, because I need to transfer files there. Otherwise I would've used a lower case comparison like you suggested Corona688.

birei, so I can I use the code I wrote (I mean the same logic), but replace the whole opendir->while loop, with a grep?

Worst case scenario I'll give the users the responsibility (since they are in charge of whatever's in those directories) to find out how the folders are written and make sure the names they supply the script and the actual names correspond, themselves...

@Furou:

Something like this, not tested but I hope you get the idea:

grep { -d && m|^(?i)$before_last/$last$| } glob qq[*/*];

Regards,
Birei

1 Like

You can still compare them in lower case, just use the unconverted mixed-cased names from readdir after you've done so.

This will make it ambiguous when more than one identical name with different cases exists. Whichever comes first in the arbitrary listing order wins. I don't think you can avoid that.

1 Like

Ok. Thanks to the both of you :slight_smile: