Create Folders Based on Filename and Move Files - Help Please

Discuss and share scripts and script files...
Post Reply
Biggynuff
Posts: 110
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: 486
Joined: 24 Oct 2021 16:10

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

Post by Norn »

Windows 11 24H2 @100% 2560x1440

Biggynuff
Posts: 110
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: 110
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: 2826
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

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?

Biggynuff
Posts: 110
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;
    }

chumbo
Posts: 266
Joined: 04 Jan 2015 15:20

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

Post by chumbo »

I needed something exactly like this but it didn't work for me?
I opened a folder with 3 test files:

test1.mp4
test2.jpg
test3.pdf

Expecting:
test1/test1.mp4
test2/test2.jpg
test3/test3.pdf

(tried selecting the files and leaving them unselected but always with the folder open and focused...)
Then I copy/pasted the last script posted above in the 'Run Script..." window and clicked 'OK'.
Nothing happened. Am I missing something?

highend
Posts: 14662
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

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

Post by highend »

Code: Select all

    foreach($item, <get SelectedItemsNames>, <crlf>, "e") { moveto gpc($item, "base"), $item, , 2; }
One of my scripts helped you out? Please donate via Paypal

chumbo
Posts: 266
Joined: 04 Jan 2015 15:20

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

Post by chumbo »

highend wrote: 08 Jan 2026 19:09

Code: Select all

    foreach($item, <get SelectedItemsNames>, <crlf>, "e") { moveto gpc($item, "base"), $item, , 2; }
Perfect, thx!! :appl:

Post Reply