ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > Finished Programs

DONE: Need a Script to Move User Files Into a Subfolder

(1/2) > >>

Stoic Joker:
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

4wd:
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 ---REM start in the Users folder@echo offrem For each year starting 20 do:for /d %%Y in (20??) do (        rem Change to year folder        pushd "%%~Y"         rem For each student folder do:        for /d %%U in (*) do (                robocopy "%%~U" "%%~U\Documents" /e /move /xd "Documents"         )        rem Return to parent folder (\Users)        popd)
One-liner command file, (lots of output since echo isn't turned off):

--- Code: Text ---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 ---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"))

Stoic Joker:
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:
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 ---REM start in the Users folder IF EXIST temp\. ECHO error, will quit. & GOTO :EOF REM for each year do:for /L %Y in (2012,1,13) Do (      REM for each user do:     for /F "tokens=*" %U in ('dir /b %Y') Do (           md "temp\%Y\%U\documents"          move "%Y\%U\*.*"   "temp\%Y\%U\documents"           move "temp\%Y\%U\*.*"   "%Y\%U\"          rem del "temp\%Y\%U\"     ))rem del temp


.

4wd:
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. :)-Stoic Joker (July 27, 2014, 01:03 PM)
--- End quote ---

Aw shucks  :-[

Thanks  :)

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.
--- End quote ---

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.
-AbteriX (July 27, 2014, 03:13 PM)
--- End quote ---

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.

I would try it this way...
--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version