Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 09, 2016, 09:04:35 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: DONE: Need a Script to Move User Files Into a Subfolder  (Read 4085 times)

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
DONE: Need a Script to Move User Files Into a Subfolder
« on: July 26, 2014, 06:43:37 PM »
Okay so I'm in the middle of a nightmare project (cleaning up a kludge while virtualizing and updating the servers for a school) and was hoping someone could give me a hand with this part.

The file server (is a mess) has a group of user folders for the students under D:\Students\Users (so far so good). Now in that Users folder there are folders for each year, that contain folders for each student. The problem is that I need to get all of the files, in all of the student folders, for all of the years, moved into a subdirectory (called Documents) of the student's folder ... So that it will synchronize properly with the newly created Domain DFS Root's User Home folders.

So what I'm looking at is this:

Users\
   2012\
      Student 1
          (All Their Files)
      Student 2
          (All Their Files)
      ...
      Student 199
          (All Their Files)

      Student 200
          (All Their Files)
   2013\
      Student 1
          (All Their Files)
      Student 2
          (All Their Files)
      ...
      Student 199
          (All Their Files)
      Student 200
          (All Their Files)
   ...
   2024\
      Student 1
          (All Their Files)
      Student 2
          (All Their Files)
      ...
      Student 199
          (All Their Files)
      Student 200
          (All Their Files)
   2025\
      Student 1
          (All Their Files)
      Student 2
          (All Their Files)
      ...
      Student 199
          (All Their Files)
      Student 200
          (All Their Files)


And what I need the script to make happen is this:

Users\
   2012\
      Student 1
          Documents\
              (All Their Files)
      Student 2
          Documents\
              (All Their Files)
      ...
      Student 199
          Documents\
              (All Their Files)

      Student 200
          Documents\
              (All Their Files)
   2013\
      Student 1
          Documents\
              (All Their Files)
      Student 2
          Documents\
              (All Their Files)
      ...
      Student 199
          Documents\
              (All Their Files)
      Student 200
          Documents\
              (All Their Files)
   ...
   2024\
      Student 1
          Documents\
              (All Their Files)
      Student 2
          Documents\
              (All Their Files)
      ...
      Student 199
          Documents\
              (All Their Files)
      Student 200
          Documents\
              (All Their Files)
   2025\
      Student 1
          Documents\
              (All Their Files)
      Student 2
          Documents\
              (All Their Files)
      ...
      Student 199
          Documents\
              (All Their Files)
      Student 200
          Documents\
              (All Their Files)


Now if I have to run the script once from inside each of the Year folders that's fine...especially if it makes the script easier to create... :D ...I just don't want to get stuck manually moving all the files for 3,000+ Student folders.

Thank you,

Stoic Joker
« Last Edit: July 27, 2014, 12:46:10 AM by Stoic Joker, Reason: Just Realized I Borked the Title »

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,476
    • View Profile
    • Donate to Member
Re: DONE: Need a Script to Move User Files Into a Subfolder
« Reply #1 on: July 27, 2014, 10:09:17 AM »
Try this, put the command file into the \Users directory, open a CLI and enter MoveIt.cmd

