Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 04, 2016, 04:35:52 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Last post Author Topic: IDEA: Program to create subfolders and move files for list of shows BY name.  (Read 10516 times)

famewolf

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 31
    • View Profile
    • Donate to Member
I am requesting a program that given a list of filenames such as:

Airwolf-2009-06-11-0.tp
Airwolf-2009-06-11-0.tp.index.dat
Airwolf-2009-06-11-0.tp.inprogress.xml
Airwolf-2009-06-11-0.tp.timeindex.dat
A-Team-2009-06-11-0.tp
A-Team-2009-06-11-0.tp.chapters.xml
A-Team-2009-06-11-0.tp.index.dat
A-Team-2009-06-11-0.tp.timeindex.dat
A-Team-2009-06-11-1.tp
A-Team-2009-06-11-1.tp.AudioStats.xml
A-Team-2009-06-11-1.tp.index.dat
A-Team-2009-06-11-1.tp.timeindex.dat
A-Team-2009-06-11-1.tp.VideoStats.xml
Knight Rider-2009-06-11-0.tp
Knight Rider-2009-06-11-0.tp.chapters.xml
Knight Rider-2009-06-11-0.tp.index.dat
Knight Rider-2009-06-11-0.tp.timeindex.dat
Rockford Files-2009-06-11-0.tp
Rockford Files-2009-06-11-0.tp.chapters.xml
Rockford Files-2009-06-11-0.tp.index.dat
Rockford Files-2009-06-11-0.tp.timeindex.dat

will create a subfolder for each show (Airwolf, A-Team, Knight Rider, Rockford Files) or use the one that already exists and move each file to the appropriate show.  Ideally I'd like to run this daily from windows task manager at a set time to keep my shows organized.  If a file is open (being written to) the move will probably fail so the program should be able to handle this.   The path to the video folder would be passed on the command line....i.e.  filename.exe G:\Videos

There was a program for beyondtv users that did this but the author has vanished and the program now gives an "index" error of some sort.  Can anyone help us out?  

« Last Edit: June 11, 2009, 08:56:09 PM by famewolf »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,406
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
seems like a good idea for a coding snack or small utility.. probably there are some renamers that might handle it as well.
may i suggest that the tool should use a regular expression that provides the directory name?  e.g. in your case it would be ^(.*)-\d\d\d\d

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,605
    • View Profile
    • Donate to Member
here's a simple AHK script that does what you're looking for


#singleinstance

ifnotexist, showme.ini
msgbox, INI not found - script will exit

iniread, SPath, ShowMe.ini, Paths, source
iniread, TPath, ShowMe.ini, Paths, target

loop, %spath%\*.*,0,0
{
    RegExMatch(a_loopfilename, "P)^(.*)-\d\d\d\d",TmpVar)
    if TmpVar
    {
        NewFold:= substr(a_loopfilename,1,tmpvar-5)
        filecreatedir, %tpath%\%newfold%
        filemove, %a_loopfilefullpath%, %tpath%\%newfold%
        tmpvar:=0
    }
}

it doesn't accept command line parameters (I just knocked this up in my lunch hour) so source and output directories are in an ini file in the format

[PATHS]
Source = d:\temp
Target = d:\temp

if the ini file doesn't exist the script will exit

(All credit to Mouser for the RegEx tip, else I wouldn't have worked this out  :-[)

famewolf

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 31
    • View Profile
    • Donate to Member
Thank you for the script!  How would you handle multiple source/target's?   Just repeat them in the ini?  The ini goes in same dir as the script?  I'm downloading autohotkey which I haven't used previously so will read up on it.  Sorry if I asked something obvious once reading up on AHK.


Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,605
    • View Profile
    • Donate to Member
sorry, AHK is required to run the script, but the script can be compiled into a stand alone (attached) if you like

if you use the installer the script can go anywhere as the installation process sets up the associations

Hadn't considered multiple sources and/or targets - how many do you have?
« Last Edit: June 16, 2009, 12:04:12 AM by Target »

famewolf

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 31
    • View Profile
    • Donate to Member
Hmm so would this change make it pass the filename in from command line?  The documentation for AHK says the script can be compiled to be a standalone exe.   I only have one directory but other beyondtv users might have multiple ones...with the ability to pass in the directory name they can call the script multiple times via windows scheduler...hmm would I need to remove the single instance parm then?

#singleinstance

if 0 < 1  
{
    MsgBox Directory must be passed after the command.
    ExitApp
}

SPath := %1%

loop, %spath%\*.*,0,0
{
    RegExMatch(a_loopfilename, "P)^(.*)-\d\d\d\d",TmpVar)
    if TmpVar
    {
        NewFold:= substr(a_loopfilename,1,tmpvar-5)
        filecreatedir, %spath%\%newfold%
        filemove, %a_loopfilefullpath%, %spath%\%newfold%
        tmpvar:=0
    }
}


Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,605
    • View Profile
    • Donate to Member
not sure about the command line part (though I'm pretty sure your posted code isn't correct)

how about this then

#singleinstance

ifnotexist, showme.txt
msgbox, input file not found - script will exit

Loop, read,ShowMe.txt
{
    stringsplit, MyPath, A_loopreadline,`,
    loop, %Mypath1%\*.*,0,0
    {
        RegExMatch(a_loopfilename, "P)^(.*)-\d\d\d\d",TmpVar)
        if TmpVar
        {
            NewFold:= substr(a_loopfilename,1,tmpvar-5)
            filecreatedir, %Mypath2%\%newfold%
            errorlevel:=0

            filemove, %a_loopfilefullpath%, %Mypath2%\%newfold%
            if errorlevel
                fileappend, %a_loopfilefullpath%, ErrorLog_%A_YYYY%%a_mm%%A_dd%

            errorlevel:=0
            tmpvar:=0
        }
    }
}

replaces the INI file with a text file (ie ShowMe.txt) listing the matching source and target folders in the format

source folder 1,target folder 1
source folder 2,target folder 2
source folder 3,target folder 3
source folder 4,target folder 4
source folder 5,target folder 5

Script will loop through the matching pairs - same process, but it will handle multiple directories from a single calling.  any missed files will be logged to a file called ErrorLog_YYYYMMDD (a plain text file)

note that the text file needs to be in the same folder as the script/exe, and as before, if it's not found it will exit




famewolf

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 31
    • View Profile
    • Donate to Member
Thanks..your script will work fine for me...I'm going to continue to play with the command line part though if for nothing more than an exercise to learn about auto hot key scripting.   The command line code should be close as it came from http://www.autohotke...com/docs/Scripts.htm  ...will look into it more.  There really isn't need for source/target since the new folders should always be created under the original dir...at least in my particular scenario.

Quote
Script Parameters: The string(s) you want to pass into the script, with each separated from the next by a space. Any parameter that contains spaces should be enclosed in quotation marks. A literal quotation mark may be passed in by preceding it with a backslash (\"). Consequently, any trailing slash in a quoted parameter (such as "C:\My Documents\") is treated as a literal quotation mark (that is, the script would receive the string C:\My Documents"). To remove such quotes, use StringReplace, 1, 1, ",, All

The script sees incoming parameters as the variables %1%, %2%, and so on. In addition, %0% contains the number of parameters passed (0 if none). The following example exits the script when too few parameters are passed to it:

if 0 < 3  ; The left side of a non-expression if-statement is always the name of a variable.
{
    MsgBox This script requires at least 3 incoming parameters but it only received %0%.
    ExitApp
}

If the number of parameters passed into a script varies (perhaps due to the user dragging and dropping a set of files onto a script), the following example can be used to extract them one by one:

Loop, %0%  ; For each parameter:
{
    param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
    MsgBox, 4,, Parameter number %A_Index% is %param%.  Continue?
    IfMsgBox, No
        break
}

If the parameters are file names, the following example can be used to convert them to their case-corrected long names (as stored in the file system), including complete/absolute path:

Loop %0%  ; For each parameter (or file dropped onto a script):
{
    GivenPath := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
    Loop %GivenPath%, 1
        LongPath = %A_LoopFileLongPath%
    MsgBox The case-corrected long path name of file`n%GivenPath%`nis:`n%LongPath%
}

Known limitation: dragging files onto a .ahk script may fail to work properly if 8-dot-3 (short) names have been turned off in an NTFS file system. One work-around is to compile the script then drag the files onto the resulting EXE.

« Last Edit: June 16, 2009, 01:03:40 AM by famewolf »

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,605
    • View Profile
    • Donate to Member
I have no experience with the command line options - I've never needed it so I've never had to learn (but I'll be looking into it now!)

the code you posted probably was close, it just didn't look quite right to me...

Quote
There really isn't need for source/target since the new folders should always be created under the original dir

that was my original assumption - but who knows what other people do (or why!)

famewolf

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 31
    • View Profile
    • Donate to Member
I tried to use this program tonight and ran into a few problems..hoping these are minor....

Instead of creating a folder of the show name and copying all episodes to that folder it's creating a folder with BOTH the show name and the episode name...example "Mental - (Manic at the disco)" instead of the folder just being named Mental.  It also tried to move a file currently being written to which resulted in the file being in both the folder AND the root dir but what concerns me more is it moved some of the other files associated with that show.  For each video it also has other files in my example with the same show name and maybe a different extension...I'm not sure what the result will be of having these moved in the middle of a recording.  Can the program attempt to move the *.tp files and only then move the .tp.index etc if that move was successful?  If a move gets an error can it not exist in the folder as well as root dir?

Was all that clear as mud?  While some shows are like Airwolf-2009-06-11-0.tp  some are also like Mental-(Manic at the disco)-2009-06-11-0.tp
« Last Edit: June 18, 2009, 10:36:53 PM by famewolf »

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,605
    • View Profile
    • Donate to Member
Quote
Instead of creating a folder of the show name and copying all episodes to that folder it's creating a folder with BOTH the show name and the episode name...

the problem here is that the naming convention is inconsistent which makes it almost impossible to work out what the correct title is

Where a title includes the episode title will it always be in brackets, and in the format you described (ie NAME - (EPISODE) - XXXX)?


famewolf

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 31
    • View Profile
    • Donate to Member
The older shows don't have episodes which is why the information wasn't displayed however when an episode name is included it will always be in parenthesis like that....is there any way to just take the name till you get to the first "-" and ignore the rest?  Would seem to be easier to do that.


Nothing is ever as simple as you think. ;)


« Last Edit: June 18, 2009, 11:28:21 PM by famewolf »

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,605
    • View Profile
    • Donate to Member
I can deal with the naming convention, though i'll have to think about the 'in use' problem a bit more

famewolf

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 31
    • View Profile
    • Donate to Member
Well the in use is a concern...beyondtv saves the show as a .tp file....however it schedules a job to convert the .tp to a .avi (divx) which I have run from midnight to 8am....so I wonder if I should only copy .avi files and ignore .tp as they have not yet been converted....if they get moved to the subfolders I don't think the queued jobs are gonna work as they will point to the wrong place...I can schedule the move job to run after 8am but if a file didn't get converted in the time allowed it might still be a .tp.....like i said...clear as mud. ;)


Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,605
    • View Profile
    • Donate to Member
