| View previous topic :: View next topic |
| Author |
Message |
COBRa Halfop
Joined: 04 Jan 2013 Posts: 49
|
Posted: Sat Mar 23, 2013 5:08 am Post subject: Need some help please |
|
|
Hi not sure if im posting in the right place but im trying to create a script to move folders from a root folder to a alphabetically listed folder
for example
This.folder.blah would move from /site/ARCHIVE to /site/ARCHIVE/MOVIE/T and
Same.again.blah would move from /site/ARCHIVE to /site/ARCHIVE/MOVIE/S etc so the script would pick up the the first letter of the first word and move to a predesignated folder
First of all is this possible if so could someone provide some help to get me started plz as i have no clue on how to make this
many thx in advance
Last edited by COBRa on Sat Mar 23, 2013 9:14 am; edited 1 time in total |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Mar 23, 2013 8:58 am Post subject: |
|
|
This would be fully possible, indeed.
Most of the things you'll need is available through the "file" and "glob" commands.
Breaking it down into the various steps:
Getting a list of directories in the source directory
| Code: | | set Directories [glob -type d /source/*] |
This will retrieve all directories under /root. You might consider filtering out directories with one-letter names if your source and destination trees are the same, otherwize you'd just try to move the /root/T directory to be located under /root/T (being /root/T/T, then /root/T/T/T, etc..).
Simplest way to achieve this, is to modify the glob-pattern to something like /source/??*
Extracting the first letter of the directory to be moved
| Code: | | set Target "/destination/[string index $Source 0]" |
This assumes that the name of the directory to be moved, has been stored in Source.
If combining this with the glob-code above, you'd end up with something like this:
| Code: | foreach Source [glob -type d /source/*] {
set Target "/destination/[string index $Source 0]"
#...
} |
If you'd like to make sure that the destination is always upper-case, you could modify the code to use "string toupper":
| Code: | | set Target "/destination/[string toupper [string index $Source 0]]" |
Moving the directory
First off, we'll have to make sure that the destination directory already exists. Otherwize, we'd have to create it before doing anything else
| Code: | if {![file exists $Target]} {
file mkdir $Target
} elseif {![file isdirectory $Target]} {
error "$Target already exists as a file"
} |
The last part is there to handle the case when there might already be a file with the same name as the target directory.
Next, do the actual move
| Code: | | file rename -- $Source $Target |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
COBRa Halfop
Joined: 04 Jan 2013 Posts: 49
|
Posted: Sat Mar 23, 2013 9:16 am Post subject: |
|
|
| sorry after posting i realised it wasnt /root it was /site/ARCHIVE will this affect the coding |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Mar 23, 2013 9:27 am Post subject: |
|
|
Well, in this case, I wrote the examples rather generically. Hence I used /source and /destination as opposed to a real filesystem-path. You'll obviously have to modify these accordingly.
Also, this is not a complete script, but merely the bits'n'pieces you'll need for the specific task of moving the directories.
Looking at the updated post, you'd probably have to take some additional steps to avoid moving /site/ARCHIVE/MOVIE to /site/ARCHIVE/MOVIE/M/MOVIE, in case you are using the glob + foreach method. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
COBRa Halfop
Joined: 04 Jan 2013 Posts: 49
|
Posted: Sat Mar 23, 2013 9:45 am Post subject: |
|
|
Just a question as the archive has a full alphabetical listing A to Z would it be better to use a regex to select the first letter or something ?
/site/archive/movies/a,b,c,d etc |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Mar 23, 2013 11:33 am Post subject: |
|
|
Maybe, maybe not.
It all depends on how you get the names of the directories to be moved.
Using glob with a full path qualifier would cause my previous code to fail, as the returned list contains the whole path, not just the filename. The modification to sort that, however, is rather trivial:
| Code: | | set Target "/destination/[string index [file tail $Source] 0]" |
This could be sorted with a regular expression as well, but I'm not sure you'd gain anything by doing it that way...
For other scenarios, regular expressions might very well be the preferred approach though... _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
COBRa Halfop
Joined: 04 Jan 2013 Posts: 49
|
Posted: Sat Mar 23, 2013 3:56 pm Post subject: |
|
|
| tbh this way above my skill level i think im gonna need the script coded for me |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Mar 23, 2013 4:25 pm Post subject: |
|
|
Moderator action: Moved to "Script Request"
/NML_375 _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
|