Page 1 of 1

[Scripting help] move selected files to folder due to name

Posted: 29 Oct 2015 11:27
by xnmp
i have a folder that contains file like this
AYANE_COS_001.bmp
christie_COS_001.bmp
helena_COS_001.bmp
i want to write a script do this with the selected files :
get the string right before "_" character (without quotes) in filename and set it to the target folder i want to move files to (and suffix rename if there is duplicate file name)
for example
"AYANE_COS_001.bmp" will be move to "E:\copydir\AYANE\"
"christie_COS_001.bmp" will be move to "E:\copydir\christie\"
"helena_COS_001.bmp" will be move to "E:\copydir\helena\"
(that means "string_blablabla.bmp" will be move to "E:\copydir\string" )
(and suffix rename if there is duplicate file name)

i can get the target foldername by regexreplace "selecteditemsnames"
$targetfolder = regexreplace($selecteditemsnames,"_.*","")

but i have no idea how to do next ,i have no succeed with "foreach" yet

i can do this with autohotkey ,but if i move files with autohotkey ,the comments and tags of files will be lost
Any help would be really appreciated
thanks alot

Re: [Scripting help] move selected files to folder due to na

Posted: 29 Oct 2015 11:51
by highend
moveto doesn't support a on collision setting (to e.g. prefix something for an existing dst file).

So you either have to use backupto or use a while loop inside the foreach loop to check if the destination file already exists and add the appropriate suffix to it...

Code: Select all

    $dstDir = "E:\copydir";

    $selectedFiles = get("SelectedItemsPathNames", "|");
    foreach($file, $selectedFiles) {
        $affix = regexmatches(getpathcomponent($file, "file"), "^.*?(?=_)");
        if !(exists("$dstDir\$affix")) { new("$dstDir\$affix", "dir"); }
        backupto "$dstDir\$affix", $file, 4, , 0, 0, , , 0;
    }
    delete 1, 0, $selectedFiles;

Re: [Scripting help] move selected files to folder due to na

Posted: 29 Oct 2015 12:31
by xnmp
highend wrote:moveto doesn't support a on collision setting (to e.g. prefix something for an existing dst file).

So you either have to use backupto or use a while loop inside the foreach loop to check if the destination file already exists and add the appropriate suffix to it...
Thank you very much ,you are so quick
the script works fine when i tried with about <7 files at once (each files are 1MBytes)
but when i tried with about 50 files, there are files not copied , so i try viewing "background jobs" and see only 30 files copied (size of them about 30mb) and about 20 files not copied yet (about 20mb ) ,theirs status is "loading" , i wait for a while but their status not changed
then i tried deleting this line in your code and tried several times but this problem still happened

Code: Select all

  delete 1, 0, $selectedFiles;
i guess it's because xy or the computer can't handle many files copied at once or something,so i will do backup about <7 files at once
it 's fine with me :mrgreen: :mrgreen:
thank you for your helps , i really appreciate it :)

Re: [Scripting help] move selected files to folder due to na

Posted: 29 Oct 2015 12:53
by highend
use

setting "BackgroundFileOps", 0;

before the foreach loop

Re: [Scripting help] move selected files to folder due to na

Posted: 29 Oct 2015 13:37
by xnmp
highend wrote:use

setting "BackgroundFileOps", 0;

before the foreach loop
Thank you very much, it works very well :mrgreen: