create folder based on the filename

Discuss and share scripts and script files...
kotlmg
Posts: 321
Joined: 30 Jun 2010 17:14

create folder based on the filename

Post by kotlmg »

i want to create the folder based on the file name leaving extensions.
for ex: test1.zip, code should be able create test1 folder.
i do have one script collected from the forum. but is giving error i.e. overflow 0\0.

"Create Those Folders"

//Get the list of selected items, folders or files:
$LIST = Report("{Name}<crlf>", 1);

//For Each Item in Items:
$Loop = 1;
while(1)
{
//Get next ITEM:
$ITEM = gettoken($LIST, $Loop, "<crlf>");
//If ITEM is nothing end this script:
IF ($ITEM==""){break;}

//Get the BASE of the item name, i.e. drop all after including the last dot:
$Folder = regexreplace("$ITEM", "(.+)(\..+)","$1");

//Create new item, here as folder:
new($dir\$Folder, dir);

//next iteration:
incr $Loop;
}


can any one please help me.

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: create folder based on the filename

Post by Stefan »

Enable menu "Scripting > Step Mode" to watch each step the script do.
Then you are able to tell where the script fail.
The [Variables...] button show you the value of the vars... so you have could seen that the var "$dir" is nowhere defined.
(original script where $dir was needed > http://www.xyplorer.com/xyfc/viewtopic. ... 822#p49822)




Anyway, since a few months you don't need to specify the path for that new() command if you want to create the item in the current working dir, so we just can drop this part from the script and it will work:

Code: Select all

"Create Those Folders"

   //Get the list of selected items, folders or files:
   $LIST = Report("{Name}<crlf>", 1);
   
   //For Each Item in Items:
   $Loop = 1; 
   while(1)
   {
      //Get next ITEM:
      $ITEM = gettoken($LIST, $Loop, "<crlf>");
     
      //If ITEM is nothing end this script:
      IF ($ITEM==""){break;}
   
      //Get the BASE of the item name, i.e. drop all after including the last dot:
      $Folder = regexreplace("$ITEM", "(.+)(\..+)","$1");
   
      //Create new item, here as folder:
      new($Folder, dir);
   
     //next iteration:
     incr $Loop;
   }

Here an slightly modified script without the need of RegEx due the use of {BaseName}:

Code: Select all

"Create Those Folders"

   //Get the list of selected items, folders or files:
   $LIST = Report("{BaseName}<crlf>", 1);
   
   //For Each Item in Items:
   $Loop = 1; 
   while(1)
   {
      //Get next ITEM:
      $ITEM = gettoken($LIST, $Loop, "<crlf>");

      //If there is no ITEM anymore, end this script:
      IF ($ITEM==""){break;}
  
      //Create new item, here as folder:
      new($ITEM, dir);
   
   //next iteration:
   incr $Loop;
   }
Last edited by Stefan on 27 Oct 2011 13:14, edited 1 time in total.

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: create folder based on the filename

Post by highend »

Code: Select all

	$SelectedItems = get("SelectedItemsNames", "|");
	foreach($Item, $SelectedItems, "|") {
		$ItemName = regexreplace($Item, "(.+(?=\.))(.+)", "$1");
		new("$ItemName", dir);
	}
This is probably the simplest way to do what you want.

Works with several selected files and creates their pertinent directories.
One of my scripts helped you out? Please donate via Paypal

kotlmg
Posts: 321
Joined: 30 Jun 2010 17:14

Re: create folder based on the filename

Post by kotlmg »

$SelectedItems = get("SelectedItemsNames", "|");
foreach($Item, $SelectedItems, "|") {
$ItemName = regexreplace($Item, "(.+(?=\.))(.+)", "$1");
new("$ItemName", dir);
}

i have used the above code. it worked like a charm for me.
thanks a lot.

kotlmg
Posts: 321
Joined: 30 Jun 2010 17:14

Re: create folder based on the filename

Post by kotlmg »

hello sir,
how to extend the above simple code to cover clipboard contents also?
thanks in advance.

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: create folder based on the filename

Post by highend »

This has to be done in a different approach.

Paste the content of your clipboard with a few entries to this thread (please, use the "Code" tags from the list of format options right above this thread edit window) to see, which format they use.
One of my scripts helped you out? Please donate via Paypal

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: create folder based on the filename

Post by highend »

This is a generic approach.

It takes into account, if you copied only item names or item names with path into the clipboard.
Paths are stripped so it will always create the folders in the current directory.

Code: Select all

"Create folder(s) from selected file(s) / clipboard"
	;

"... from clipboard"
	// Check if a name contains a "\" (Item Path/Name(s) was used
	set $SelectedItems, <clipboard>;
	foreach($Item, $SelectedItems, "<crlf>") {
		if($Item == ""){ break; }
		if(strpos($Item, "\", 0, 1) != "-1"){
			$Path = regexreplace($Item, "(.*\\)(.*)", "$1");
			$ItemName = replace("$Item", "$Path");
			$ItemName = regexreplace($ItemName, "(.+(?=\.))(.+)", "$1");
			new("$ItemName", dir);
		} else {
			$ItemName = regexreplace($Item, "(.+(?=\.))(.+)", "$1");
			new("$ItemName", dir);
		}
	}

"... from selected file(s)"

   $SelectedItems = get("SelectedItemsNames", "|");
   foreach($Item, $SelectedItems, "|") {
      $ItemName = regexreplace($Item, "(.+(?=\.))(.+)", "$1");
      new("$ItemName", dir);
   }
One of my scripts helped you out? Please donate via Paypal

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

Re: create folder based on the filename

Post by admin »

Save the environment and spare some superfluous quotes:

Code: Select all

before: new("$ItemName", dir);
after:  new($ItemName, dir);

kotlmg
Posts: 321
Joined: 30 Jun 2010 17:14

Re: create folder based on the filename

Post by kotlmg »

hello highend, your script is superb. it worked without any hassles and my both requirements were met with one single code. thanks a lot.

i have one more small requirement.
i have files in my folder as follows.

1-devdas.mpg
2-devdas.mpg
devdas.mpg
3-devdas.mpg
4-devdas.mpg

the above pattern of files will be generated when we use winx hd video converter to clip the videos i.e . devdas.mpg, 1-devdas.mpg, 2-devdas.mpg, 3-devdas.mpg, 4-devdas.mpg
if i apply sort, the result will be

1-devdas.mpg
2-devdas.mpg
3-devdas.mpg
4-devdas.mpg
devdas.mpg.
i want the sort results as
devdas.mpg.
1-devdas.mpg
2-devdas.mpg
3-devdas.mpg
4-devdas.mpg
so that i can rename the files in the order of clipping of files.

is it possible with any rename / sort script?
right now i am organizing the files by modified time and applying rename script.
i need a code to rename files based on modified time.
thanks in advance.

zer0
Posts: 2676
Joined: 19 Jan 2009 20:11

Re: create folder based on the filename

Post by zer0 »

I am not highend, but you can enable manual sorting, drag that file above the rest and then do the script magic.
Reporting a bug? Have a wish? Got a question? Use search - View roadmap - FAQs: Forum + XY site
Windows 7/10
Always using the latest stable two-decimal build

kotlmg
Posts: 321
Joined: 30 Jun 2010 17:14

Re: create folder based on the filename

Post by kotlmg »

how to enable manual sorting?

zer0
Posts: 2676
Joined: 19 Jan 2009 20:11

Re: create folder based on the filename

Post by zer0 »

kotlmg wrote:how to enable manual sorting?
Tools | Customize List | Manual Sorting
Reporting a bug? Have a wish? Got a question? Use search - View roadmap - FAQs: Forum + XY site
Windows 7/10
Always using the latest stable two-decimal build

kotlmg
Posts: 321
Joined: 30 Jun 2010 17:14

Re: create folder based on the filename

Post by kotlmg »

manual sorting method is not good. i need a script to automate.

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: create folder based on the filename

Post by serendipity »

kotlmg wrote:manual sorting method is not good. i need a script to automate.
Hint: Use command "sortby". Help>Help on Scripting Commands.

kotlmg
Posts: 321
Joined: 30 Jun 2010 17:14

Re: create folder based on the filename

Post by kotlmg »

highend wrote:This is a generic approach.

It takes into account, if you copied only item names or item names with path into the clipboard.
Paths are stripped so it will always create the folders in the current directory.

Code: Select all

"Create folder(s) from selected file(s) / clipboard"
	;

"... from clipboard"
	// Check if a name contains a "\" (Item Path/Name(s) was used
	set $SelectedItems, <clipboard>;
	foreach($Item, $SelectedItems, "<crlf>") {
		if($Item == ""){ break; }
		if(strpos($Item, "\", 0, 1) != "-1"){
			$Path = regexreplace($Item, "(.*\\)(.*)", "$1");
			$ItemName = replace("$Item", "$Path");
			$ItemName = regexreplace($ItemName, "(.+(?=\.))(.+)", "$1");
			new("$ItemName", dir);
		} else {
			$ItemName = regexreplace($Item, "(.+(?=\.))(.+)", "$1");
			new("$ItemName", dir);
		}
	}

"... from selected file(s)"

   $SelectedItems = get("SelectedItemsNames", "|");
   foreach($Item, $SelectedItems, "|") {
      $ItemName = regexreplace($Item, "(.+(?=\.))(.+)", "$1");
      new("$ItemName", dir);
   }

in addition to the folder, i want to create a text file based on the selected movie. is it possible to add this to the above script please.

thanks in advance.

Post Reply