Page 1 of 1 1
Topic Options
#208940 - 2014-05-23 04:38 PM create folder for each file
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
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
_________________________

Top
#208941 - 2014-05-23 04:59 PM Re: create folder for each file [Re: Valentim]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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
_________________________
Actually I am a Rocket Scientist! \:D

Top
#208942 - 2014-05-23 07:07 PM Re: create folder for each file [Re: Glenn Barnas]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
I'm trying to run script, it's just creating folder, files are not moving.
_________________________

Top
#208943 - 2014-05-23 10:14 PM Re: create folder for each file [Re: Valentim]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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?

Top
#208944 - 2014-05-23 10:18 PM Re: create folder for each file [Re: NTDOC]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
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

Top
#208945 - 2014-05-24 12:34 AM Re: create folder for each file [Re: ShaneEP]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
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


Edited by Valentim (2014-05-24 12:35 AM)
_________________________

Top
#208946 - 2014-05-24 01:04 AM Re: create folder for each file [Re: Valentim]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
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.
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.137 seconds in which 0.067 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org