Create Folders Based on Filename and Move Files - Help Please
Posted: 06 Jun 2023 16:50
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:
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.
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;
}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.