Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Sorting Files into folders

  • 17-05-2005 11:13am
    #1
    Registered Users, Registered Users 2 Posts: 8,382 ✭✭✭


    Quick one guys. Not sure if this can be done. Say I have a list of folders each with 4 to 5 sub folders. Then I have a lot of files in these directories with the subfolders. Is there anyway to automatically sort these files into the subfolders
    by giving them certain criteria for all the parent directories. Not sure if I explained myself well there.


Comments

  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    I suppose it depends on the criteria. If it's stuff like file size, type, date modified or certain characters in the name of the file you could use windows find to give you the listing, then cut and paste into where they're meant to be.


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 93,562 Mod ✭✭✭✭Capt'n Midnight


    What OS ?

    You are looking for a script - it all depends on the criteria , by name , type , date ., size or what ?


  • Registered Users, Registered Users 2 Posts: 8,382 ✭✭✭petes


    server 2003 and sorted into each folder by name. e.g
    Parent Directory


    Folder1

    SubfolderA
    SubfolderB
    SubfolderC

    File1
    File2
    File3

    There are hundreds of directories under the parent directory(folder1,folder2 etc)
    Need to sort the files for each of these the exact same i.e File 1 goes into subfolder a, file 2 into b etc) All will be done with name as the criteria. Is this possible. Again I know in my head whats rewuired, its just putting it down.


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    This is very possible.

    I'm a perl man, so this is a 5 minute perl script which I will write for you if you have perl installed (or are willing to install it). The captain may know how to do it as a batch file or may know someone who can.


  • Registered Users, Registered Users 2 Posts: 8,382 ✭✭✭petes


    Cheers Khannie. Perl is installed already.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    Ok, give me a concrete example of the files you want to work with then and I will write you the script.

    like this if you can:

    C:\parent\
    C:\parent\subfolderA\
    C:\parent\subfolderB\
    C:\parent\fileA.txt
    C:\parent\fileB.txt

    where fileA.txt is to go into subfolderA, fileB.txt into subfolderB, etc.


  • Registered Users, Registered Users 2 Posts: 8,382 ✭✭✭petes


    C:\Office
    C:\Office\1000
    C:\Office\1000\Correspondence
    C:\Office\2000
    C:\Office\2000\Correspondence
    C:\Office\1000\Correspondance\1000corr.doc
    C:\Office\2000\Correspondance\2000corr.doc


    There are actually 2 subfolders. Hope this is clear enough.


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    Yup, that's fine.

    Just checking that you want to move

    C:\Office\1000corr.doc
    to
    C:\Office\1000\Correspondance\1000corr.doc
    etc.

    Are they always 4 digits? e.g. 0001corr.doc up to 9999corr.doc?

    I'm just trying to get exact pattern matches. That's the key here.

    If you want to send me a full listing of the directory structure, do this:

    start->run->cmd <enter>
    in the command prompt:
    cd C:\office
    dir/s *.txt > files.txt
    and email me the files.txt

    If you're going to do this, I'll PM you my email address.


  • Registered Users, Registered Users 2 Posts: 8,382 ✭✭✭petes


    The files are already in C:\Office\1000. A new folder is created under C:\Office\1000\ called correspondance and the files need to go in here. A simple matter for one folder but the folders range from 1000 to 9999. Thanks again.


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    Here you go:
    #FileToDir.pl
    #usage: perl FileToDir.pl
    use strict;
    
    #get directory contents
    opendir PWD, "." or die "Couldn't open the current directory. Check permissions.";
    my @files = readdir(PWD);
    closedir PWD;
    
    #Sort by directory name
    my $filename;
    
    foreach $filename (@files)
    {
    	if (-d $filename && $filename ne "." && $filename ne "..")
    	{
    		my $docfile = $filename . "corr.doc";
    		my $correspondance_dir = $filename . '\\Correspondance';
    		if (-f ($filename . '\\' . $docfile) && -d $correspondance_dir)
    		{
    		print "moving $docfile to $correspondance_dir\n"; 
    		    system 'move ' . $filename . '\\' . "$docfile $correspondance_dir";
    		}
    	}
    }
    
    __END__
    

    copy and paste that into a file called FileToDir.pl, then cd into C:\office and run
    perl C:\<pathtoperlscript>\FileToDir.pl and it'll do the rest for you.

    I'd suggest making a backup copy of the directory first. I can't be held responsible for any screw ups (though I have tested this locally).

    Best of luck!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    Did that work allright?


  • Registered Users, Registered Users 2 Posts: 8,382 ✭✭✭petes


    Cheers for that. Will give that a go this morning.


Advertisement