looks like there are a number of issues here...

does the conversion process affect all the files, and are all the files required after the conversion?

if you're converting to avi, I suspect you will only need/want the avi files moved (and the balance deleted?)

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 4,664
    • View Profile
    • Donate to Member
is there any way to just take the name till you get to the first "-" and ignore the rest?  Would seem to be easier to do that.

Keep in mind that that logic won't work for one of your examples:  A-Team

famewolf

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 31
    • View Profile
    • Donate to Member
is there any way to just take the name till you get to the first "-" and ignore the rest?  Would seem to be easier to do that.

Keep in mind that that logic won't work for one of your examples:  A-Team

Ouch...your absolutely right.

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,605
    • View Profile
    • Donate to Member
is there any way to just take the name till you get to the first "-" and ignore the rest?  Would seem to be easier to do that.

Keep in mind that that logic won't work for one of your examples:  A-Team

Ouch...your absolutely right.

I got that, but - ( is perfectly acceptable

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,605
    • View Profile
    • Donate to Member
I'm still looking at this but I need a bit more info on what it is you're trying to do

From what you've said so far it sounds like you're -

  • downloading TV shows (as a set of files)
  • Converting each set of files to a corresponding AVI file
  • Moving the final (AVI) file to an appropriately named subfolder
  • deleting the original sets of files (?)

the last step (above) is an assumption on my part.

is this a fair précis of what you're doing?


famewolf

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 31
    • View Profile
    • Donate to Member
I'm still looking at this but I need a bit more info on what it is you're trying to do

From what you've said so far it sounds like you're -

  • downloading TV shows (as a set of files)
  • Converting each set of files to a corresponding AVI file
  • Moving the final (AVI) file to an appropriately named subfolder
  • deleting the original sets of files (?)

the last step (above) is an assumption on my part.

is this a fair précis of what you're doing?



Your exactly onto what I'm doing.   The shows record in beyondtv as .tp files and as you saw, some have the episode name in parenthesis while older shows only have the name.   Beyondtv has a task that converts the .tp file to a .avi file and for me that runs from 12 midnight to 8am on any shows recorded that day or leftover from previous days. There may still be an .avi.chapters or something similar which is where the commercials have been marked for skipping when playing back.    The safest bet I would assume is to run the move job about 8:05am and have it only look at .avi files and their support files...skipping the .tp ones.    This, for me would get around the "file is being written to" issues....not sure how other beyondtv users would like it but in the end I'm the one requesting it and your the one being nice enough to code it..they can be happy with what they get just like I can. :)

