Move selected files and folders into parent folder.

Discuss and share scripts and script files...
Post Reply
leinad
Posts: 47
Joined: 17 Jun 2010 09:08

Move selected files and folders into parent folder.

Post by leinad »

When you unzip a file, you usually don't know if the zipped content is in the root of the zip file or inside a single root folder of the zip file, unless you open it and look at the content.

If you have the habit of using "Extract to zipfilename\", in order to avoid tons of files cluttering your desktop if you use "Extract Here" and when the content of the file was not contained in a single root folder, you'll occasionally end up extracting the files into zipfilename\ziprootfolder\*.* where ziprootfolder is then the only item inside the zipfilename folder.

The following script helps you to move all those items into the parent folder.

Code: Select all

$initial = getinfo("CountSelected");
  if ($initial == 0) {
   echo "Command aborted, nothing is selected!";
   end 1;
  };
  $parent = RegExReplace(<curpath>, "(.*)\\[^\\]+", "$1");
  $child = <curfolder>;
  moveto $parent;
  goto $parent;
  sel "[$child]";
The above code works fine. I wanted to let the script check the original folder, and if it is empty, then offer to delete the folder, but for some reason the moveto appears to be an asychronous command, so this doesn't work:

Code: Select all

$initial = getinfo("CountSelected");
  if ($initial == 0) {
   echo "Command aborted, nothing is selected!";
   end 1;
  };
  $parent = RegExReplace(<curpath>, "(.*)\\[^\\]+", "$1");
  $child = <curfolder>;
  moveto $parent;
  sel "a";
  $remainder = getinfo("CountSelected");
  goto $parent;
  if ($remainder == 0) {
    if (confirm("Delete the empty folder named '$child'?") == 1) {
      delete $child, 1, 0;
      end 1;
    }
  }
  sel "[$child]";

admin
Site Admin
Posts: 66249
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Move selected files and folders into parent folder.

Post by admin »

FYI, moveto takes relative path syntax (relative to current path), so you can simply use ".." to designate the parent of the current folder.

(BTW, goto also takes relative path syntax, but relative to <xypath> (path of XYplorer.exe)!)

leinad
Posts: 47
Joined: 17 Jun 2010 09:08

Re: Move selected files and folders into parent folder.

Post by leinad »

Ok, good to know the thing with the relative syntax.

I've fixed the code a bit, and, since I now disabled "File Operations|Enable background processing|Apply to...|Move intra-volume (runs in parallel process)", the code below is working just fine.

Having a blocking version of the moveto, where a parameter could specify if it is a blocking action, would be nice to have (but not so important, since disabling an out of process intra-volume move operation is not really that bad).

But what happens with cross-volume moves with background operations enabled? Well, the code below is a special case which will never be doing that, but there might be other cases where it could be useful to have a blocking moveto, maybe for large batch operations which take a couple of hours, it would be nice then to have the script then continue it's execution automatically.

Code: Select all

$initial = getinfo("CountSelected");
  if ($initial == 0) {
   echo "Command aborted, nothing is selected!";
   end 1;
  };
  $initial = <curpath>;
  $parent = RegExReplace($initial, "(.*)\\[^\\]+", "$1");
  $child = <curfolder>;
  moveto $parent;
  sel "a";
  $remainder = getinfo("CountSelected");
  goto $parent;
  if ($remainder == 0) {
    if (confirm("Delete the empty folder named '$child'?") == 1) {
      delete 1, 0, $initial;
      end 1;
    }
  }
  sel "[$child]";

admin
Site Admin
Posts: 66249
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Move selected files and folders into parent folder.

Post by admin »

Agreed, one could add a parameter to moveto, copyto, and backupto, to force foreground processing.

nas8e9
Posts: 2232
Joined: 21 Jun 2008 14:50

Re: Move selected files and folders into parent folder.

Post by nas8e9 »

Another option could be to call

Code: Select all

setting BackgroundFileOps, 0
before the move operation and

Code: Select all

setting BackgroundFileOps, 1
afterwards?

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Move selected files and folders into parent folder.

Post by SkyFrontier »

Can this one have a improvement so through script/search results one could have a level 2 checker/auto move to level 1?
Like this:

<base>\<level1>\<level2>\<level3>\onwards...

Themes\Pictures\JPGs\File1.jpg - File1.jpg will be moved to "Pictures".

Themes\theme\Text1.txt - left untouched, it's on level 1 folder (themes).

Themes\Use\Assorted\Completed\Work\Pallete.bmp - will turn into Themes\Use\Completed\Work\Pallete.bmp

So level 3 SUBFOLDERS will also be moved to level 1, whatever name they have.

Checking must be relative to user defined level 1, say:

c:\art files\field work\Themes
-user could tell XYscript that "themes" folder is "base", level 1 for any reason is their sufolders, moving any deeper level files/folders one level up.

"Automation" of the process is the key. Instead of scanning folder after folder and doing whatever needed changes, XY could do it in a single stroke for hundreds of folders. Useful for website maintenance, picture/movie webcam folders, download folders organization and generic tarbomb recently discussed situations.

For a fine-tuning, level "X" (say "4", for instance") could be set to be ignored - just an idea.

Thank you all!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Post Reply