Page 1 of 1

renmae files in current folder and move them to top folder

Posted: 24 Jan 2012 11:39
by vortex
Hello,

The XYplorer is a good tool, but I just tried script coding for days and still not workable, pls help this:

---------------------------------------------
Here is a folder named A, and also some subfolders and some files in every subfolder, like this:

subfolders under folder A:
1)aaa -> subfolder name is "1)aaa"
2)bbb
3)ccc
......
187)yyy
188)zzz

in every subfolder, here are some files like:
1)f1.jpg -> file name is "1)f1.jpg"
2)f2.jpg
3)f3.jpg
......

I wanna rename all files' name to their own subfolder name + number suffix, and then move them to the root of folder A, and then delete all subfolders.

Results in folder A with no subfolders like:
1)aaa 001.jpg
1)aaa 002.jpg
1)aaa 003.jpg
2)bbb 001.jpg
2)bbb 002.jpg
3)ccc 001.jpg
3)ccc 002.jpg
3)ccc 003.jpg
3)ccc 004.jpg
......
187)yyy 001.jpg
187)yyy 002.jpg
188)zzz 001.jpg
188)zzz 002.jpg
188)zzz 003.jpg


thks...

Re: renmae files in current folder and move them to top fold

Posted: 24 Jan 2012 14:24
by highend
Instructions: Go to the folder (in your case <path\A>) inside XYplorer, that contains the subfolders with the pictures in it.
Execute the script...

Code: Select all

/* 24.01.2012, Move files with patterns
   ::load "<xyscripts>\.snippets\MoveWithPattern.xys";
*/
	setting('BackgroundFileOps', 0);
	$basefolder = "<curpath>";

	$folders = folderreport("dirs", "r", $basefolder, , , "|");
	foreach($item, $folders, "|") {
		// Extract folder name for affix
		$foldername = strpos($item, "\", -1);
		$foldername = substr($item, $foldername +1);
		$report = folderreport("files", "r", $item, , , "|");
		// Move files
		$i = 1;
		foreach($file, $report, "|") {
			moveas "$foldername ".format($i, "000").".jpg", $basefolder, $file;
			$i++;
		}
	}
	delete 1, 1, "$folders";

Re: renmae files in current folder and move them to top fold

Posted: 25 Jan 2012 05:20
by vortex
Thks highend, I'm testing your sample code, and trying modify it to fit my req...

Re: renmae files in current folder and move them to top fold

Posted: 25 Jan 2012 11:36
by highend
vortex wrote:Thks highend, I'm testing your sample code, and trying modify it to fit my req...
Mh? You've listed your requirements and the code was written for them.

There are only two things that should be changed (if you need to).

$basefolder = "<curpath>"; - to - $basefolder = inputfolder() or a fixed path if you don't want to change into your root folder manually and delete 1, 1, "$folders"; if you don't want to be prompted for the deletion of the subfolders :D

Re: renmae files in current folder and move them to top fold

Posted: 25 Jan 2012 14:24
by vortex
highend wrote: Mh? You've listed your requirements and the code was written for them.

There are only two things that should be changed (if you need to).

$basefolder = "<curpath>"; - to - $basefolder = inputfolder() or a fixed path if you don't want to change into your root folder manually and delete 1, 1, "$folders"; if you don't want to be prompted for the deletion of the subfolders :D
highend, thanks again for your professional help...

Yes I use delete 0, 0, "$folders"; to avoid the prompts due there maybe handreds of subfolders even thousands of subfolders; and I also add codes to deal with null subfolders(without files in subfolder) to remove the prompts appeared, and other little codes in getting current subfolder name.

Now it's working perfect with my real folders/files, just process' on a folder with ~3000 subfolders inside and works fine :D :D

here is the final script I use, based on yours:

Code: Select all

/* 24.01.2012, Move files with patterns
   ::load "<xyscripts>\.snippets\MoveWithPattern.xys";
*/
   setting('BackgroundFileOps', 0);
   $basefolder = "<curpath>";
   $curpathlen = strlen($basefolder);

   $folders = folderreport("dirs", "r", $basefolder, , , "|");
   foreach($item, $folders, "|") {
      // Extract folder name for affix
      $foldername = strpos($item, "\", $curpathlen);
      $foldername = substr($item, $foldername +1);
      $report = folderreport("files", "r", $item, , , "|");
      
      // Move files
      if($report!=""){
      	$i = 1;
      	foreach($file, $report, "|") {
         	moveas "$foldername ".format($i, "000").".jpg", $basefolder, $file;
         	$i++;
      	}
      }
      else {
      }
   }
   delete 0, 0, "$folders";
xyplorer is a must run application on my desktop and notebook, the batch rename and the script function is so cool, it saves my times and doing better..

Re: renmae files in current folder and move them to top fold

Posted: 25 Jan 2012 16:32
by highend
Btw, you don't need to use:

Code: Select all

      else {
      }
If there isn't anything to do in an else case :P

Re: renmae files in current folder and move them to top fold

Posted: 25 Jan 2012 18:48
by PeterH
And please excuse, but for:

Code: Select all

         $i = 1;
         foreach($file, $report, "|") {
            moveas "$foldername ".format($i, "000").".jpg", $basefolder, $file;
            $i++;
         }
you can write:

Code: Select all

         $i = 0;
         foreach($file, $report, "|") {moveas "$foldername ".format($i++, "000").".jpg", $basefolder, $file;}
Of course you could keep it on more lines, if you wish...
(... I like it this way.)

But the "integrated $i++" was just created upon my wish :D