Welcome Guest.   Make a donation to an author on the site December 20, 2014, 07:43:01 AM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
View the new Member Awards and Badges page.
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: DONE: Need a Script to Move User Files Into a Subfolder  (Read 1596 times)
Stoic Joker
Honorary Member
**
Posts: 5,448



View Profile WWW Give some DonationCredits to this forum member
« 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... cheesy ...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 » Logged
4wd
Supporting Member
**
Posts: 3,528



see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« 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.  Sad

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)
Formatted for Generic Code with the GeSHI Syntax Highlighter [copy or print]
  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):
Formatted for Generic Code with the GeSHI Syntax Highlighter [copy or print]
  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):
Formatted for Generic Code with the GeSHI Syntax Highlighter [copy or print]
  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. » Logged

I do not need to control my anger ... people just need to stop pissing me off!
Stoic Joker
Honorary Member
**
Posts: 5,448



View Profile WWW Give some DonationCredits to this forum member
« 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. smiley

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. cheesy
Logged
AbteriX
Charter Honorary Member
***
Posts: 1,053


Member #520

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« 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..
Formatted for QBasic/QuickBASIC with the GeSHI Syntax Highlighter [copy or print]
  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 » Logged

Greetings, Stefan.
4wd
Supporting Member
**
Posts: 3,528



see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« 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. smiley

Aw shucks  embarassed

Thanks  smiley

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  undecided

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  smiley

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 » Logged

I do not need to control my anger ... people just need to stop pissing me off!
Stoic Joker
Honorary Member
**
Posts: 5,448



View Profile WWW Give some DonationCredits to this forum member
« 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!
Logged
4wd
Supporting Member
**
Posts: 3,528



see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« 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.   Wink

Thank Abterix for that  Thmbsup

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

I do not need to control my anger ... people just need to stop pissing me off!
Stoic Joker
Honorary Member
**
Posts: 5,448



View Profile WWW Give some DonationCredits to this forum member
« Reply #7 on: July 28, 2014, 05:46:39 PM »

@AlbertiX - Thank you!

Thank Abterix for that

 cheesy

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.
Logged
4wd
Supporting Member
**
Posts: 3,528



see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« 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  smiley

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

I do not need to control my anger ... people just need to stop pissing me off!
Stoic Joker
Honorary Member
**
Posts: 5,448



View Profile WWW Give some DonationCredits to this forum member
« 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.. cheesy

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

Werks 4 me.
Logged
Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.091s | Server load: 0.11 ]