Thread: Moving files from one directory to multiple dirs

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    10

    Moving files from one directory to multiple dirs

    I have a project that I need to complete to run unattended on windows computer.

    I would like to schedule it to run at a set interval or to have it watch a folder for activity and then process the files.

    I have heard that C is a good programming language to use and am hoping that I can accomplish this task qucikly, here it is:

    I need to take files that are placed into a directory and based on the last 4 characters of the file name place them into different directories on the same computer hard drive.

    an example would be these files in the original directory
    file1v004.pdf
    file2v009.pdf
    file3v004.pdf
    file4v008.pdf

    they should be placed into the correct directories as here
    directory004
    -file1v004.pdf
    -file3v004.pdf
    directory008
    -file4v008.pdf
    directory009
    -file2v009.pdf


    Any help in finding functions or procedures that I can incorporate into/with my own code would be helpful.

    I have written BASIC programs in the distant past and also can write ASP code but C is too new for me to just dive in and start.

    Thanks for any help.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I love c, but let me recommend the right tool for the task: VBScript. You already know ASP, so that tells me that you know VBScript. You might not know that VBS only needs a scripting host to run under, and the ASP engine is one such host. But for a command line driven app, you should run under cscript.exe. You invoke a script with "cscript.exe script.vbs" from a windows command line. You can't use Response.Write (thats ASP specific) but if you google Wscript, that is an object you can use. Also you can google "Scripting.FileSystemObject", since that is the object you need to copy the files, and to check if the file exists or has been modified, etc. There is lots of free code posted on the Web for VBS, so this should take you all of 10 minutes.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    If you read through my 3 part tutorial here you'll see how to log your receiving directory. Extract the key info from the filename, build a target directory string and call MoveFile() or CopyFile() as appropriate.

    The tutorial is written using C++ I/O but change the headers and cout's to printf() and it will work, the API routines are language neutral.

    There are other ways to do this, but ths is probably simplest.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    If you decide to do it in c you will need these functions defined in winbase.h
    FindFirstFile
    FindNextFile
    MoveFile
    CreateDirectory

    You beet me to it adrianxw
    Last edited by Quantum1024; 03-18-2005 at 05:07 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Boy you guys sure like to do things the hard way. Why don't you just use rename? Read the man page.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    10

    Many thanks

    I want to say thank you to everyone, all of your posts provided very useful information. I think I will be able to get this done right away thanks to you all.

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Using rename() is fine, it is pretty much equivalent to MoveFile(), but it assumes that you already know the source filename. If you are watching a directory for files arriving, as he says, rename() by itself achieves nothing.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's like saying prinf alone won't display the output I want. I have to have variables with my information in it and pass it to that function! Of course it won't do it alone. For starters, you have to build a directory name for it based off of the file name. So yeah, just using rename all by itself isn't going to work. You'll have to put a bit more thought into it.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2008, 09:02 AM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 05:27 PM
  3. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  4. Reading files in a directory
    By roktsyntst in forum Windows Programming
    Replies: 5
    Last Post: 02-07-2003, 10:04 AM
  5. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM