+ Post New Thread
Results 1 to 12 of 12
Scripts Thread, Rename all files in a directory(s) in Coding and Web Development; Ok I've got a Folder in location 1, a folder in location 2 and i'm going to be using a ...
  1. #1
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,579
    Thank Post
    368
    Thanked 269 Times in 221 Posts
    Rep Power
    101

    Rename all files in a directory(s)

    Ok I've got a Folder in location 1, a folder in location 2 and i'm going to be using a rather large robocopy script to Move (not copy, move!) all files form all those directories on a set date....

    Now either once the files are moved, or during the file move process I need the files to keep their current names but append 4 numbers with an underscore infront of the current file name so doc1.doc would look like 0123_doc1.doc

    I should also mention that the 0123 number needs to be pulled from active directory, luckily @ChrisMiles created a fantastic little program for pulling out AD attributes for me so i've an application to do that (see this thread: Environment variables GPP)

    Any suggestions?

  2. #2

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,241
    Thank Post
    882
    Thanked 2,743 Times in 2,317 Posts
    Blog Entries
    11
    Rep Power
    784
    Not really helping but copy is a safer operation then deleting after the transfer has been verified. As to the passing of individual rename appends it may be easier to use something like VBS that grabs the variables then sends the commands to robocopy via a shell command. It depends on how you are generating the numbers, per file or per batch as to how efficient this will be.

  3. #3
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,579
    Thank Post
    368
    Thanked 269 Times in 221 Posts
    Rep Power
    101
    Quote Originally Posted by SYNACK View Post
    Not really helping but copy is a safer operation then deleting after the transfer has been verified. As to the passing of individual rename appends it may be easier to use something like VBS that grabs the variables then sends the commands to robocopy via a shell command. It depends on how you are generating the numbers, per file or per batch as to how efficient this will be.
    The reason for preferring the move over copying is that in 5 years time this system will be running for every single student in the school, if we're copying we're going to be duplicating the data in two places on the same drive. Whereas in case of any erroneous moves we will still have shadow copies and backups in place to recover should the worst happen. (will be rigorously tested before hand though i must add).

    I have a rough idea of how to use the GetADAttributes.exe to parse the AD attribute containing the 4 digit number, but i think my idea could be very long winded and over the top.

    Just as a more precise example:
    Location 1 = folder within students home folder
    Location 2 = folder within a share that students will be locked out of once the files are moved across

    Each Student has an individual folder named "Bloggs, Joe" for example in location 2, within that folder is 4 folders called Unit 1, 2 ,3 ,4. Each individual student has their own specific 4 digit candidate number in AD + SIMS, every file copied for that particular student needs to have that 4 digit candidate number appended.

  4. #4

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    9,107
    Thank Post
    352
    Thanked 1,320 Times in 905 Posts
    Blog Entries
    4
    Rep Power
    1135
    Sounds like a utility that I mentioned in this reply might help:

    Link: Rename a batch of files all at once.

    Could be worth a look?

  5. Thanks to DaveP from:

    mrbios (10th October 2012)

  6. #5
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,579
    Thank Post
    368
    Thanked 269 Times in 221 Posts
    Rep Power
    101
    Good little utility that, though it still seems focused on doing things manually by selecting a folder etc. Using Add prefix 1234_ works fine manually but isn't an option in the long run.

    I need to automate it for 100's of folders based on the information within a set field in AD. Unless this program has a command line utility I'm not seeing?

  7. #6
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,579
    Thank Post
    368
    Thanked 269 Times in 221 Posts
    Rep Power
    101
    Just making a little test script and struggling, hopefully someone more experienced with this can fix my problem here:
    Code:
    for %%f in ("H:\Cambridge Nationals\Unit 1\") do chdir /D %%f
    for /f %%g in ('C:\GetADAttributes.exe st') do for %%e in (*) do  rename "%%e" "%%g_%%e"  
    for /f %%a in ('C:\GetADAttributes.exe givenName') do for /f %%b in ('C:\GetADAttributes.exe sn') do for /f %%c in ("H:\Cambridge Nationals\Unit 1\*") do for /f %%d in ("T:\ICT\OCR Coursework Repository\2012-2013\Year 10\%%b, %%a\Unit 1") do move "%%c" "%%d"
    First line changes directory to a set folder within that users home folder
    the second line renames every file within that users folder with the candidate ID from active directory
    The third line: the first part gets the forename, secondly the surename, third the home path and fourth the path it needs moving to, then attempts to do a move command.... problem is as each section is executed on a separate line in a batch file it doesn't like the fact the paths have spaces in them and so once finally executed i end up with this as the final execution:
    Code:
    Move "H:\Cambridge" "T:\ICT\OCR"
    1. How do i stop it from losing the full path?
    2. This is currently only a test, I'm very happy with how the renaming from AD has gone but i need to be able to run this from an admin perspective whereas i currently have it setup to run per user (H:\ etc) and it doesn't appear to like UNC paths either! HELP!

  8. #7

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,731
    Thank Post
    344
    Thanked 520 Times in 488 Posts
    Rep Power
    180
    If you're doing it with spaces you need a tokens variable, else it splits it up per space.

    e.g.

    Code:
    for /f %%d in ("C:\test\AB C") do echo %%d
    Won't work.
    Code:
    for /f "tokens=*" %%d in ("C:\test\AB C") do echo %%d
    Will work.

    Steve

  9. Thanks to Steve21 from:

    mrbios (10th October 2012)

  10. #8
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,579
    Thank Post
    368
    Thanked 269 Times in 221 Posts
    Rep Power
    101
    Thanks steve that worked a treat, so i've now got this:
    Code:
    chdir /D "H:\Cambridge Nationals\Unit 1\"
    for /f %%g in ('C:\GetADAttributes.exe st') do for %%e in (*) do  rename "%%e" "%%g_%%e"  
    for /f %%a in ('C:\GetADAttributes.exe givenName') do for /f %%b in ('C:\GetADAttributes.exe sn') do for /f "tokens=*" %%c in ("H:\Cambridge Nationals\Unit 1\*") do for /f "tokens=*" %%d in ("T:\ICT\OCR Coursework Repository\2012-2013\Year 10\%%b, %%a\Unit 1\") do move "%%c" "%%d"
    And that tells me "cannot move multiple files to a single file" What's the syntax for the move command to make it move files from one folder to another rather than whatever it's currently trying to do?

    I've also no idea how I'm going to make this executable yet, I guess i can't actually make this executable from an admin perspective as I'm pulling so many things out for the logged on user from AD...
    Last edited by mrbios; 10th October 2012 at 11:38 AM.

  11. #9

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,731
    Thank Post
    344
    Thanked 520 Times in 488 Posts
    Rep Power
    180
    Are you trying to move directories or files though?

    As an example:

    Code:
    for /d %%a in ("C:\test\test2\*") do move "%%~a" "C:\test\test3\"
    Will move every folder (and files within) from test2 to test3

    Steve

  12. #10
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,579
    Thank Post
    368
    Thanked 269 Times in 221 Posts
    Rep Power
    101
    Ah I got it, used robocopy instead
    Code:
    chdir /D "H:\Cambridge Nationals\Unit 1\"
    for /f %%g in ('C:\GetADAttributes.exe st') do for %%e in (*) do  rename "%%e" "%%g_%%e"  
    for /f %%a in ('C:\GetADAttributes.exe givenName') do for /f %%b in ('C:\GetADAttributes.exe sn') do for /f "tokens=*" %%c in ("H:\Cambridge Nationals\Unit 1") do for /f "tokens=*" %%d in ("T:\ICT\OCR Coursework Repository\2012-2013\Year 10\%%b, %%a\Unit 1") do robocopy /move /e "%%c" "%%d"
    So that works, renames the files within Unit 1, prefixes them with their candidate number from AD then moves the Unit 1 folder to their named folder within T:\

    So....that works BUT now ideally i need to be able to do this as an admin, so @ChrisMiles any chance of modifying that GetADAttributes so that a domain admin can select a specific users attributes to call up? If it could be done by running say "C:\GetADAttributes.exe givenName bloggsjoe" or something like that it'd be incredible, I know I'm probably asking a hell of a lot there though, and will likely need to find another way of doing this

  13. #11

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    9,107
    Thank Post
    352
    Thanked 1,320 Times in 905 Posts
    Blog Entries
    4
    Rep Power
    1135
    Although this does not address the original question I think it is close enough to attach to this thread [rather than create an new thread]

    Link: Freename - Freeform batch file renaming

  14. #12
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,579
    Thank Post
    368
    Thanked 269 Times in 221 Posts
    Rep Power
    101
    Cheers will take a look at that, at the moment I've got an ex-6thformer who has been working as a software engineer for 2 years helping me out, we're making a purpose built application in C# to do everything i need, quite a fun little project overall He owes us for having gained an education and practically gained his job because of our department so luckily it's all free hehe

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 6
    Last Post: 30th November 2011, 03:12 PM
  2. Replies: 2
    Last Post: 2nd September 2011, 12:10 PM
  3. Blocking ALL Executables in Students Home Directory
    By markwilliamson2001 in forum Windows
    Replies: 31
    Last Post: 9th March 2010, 08:51 AM
  4. Replies: 6
    Last Post: 11th July 2007, 09:53 PM
  5. Replies: 3
    Last Post: 25th September 2006, 12:27 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •