Create Folders Based on Filename and Move Files - Help Please

Discuss and share scripts and script files...
Post Reply
Biggynuff
Posts: 108
Joined: 13 May 2010 14:08

Create Folders Based on Filename and Move Files - Help Please

Post by Biggynuff »

This is driving me crazy. This should be a really simple task, but because I'm even more simple I'm making a mountain out of it. I've tried over and over and just can't get it to work.

Here's the weirdest thing... if I run this script in 'try script' mode, it works perfectly. As soon as I try to run the script on a folder with many files in it, it just locks up XY and the whole thing crashes.

Anyway, here's the script followed by a description of what I'm trying to do:

Code: Select all

sel 1;
   
   while ( <curbase> != '' )
   {
       $B = <curbase>;
       $Name = regexreplace($B, (.+)@(.+)_(.+), $2);  // $2 should return the BASENAME of the file
       moveto "C:\Organized\$Name",  ,  , 2;
       sel 1;
    }
Basically, I have folders with hundreds of files which have filenames such as 78567@BASENAME_sdjljgs. The BASENAME is the important part, the rest are simply random and constantly change, even when the basename part stays the same. The basename is surrounded by an @ symbol and an _ underscore.

What I'm trying to do is to simply run through the list of all files in the folder, extract the basename for each file, then move the selected file into a new folder based on the basename.

So,
oonsgsgdt@TEST_iuihksf would be moved into Organized\TEST
iibienr@ANOTHERTEST_oououns would be moved into Organized\ANOTHERTEST etc etc.

I'm sure this should be a 30 second job, but having tried for hours I'm stuck!

Thanks for any help.

Norn
Posts: 415
Joined: 24 Oct 2021 16:10

Re: Create Folders Based on Filename and Move Files - Help Please

Post by Norn »

Win10, Win11 @100% 2560x1440 22H2

Biggynuff
Posts: 108
Joined: 13 May 2010 14:08

Re: Create Folders Based on Filename and Move Files - Help Please

Post by Biggynuff »

Thank you! I'll take a look at that post and the thread and see if I can come up with something.

Biggynuff
Posts: 108
Joined: 13 May 2010 14:08

Re: Create Folders Based on Filename and Move Files - Help Please

Post by Biggynuff »

I'm working on this but it's driving me mad! I've tried to use the method outlined in the linked page but I can't get anything to work. This is what I have now:

Code: Select all

foreach($item, <get SelectedItemsNames |>, , "e") 
  {
    $name = regexreplace($item, (.*)@(.*)_(.*), $2); // Extract the BASENAME from the file name.
    moveto "$name", , ,2;

 }

The script extracts the basename part that I need to create the new folder, no problem, but then it moves ALL selected files into the very first one that it generates. I expected that the foreach loop would treat each selected file individually? I'm lost!!

PeterH
Posts: 2776
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Create Folders Based on Filename and Move Files - Help Please

Post by PeterH »

Hm - I think if moveto documents that it moves the selected items, it will move the (=all) selected items.
Be it in a loop or not.

So: did you read and understand the documentation?
W7(x64) SP1 German
( +WXP SP3 )

Biggynuff
Posts: 108
Joined: 13 May 2010 14:08

Re: Create Folders Based on Filename and Move Files - Help Please

Post by Biggynuff »

Hi Peter and thanks. I've read the documentation many times but I'm really no good at all at programming, I just don't seem to be able to grasp it. Horses for courses I suppose!

However, I'm now making progress thanks to a little snippet posted by Highend Here:

viewtopic.php?p=169473&sid=db2d90eae3c0 ... 8d#p169473

I've adapted the Regex and so far in testing this seems to be working:

Code: Select all

    foreach($file, listfolder(, , 1), , "e") 

    {
        $folderName = gpc($file, "base");

        $shortened  = regexreplace($folderName, "^(.*)@(.*)_(.*)", "$2");

        moveto $shortened, $file, , 2, 2, 2;
    }

Post Reply