Original version removed because it was "clunky" and structure specific.  :(

Worked here on my limited test with your structure above but try it on something that can afford to be nuked first.

NOTE: Since the robocopy command will output results, you could redirect the output to a file if you want to keep a log in case of Murphy.

Using some of Abterix' info below, (THANKS!), here's a shorter version:

(Cleaned it up a little)
Code: Text [Select]
  1. REM start in the Users folder
  2. @echo off
  3. rem For each year starting 20 do:
  4. for /d %%Y in (20??) do (
  5.         rem Change to year folder
  6.         pushd "%%~Y"
  7.  
  8.         rem For each student folder do:
  9.         for /d %%U in (*) do (
  10.                 robocopy "%%~U" "%%~U\Documents" /e /move /xd "Documents"
  11.  
  12.         )
  13.         rem Return to parent folder (\Users)
  14.         popd
  15. )

One-liner command file, (lots of output since echo isn't turned off):
Code: Text [Select]
  1. for /d %%Y in (20??) do (for /f "usebackq tokens=*" %%U in (`dir /b /ad "%%~Y\*"`) do (robocopy "%%~Y\%%~U" "%%~Y\%%~U\Documents" /e /move /xd "Documents"))

And one-liner for use at command prompt, (same as above except no double % for variables):
Code: Text [Select]
  1. for /d %Y in (20??) do (for /f "usebackq tokens=*" %U in (`dir /b /ad "%~Y\*"`) do (robocopy "%~Y\%~U" "%~Y\%~U\Documents" /e /move /xd "Documents"))
« Last Edit: July 28, 2014, 01:43:28 AM by 4wd, Reason: Using robocopy and Abterix lines. Deleted original. »

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: DONE: Need a Script to Move User Files Into a Subfolder
« Reply #2 on: July 27, 2014, 01:03:37 PM »
Okay first let me say thank you, I'd been hoping you'd show up here as I've seen your work and had a feeling you were the guy for the job. :)

Now the bad news...

It was/is really close in that it did move all the files, but it didn't move any of the folders that were in the Student\ directories. It instead looped and created a documents folder inside of all the sub directories and moved the contents into them.

...And yes it was only a copy that got torched. :D

AbteriX

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,115
    • View Profile
    • Donate to Member
Re: DONE: Need a Script to Move User Files Into a Subfolder
« Reply #3 on: July 27, 2014, 03:13:42 PM »
4wd, isn't this recursive?

mkdir "%~1\Documents"
move "%~1\*.*" "%~1\Documents\"

Since "%~1\*.*" includes Documents already.



I would try it this way...

Pseudo code. I can try it tomorrow too myself, if I get some free time..
Code: Text [Select]
  1. REM start in the Users folder
  2.  
  3. IF EXIST temp\. ECHO error, will quit. & GOTO :EOF
  4.  
  5. REM for each year do:
  6. for /L %Y in (2012,1,13) Do (
  7.  
  8.      REM for each user do:
  9.      for /F "tokens=*" %U in ('dir /b %Y') Do (
  10.  
  11.           md "temp\%Y\%U\documents"
  12.           move "%Y\%U\*.*"   "temp\%Y\%U\documents"
  13.  
  14.           move "temp\%Y\%U\*.*"   "%Y\%U\"
  15.           rem del "temp\%Y\%U\"
  16.      )
  17. )
  18. rem del temp



.
« Last Edit: July 27, 2014, 03:28:18 PM by AbteriX »

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,476
    • View Profile
    • Donate to Member
Re: DONE: Need a Script to Move User Files Into a Subfolder
« Reply #4 on: July 27, 2014, 04:14:26 PM »
Okay first let me say thank you, I'd been hoping you'd show up here as I've seen your work and had a feeling you were the guy for the job. :)

Aw shucks  :-[

Thanks  :)

Quote
It was/is really close in that it did move all the files, but it didn't move any of the folders that were in the Student\ directories. It instead looped and created a documents folder inside of all the sub directories and moved the contents into them.

Ah yes, forgot that case  :-\

The structure you gave above, the \Student xxx, is that the literal structure or figurative?

ie. Is it really \Student 123 or was that just a way of describing the structure?

If it's the first case, then if you change:

for /f "usebackq tokens=*" %%a in (`dir /s /b /ad *.*`) do (call :MakeMove "%%a")

to:

for /f "usebackq tokens=*" %%a in (`dir /s /b /ad Student*`) do (call :MakeMove "%%a")

That'll list only the folders with Student in them and stop the creation of Documents in the sub-folders.


I've changed the move to robocopy because move didn't handle folders, besides which robocopy will do a verified copy before deleting the original.

Modified original command file.

4wd, isn't this recursive?

mkdir "%~1\Documents"
move "%~1\*.*" "%~1\Documents\"

Since "%~1\*.*" includes Documents already.

Didn't seem to be since the move command just ignores folders, which is what SJ mentioned.  I've switched to robocopy since it does it all in one hit and I can exclude the destination folder from the move.

Quote
I would try it this way...

Certainly a better way to create the folders since it takes into consideration non-uniform sub-dir names - might have to rejig that bit later  :)

Using robocopy obviated the need for recursive move commands ... I hope ... seems to work OK with my test structure.

Addendum:  The shorter version using some of Abterix' lines:

All versions are up there.

As before, try it on nuke-friendly structure first.

Modified my first post to add this version.
« Last Edit: July 28, 2014, 02:35:22 AM by 4wd, Reason: Irrelevant content struck thru »

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: DONE: Need a Script to Move User Files Into a Subfolder
« Reply #5 on: July 27, 2014, 10:25:27 PM »
Okay, initial small sample testing looks good ... But I'm going to get some sleep before a try touching this off on a full scale test. I've been pulling 16 hour days for 3 weeks straight on this gig so being fried is a state I past long ago and remember fondly.

Oh and yes the structure is figurative (they actually use name fragments), I was just tying to describe the problem as clearly as possible.

Thank You!

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,476
    • View Profile
    • Donate to Member
Re: DONE: Need a Script to Move User Files Into a Subfolder
« Reply #6 on: July 28, 2014, 12:23:37 AM »
In that case it should be obvious that you should use the shorter versions rather than my original.   ;)

Thank Abterix for that  :Thmbsup:

And as Anthony Quayle used to say: "Pleasant dreams."  >:D
« Last Edit: July 28, 2014, 01:52:45 AM by 4wd »

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: DONE: Need a Script to Move User Files Into a Subfolder
« Reply #7 on: July 28, 2014, 05:46:39 PM »
@AlbertiX - Thank you!

Thank Abterix for that

 :D

Well I ran it live on the production system and it worked perfectly. I even discovered an interesting ("Feature") side effect. It seems that if there is not adequate permissions to move the file, it will loop at 30 second intervals and stall on that file. The advantage here was once noticed, all I had to do was reset the permissions on that file and wait 30 seconds for the script to continue ... Which was way better than having to start the whole shebang over from the beginning.

Now I just have to point it at the 568GB of faculty files and I should be just about done with this part.

Thanks Guys! You really saved my ass on this one.

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,476
    • View Profile
    • Donate to Member
Re: DONE: Need a Script to Move User Files Into a Subfolder
« Reply #8 on: July 29, 2014, 05:27:40 AM »
It seems that if there is not adequate permissions to move the file, it will loop at 30 second intervals and stall on that file.

I noted the permissions aspect when I was playing with the robocopy /COPY: option - didn't get the 30 second loop though, possibly because I wasn't Admin.

Quote
Now I just have to point it at the 568GB of faculty files and I should be just about done with this part.

Is it about now I should mention the hidden format command?

Quote
Thanks Guys! You really saved my ass on this one.

No problem  :)

Probably get skwire to mark as DONE if your OK with it.

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: DONE: Need a Script to Move User Files Into a Subfolder
« Reply #9 on: July 29, 2014, 06:53:30 AM »
It seems that if there is not adequate permissions to move the file, it
Quote
Now I just have to point it at the 568GB of faculty files and I should be just about done with this part.

Is it about now I should mention the hidden format command?

Hay, either way I'm done...so it's all good.. :D

Probably get skwire to mark as DONE if your OK with it.

Werks 4 me.