Did I answer all the questions?

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,605
    • View Profile
    • Donate to Member
cool, many thanks

this might sound like a stupid question, but what i know about video isn't worth repeating, so just to confirm there could be files associated with the converted AVI's, eg XXX.AVI.XXX, XXX.AVI.YYY, etc 

famewolf

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 31
    • View Profile
    • Donate to Member
cool, many thanks

this might sound like a stupid question, but what i know about video isn't worth repeating, so just to confirm there could be files associated with the converted AVI's, eg XXX.AVI.XXX, XXX.AVI.YYY, etc  

Yes there could be and they would need to be moved as well.   How about if you did you loop based on *.avi but when you got to the actual move you did it based on XXX.* or if it's easier the user could specify the masks in the txt file.

Real example:

smb://famewolf@192.168.2.107/G/Video/A-Team/A-Team-2009-06-18-0.avi
smb://famewolf@192.168.2.107/G/Video/A-Team/A-Team-2009-06-18-0.avi.chapters.xml
smb://famewolf@192.168.2.107/G/Video/A-Team/A-Team-2009-06-18-0.csv
« Last Edit: June 21, 2009, 11:21:49 PM by famewolf »

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,605
    • View Profile
    • Donate to Member
so we're only moving files that don't contain 'TP'?

famewolf

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 31
    • View Profile
    • Donate to Member
Well if your using an exclusion then yes but if a tp file exists you don't want to move ANY of it's files either (xxx.*).....cause it hasn't been converted yet and workfiles might exist.

You have the rare possibility that a .avi could be being written to at the time your job runs..if so it's not going to allow you to move the avi.....it's almost as though you want to try to move the avi and check the result....if positive you can move xxx.*......if an error occured you probably want to do some cleanup and remove the copy that might exist in the subfolder....was that clear as mud?

Here's a list of the files currently in the video dir.....

