Valentim
(Fresh Scripter)
2014-05-23 04:38 PM
create folder for each file

How can I create a folder for each file in a specific location?

For Example:
MovieA.mp4
MovieA.nfo
MovieA-fanart.jpg
MovieA-poster.jpg

The script will create a folder for each file found and all these files seram moved into it.

Folder:
MovieA
MovieA \ MovieA.mp4
MovieA \ MovieA.nfo
MovieA \ MovieA-fanart.jpg
MovieA \ MovieA-poster.jpg


Glenn BarnasAdministrator
(KiX Supporter)
2014-05-23 04:59 PM
Re: create folder for each file

Nice question!

First step, you need to decide how to determine the "root" name, since some are "MovieA.xxx" and others are "Movie1.xxx". If .MP4 is a standard extension, I might start there.. or with a list of standard extensions as shown below. My code assumes a 3-char extension - you may want to use some other logic if you have 4-char extensions.

This is PSEUDOCODE to give you an idea
 Code:
$Extensions = '.MP4.MPG.AVI'
$aFiles = DirList('.')
For Each $File in $aFiles
  $Extn = Right($File, 4)
  If InStr($Extensions, $Extn) ; primary file type?
    $Root = Left($File, Len($File) - 4) ; get root of file name
    If Not Exist($Root)
      MD $Root ; make the folder if needed
    EndIf
  EndIf
  If InStr($File, $Root) = 1  ; root name matches?
    Move $File $Root ; move file to subfolder
  EndIf
Next
Note that this is not tested, you'll need the DirList UDF and need to call it properly, but this should get your started.

I have several Kix scripts that I use to manage my video & audio library, mostly to create a consistent name format and directory structure as you appear to be doing here.

Glenn


Valentim
(Fresh Scripter)
2014-05-23 07:07 PM
Re: create folder for each file

I'm trying to run script, it's just creating folder, files are not moving.

NTDOCAdministrator
(KiX Master)
2014-05-23 10:14 PM
Re: create folder for each file

It's not completed and tested code. It's an IDEA to work on making working code.

First you should try to answer the questions that Glenn asked so that him or others can better assist you.

Is this all local as well and what OS is it on?


ShaneEP
(MM club member)
2014-05-23 10:18 PM
Re: create folder for each file

I tested this on some files I created to match yours. It worked.

 Code:
$folderFiles = DirList(@ScriptDir+"\*.MP4")
$allFiles = DirList(@ScriptDir+"\*.*")

For Each $folderFile in $folderFiles
   $folder = Left($folderFile, -4)
   If Not Exist(@ScriptDir+"\"+$folder)
      MD @ScriptDir+"\"+$folder
   EndIf
   For Each $file in $allFiles
      If Left($file, Len($folder)) = $folder
         Move @ScriptDir+"\"+$file @ScriptDir+"\"+$folder+"\"+$file
      Endif
   Next
Next


function dirlist($dirname, optional $options)
  dim $filename, $counter, $filepath, $mask
  dim $list, $sublist, $subcounter
  $counter=-1
  $dirname=trim($dirname)
  if $dirname=''
    $dirname=@CURDIR
  endif
  if right($dirname,1)='\'
    $dirname=left($dirname,len($dirname)-1)
  endif
  if getfileattr($dirname) & 16
    $mask='*.*'
  else
    $mask=substr($dirname,instrrev($dirname,'\')+1)
    $dirname=left($dirname,len($dirname)-len($mask)-1)
  endif
  redim $list[10]
  $filename=dir($dirname+'\'+$mask)
  while $filename<>'' and @ERROR=0
    if $filename<>'.' and $filename<>'..'
      select
      case (getfileattr($dirname+'\'+$filename) & 16)
        if $options & 1
          $counter=$counter+1
          if $options & 2
            $list[$counter]=$dirname+'\'+$filename+'\'
          else
            $list[$counter]=$filename+'\'
          endif
        endif
        if ($options & 4)
          $sublist=<b>dirlistb>($dirname+'\'+$filename+'\'+$mask,$options)
          if ubound($sublist)+1
            redim preserve $list[ubound($list)+ubound($sublist)+1]
            for $subcounter=0 to ubound($sublist)
              $counter=$counter+1
              if $options & 2
                $list[$counter]=$dirname+'\'+$filename+'\'+$sublist[$subcounter]
              else
                $list[$counter]=$filename+'\'+$sublist[$subcounter]
              endif
            next
          endif
        endif
      case ($options & 2)
        $counter=$counter+1
        $list[$counter]=$dirname+'\'+$filename
      case 1
        $counter=$counter+1
        $list[$counter]=$filename
      endselect
      if $counter mod 10
        redim preserve $list[$counter+10]
      endif
    endif
    $filename = dir('')
  loop
  if $counter+1
    redim preserve $list[$counter]
  else
    $list=''
  endif
  if $mask<>'*.*' and ($options & 4)
    $filename=dir($dirname+'\*.*')
    while $filename<>'' and @ERROR=0
      if $filename<>'.' and $filename<>'..'
        if (getfileattr($dirname+'\'+$filename) & 16)
          $sublist=<b>dirlistb>($dirname+'\'+$filename+'\'+$mask,4)
          if ubound($sublist)+1
            redim preserve $list[ubound($list)+ubound($sublist)+1]
            for $subcounter=0 to ubound($sublist)
              $counter=$counter+1
              if $options & 2
                $list[$counter]=$dirname+'\'+$filename+'\'+$sublist[$subcounter]
              else
                $list[$counter]=$filename+'\'+$sublist[$subcounter]
              endif
            next
          endif
        endif
      endif
      $filename = dir('')
    loop
  endif
  if $counter+1
    redim preserve $list[$counter]
  else
    $list=''
  endif
  $dirlist=$list
endfunction


Valentim
(Fresh Scripter)
2014-05-24 12:34 AM
Re: create folder for each file

Thanks for everyone's help, with the idea that Glenn gave, I created a simple script (I think \:D ).

 Code:
function DirList($dirname, optional $options)
.... UDF ...
endfunction

Function StringReplace($String, $Search ,$Replace)
  $StringReplace = Join(Split($String, $Search), $Replace)
EndFunction

$aExt = Split(".mp4;.mkv;.avi;.rmvb",";")
For Each $Extn In $aExt
  $aFiles = DirList(@ScriptDir + "\*"+$Extn)
  For Each $File in $aFiles

    $OldFile = Left($File, Len($File) - Len($Extn))
    $NewFile = StringReplace(Left($File, Len($File) - Len($Extn)), ".", " ")
    $folder = StringReplace(Left($File, Len($File) - Len($Extn)), ".", " ")

    If Not Exist(@ScriptDir+"\"+$folder)
      MD @ScriptDir+"\"+$folder
    EndIf

    Move @ScriptDir+"\"+$File                       @ScriptDir+"\"+$folder+"\"+$NewFile+$Extn
    Move @ScriptDir+"\"+$OldFile+".nfo"             @ScriptDir+"\"+$folder+"\"+$NewFile+".nfo"
    Move @ScriptDir+"\"+$OldFile+"-fanart.jpg"      @ScriptDir+"\"+$folder+"\"+$NewFile+"-fanart.jpg"
    Move @ScriptDir+"\"+$OldFile+"-poster.jpg"      @ScriptDir+"\"+$folder+"\"+$NewFile+"-poster.jpg"

  Next
Next


ShaneEP
(MM club member)
2014-05-24 01:04 AM
Re: create folder for each file

Just an FYI, the Replace() function was added to Kix a version or two ago. You no longer need to have a UDF to do it if you are using the most recent version.