There and back again

Discuss and share scripts and script files...
Post Reply
FruehBird
Posts: 10
Joined: 27 Mar 2017 20:46

There and back again

Post by FruehBird »

I have a network path and a local path that a lot of files get passed to and from. Previously I was able to use regexreplace to swap out the drive letter and then just use goto. However, the network path now has an additional folder and I can't seem to figure out how to strip out that additional folder in the string to move from network and vice versa.

Local Structure:
J:\JOB-001_JobName
J:\JOB-002_JobName

Network Structure:
P:\ClinetName_1\JOB-001_JobName
P:\ClinetName_2\JOB-002_JobName

To go from Network to Local I would need to simply remove "ClinetName_1\" and Replace "P:" with "J:" and if the folder did not exist to create it.
This I have tried with regex but can't seem to get the output correct I am using:

Code: Select all

(?:(.*?\\.*?)\\)
Which should return :

Code: Select all

P:\ClinetName_1\
However, when using it this way:

Code: Select all

regexreplace( <curpath> , "(?:(.*?\\.*?)\\)" , "J:\" ) ;
It just returns "J:\"

The harder part to this would be going from J: to P: as I would have to search each folder in P: to see if the JOB-001_JobName folder exists. Is looping through the first layer of subfolders doable?

For checking whether or not I am in J: or P: Is there a way to check is a string contains another string?

Code: Select all

//  Check to see if the current directory is the pathway
    if ( "P:" in <curpath> ) {
        // Remove all characters up the the second backslash
        $local = regexreplace( <curpath> , "(?:(.*?\\.*?)\\)" , "J:\" ) ;
        msg $local ;
    }

highend
Posts: 13311
Joined: 06 Feb 2011 00:33

Re: There and back again

Post by highend »

01. Don't know where the exact problem is
You are in a folder like P:\ClinetName_1\JOB-001_JobName
and want to switch to the belonging folder on J, no?

Why not just use:

Code: Select all

regexreplace(<curpath>, "^.*\\" , "J:\");
02. A loop is not necessary

Code: Select all

    $local = gpc(, "base");
    $remotes = quicksearch("/d /maxdepth=1", "P:\");
    $remote = regexmatches($remotes, "^[a-z]:\\.+\\$local");
    // if $remote is NOT empty, target folder found...
03. Use regexmatches() or strpos()
One of my scripts helped you out? Please donate via Paypal

FruehBird
Posts: 10
Joined: 27 Mar 2017 20:46

Re: There and back again

Post by FruehBird »

Wow quick search works really well! Thank you.

The problem is that I might be in a deeper level of the directory. for example. P:\ClinetName_1\JOB-001_JobName\JobSubfolderA\JobSubfolderASubfolderA. When switching back and forth I always want to be in the root JOB-XXX_JobName folder and would have to make sure that I strip all of the characters before and after JOB-001_JobName

For example..

P:\ClinetName_1\JOB-001_JobName\JobSubfolderA\JobSubfolderASubfolderA
would become J:\JOB-001_JobName

And
J:\JOB-001_JobName\JobSubfolderA\JobSubfolderASubfolderA
Would become P:\ClinetName_1\JOB-001_JobName


EDIT: looks like $local = gpc(, "component", 2); will strip the path like I want. so I should be able to use that to finds the directories to go back and forth.

highend
Posts: 13311
Joined: 06 Feb 2011 00:33

Re: There and back again

Post by highend »

So for your first example something like this?

Code: Select all

    $src = "P:\ClinetName_1\JOB-001_JobName\JobSubfolderA\JobSubfolderASubfolderA";
    $dst = regexreplace($src, "^(?:[a-z]:\\[^\\]+\\)([^\\]+)(?:\\.*)?", "J:\$1");
One of my scripts helped you out? Please donate via Paypal

FruehBird
Posts: 10
Joined: 27 Mar 2017 20:46

Re: There and back again

Post by FruehBird »

Yes exactly! I didn't realize you could use the matched groups in regexreplace.

So This is what I have so far and the if/else statements don't seem to work how I would expect.

Code: Select all

    $current = <curpath>;
    $Pathway = (strpos( $current , 'P:')+1);

    if ( $Pathway ){
        $remote = regexreplace( $current , "^(?:[a-z]:\\[^\\]+\\)([^\\]+)(?:\\.*)?", "J:\$1");
        goto $remote;
    }else {
            $local = gpc(, "component", 2);
            $remotes = quicksearch("/d /maxdepth=1", "P:\");
            $remote = regexmatches($remotes, "^[a-z]:\\.+\\$local");
            if ( $remote ) {
                goto $remote;
    }
         
The code seems to always execute the else statement. Do I have the If/Else setup wrong?

highend
Posts: 13311
Joined: 06 Feb 2011 00:33

Re: There and back again

Post by highend »

Code: Select all

    $localDrive  = "J:\";
    $remoteDrive = "P:\";

    if (strpos(<curpath>, $remoteDrive) != -1) {
        $local = regexreplace(<curpath>, "^(?:[a-z]:\\[^\\]+\\)([^\\]+)(?:\\.*)?", "$localDrive$1");
        goto $local;
    } else {
        $local = gpc(, "component", 2);
        $remotes = quicksearch("/d /maxdepth=1", $remoteDrive);
        $remote = regexmatches($remotes, "^[a-z]:\\.+\\$local");
        if ($remote) {
            goto $remote;
        }
    }
One of my scripts helped you out? Please donate via Paypal

FruehBird
Posts: 10
Joined: 27 Mar 2017 20:46

Re: There and back again

Post by FruehBird »

looks like that did the trick. Everything seems to work fine now!

Thank you for your help highend!

Post Reply