Yes, but counting files in the highest subfolder value isn't required either. With your help, I have managed to get it working
The below code is working as per expectations (though it lagged a little bit on moving it to a sub-folder - don't know why).
Thanks again for helping me out.
Code: Select all
//Destination --> Required, destination folder
//Source --> Optional, if source is not provided, the currently selected files are copied
//Limit --> Optional, number of files per sub-folder. Defaults to 1000
function ltdFileMove($destination = "", $selections = "", $limit = 1000) {
if (!$destination) {
status "Error!! Please provide a destination folder";
return -1;
}
if (!$selections) {
$selections = get("SelectedItemsPathNames", "|"); //store
}
$countFilesToBeMoved = gettoken($selections, "count", "|");
if (!$countFilesToBeMoved) {
status "No files were selected";
return -1;
}
$filesInFolder = listfolder($destination, , 1, "|");
$countFilesInFolder = gettoken($filesInFolder, "count", "|");
$countFilesToMoveFromSourceToSubDir = 0;
$countFilesFollowingMove = $countFilesInFolder + $countFilesToBeMoved;
if ($countFilesFollowingMove > $limit) {
$latestSubFolder = getpathcomponent(gettoken(formatlist(listfolder($destination, , 2, "|"), "rn"), 1, "|"), "base");
if !($latestSubFolder) {
$latestSubFolder = 0;
}
if ($countFilesInFolder)
{
$startIndex = 1;
while ($countFilesFollowingMove > $limit)
{
$latestSubFolder = $latestSubFolder + 1;
$dstFolder = "$destination\$latestSubFolder" ;
$filesToMove = getTokenRange($filesInFolder, $startIndex, $limit, "|");
moveto $dstFolder, $filesToMove, , 2;
$newNames = get("targetitems", "|");
rename b, "<#1> /s", , $newNames;
if ($countFilesInFolder >= $limit) {
$countFilesFollowingMove = $countFilesFollowingMove - $limit ;
$countFilesInFolder = $countFilesInFolder - $limit ;
} else {
$countFilesFollowingMove = $countFilesFollowingMove - $countFilesInFolder ;
$countFilesToMoveFromSourceToSubDir = $limit - $countFilesInFolder;
$countFilesInFolder = 0;
break; //Get out of the loop
}
$startIndex = $startIndex + $limit;
}
}
$startIndex = 1;
$countFilesToMoveFromSourceToSubDir = $countFilesToMoveFromSourceToSubDir % $limit;
if ($countFilesToMoveFromSourceToSubDir) {
$filesToMove = getTokenRange($selections, $startIndex, $countFilesToMoveFromSourceToSubDir, "|");
moveto $dstFolder, $filesToMove, , 2;
rename b, "<#1> /s", , $newNames ;
$startIndex = $startIndex + $countFilesToMoveFromSourceToSubDir;
$countFilesToBeMoved = $countFilesToBeMoved - $countFilesToMoveFromSourceToSubDir;
$countFilesToMoveFromSourceToSubDir = 0;
}
while ($countFilesToBeMoved > 0) {
if ($countFilesToBeMoved > 1000) {
$latestSubFolder = $latestSubFolder + 1;
$dstFolder = "$destination\$latestSubFolder" ;
} else {
$dstFolder = "$destination";
}
$filesToMove = getTokenRange($selections, $startIndex, $limit, "|");
moveto $dstFolder, $filesToMove, , 2;
$newNames = get("targetitems", "|");
rename b, "<#1> /s", , $newNames ;
$countFilesToBeMoved = $countFilesToBeMoved - $limit;
$startIndex = $startIndex + $limit;
}
} else {
moveto $destination, $selections, , 2;
$newNames = get("targetitems", "|");
rename b, "<#1> /s", , $newNames ;
}
return 1;
}
function getTokenRange($str, $index=1, $count=1, $sep=" ", $format="") {
if ($count > 1) {
$all = gettoken($str, $index, $sep, $format, 2);
$rem = regexreplace($sep . gettoken($str, $index + $count, $sep, $format, 2), "([\\^$.+*|?(){\[])", "\$1");
return regexreplace($all, $rem . "$");
}
return gettoken($str, $index, $sep, $format);
}