smb://famewolf@192.168.2.107/G/Video/Airwolf-2009-06-21-0.tp
smb://famewolf@192.168.2.107/G/Video/Airwolf-2009-06-21-0.tp.index.dat
smb://famewolf@192.168.2.107/G/Video/Airwolf-2009-06-21-0.tp.timeindex.dat
smb://famewolf@192.168.2.107/G/Video/airwolf-2009-06-21-0.xml
smb://famewolf@192.168.2.107/G/Video/Hardy Boys_Nancy Drew Mysteries-2009-06-21-0.tp
smb://famewolf@192.168.2.107/G/Video/Hardy Boys_Nancy Drew Mysteries-2009-06-21-0.tp.index.dat
smb://famewolf@192.168.2.107/G/Video/Hardy Boys_Nancy Drew Mysteries-2009-06-21-0.tp.timeindex.dat
smb://famewolf@192.168.2.107/G/Video/hardy boys_nancy drew mysteries-2009-06-21-0.xml
smb://famewolf@192.168.2.107/G/Video/Return to Me-2009-06-21-0_cut.xml
smb://famewolf@192.168.2.107/G/Video/Return to Me-2009-06-21-0.tp.index.dat
smb://famewolf@192.168.2.107/G/Video/Return to Me-2009-06-21-0.tp.timeindex.dat
smb://famewolf@192.168.2.107/G/Video/ts0.header.dat
smb://famewolf@192.168.2.107/G/Video/ts0.index.dat
smb://famewolf@192.168.2.107/G/Video/ts0.timeindex.dat
smb://famewolf@192.168.2.107/G/Video/ts1.header.dat
smb://famewolf@192.168.2.107/G/Video/ts1.index.dat
smb://famewolf@192.168.2.107/G/Video/ts1.timeindex.dat


I show you all the ts0 and ts1 files cause they should never move....the tp's and their support files should be ignored.....you'd be looking for some like these to move:

smb://famewolf@192.168.2.107/G/Video/The Listener/The Listener-(A Voice in the Dark)-2009-06-11-0.avi
smb://famewolf@192.168.2.107/G/Video/The Listener/The Listener-(A Voice in the Dark)-2009-06-11-0.avi.chapters.xml
smb://famewolf@192.168.2.107/G/Video/The Listener/The Listener-(Some Kinda Love)-2009-06-18-0.avi
smb://famewolf@192.168.2.107/G/Video/The Listener/The Listener-(Some Kinda Love)-2009-06-18-0.avi.chapters.xml
smb://famewolf@192.168.2.107/G/Video/The Listener/The Listener-(Some Kinda Love)-2009-06-18-0.csv
smb://famewolf@192.168.2.107/G/Video/The Listener/The Listener-(Some Kinda Love)-2009-06-18-0.log


I'm cutting and pasting across the network so obviously the local paths would be G:\Video\Filename*

I think I've used up my "one hour of coding"....is your brain hurting yet?  ;)

« Last Edit: June 21, 2009, 11:54:08 PM by famewolf »

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,605
    • View Profile
    • Donate to Member
try this and let me know how it goes

/*
A DonationCoder coding snack by Target
EpiFile - a TV show filer
dated  - Jun09

moves TV episode files to an appropriately named sub directory

requires a comma separated 'input file' containing the source and base target directories in the format
 'source dir,target dir' or 'd:\temp,d:\temp' (don't include the quotes).  Multiple sources can be
included by adding extra lines eg

d:\temp,d:\temp
d:\temp1,d:\temp
d:\temp2,d:\temp2

questions or feedback should be posted in the forum at -
     http://www.donationcoder.com/forum/index.php?topic=18729.0
*/
#singleinstance
install, epifile.txt, epifile.txt

ifnotexist, epifile.txt
msgbox, INI not found - script will exit
   
Loop, read,epifile.txt
{
    stringsplit, MyPath, A_loopreadline,`,
    loop, %Mypath1%\*.*,0,0
    {
        if a_loopfilename contains .tp ;,000
        {
        }   
        else
        {
            newfold:= regexreplace(a_loopfilename,"-\d\d\d\d\-\d\d-\d\d-.*")
            newfold:= regexreplace(newfold,"- \(.*\)")
            newfold:= regexreplace(newfold," $")
           
            filecreatedir, %Mypath2%\%newfold%
            errorlevel:=0
               
            filemove, %a_loopfilefullpath%, %Mypath2%\%newfold%
            if errorlevel
            {
                if errlist
                    errlist.= "," . a_loopfilefullpath
                else
                    errlist:= a_loopfilefullpath
            }       
            errorlevel:=0
        }
    }
}
fileappend, %mypath1%, %A_YYYY%%a_mm%%A_dd%.error

script will move anything that doesn't contain .TP (this is configurable)

I haven't done anything about locked/in use files - logically this seems more to me an issue of timing.  

Personally I would probably run something like this before the conversion stage.  This would clear out the converted files, and avoid the in use issue completely
« Last Edit: June 22, 2009, 06:57:54 PM by Target »