#208941 - 2014-05-23 04:59 PM
Re: create folder for each file
[Re: Valentim]
|
Glenn Barnas
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$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!
|
Top
|
|
|
|
#208944 - 2014-05-23 10:18 PM
Re: create folder for each file
[Re: NTDOC]
|
ShaneEP
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.
$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
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 ).
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 466 anonymous users online.
|
|
|