This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Need some help please

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
C
COBRa
Halfop
Posts: 49
Joined: Fri Jan 04, 2013 8:23 am

Need some help please

Post by COBRa »

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.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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: Select all

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: Select all

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: Select all

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: Select all

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: Select all

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: Select all

file rename -- $Source $Target
NML_375
C
COBRa
Halfop
Posts: 49
Joined: Fri Jan 04, 2013 8:23 am

Post by COBRa »

sorry after posting i realised it wasnt /root it was /site/ARCHIVE will this affect the coding
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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
C
COBRa
Halfop
Posts: 49
Joined: Fri Jan 04, 2013 8:23 am

Post by COBRa »

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
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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: Select all

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
C
COBRa
Halfop
Posts: 49
Joined: Fri Jan 04, 2013 8:23 am

Post by COBRa »

tbh this way above my skill level i think im gonna need the script coded for me
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Moderator action: Moved to "Script Request"

/NML_375
NML_375
Post Reply