Link to home
Create AccountLog in
Avatar of CFAIT
CFAIT

asked on

Script to Move Folders on Network Drive

I need to be able to move (not copy) over a thousand folders and their contents to another network location with a script.

Right now, the folders are set up like this:

H:\Legal\Location\00001\Binder
H:\Legal\Location\00002\Binder
H:\Legal\Location\00033\Binder
and so on...

I need to be able to move each Binder folder and its contents to a new location on the network, so that the new folder structure will look like this:
H:\Location\00001\Binder
H:\Location\00002\Binder
H:\Location\00033\Binder
and so on.

I need to leave the original folder structure and files intact, only moving the Binder folder to a new location.  

Any assistance would be much appreciated!  Thank you!
Avatar of knightEknight
knightEknight
Flag of United States of America image

H:
md H:\Location

move  "H:\Legal\Location\00001\Binder"  "H:\Location"
move  "H:\Legal\Location\00002\Binder"  "H:\Location"
move  "H:\Legal\Location\00033\Binder"  "H:\Location"
oops, never mind that - I see the error of my ways.
Stand by ...
Avatar of CFAIT
CFAIT

ASKER

The source folders that are numerical will change and there is no pattern to them.  I need to have a wildcard entry for those, and also keep the same numerical folder names in the destination directory.
H:
MD H:\Location >nul

CD H:\Legal\Location

for /d %%D in (*) do (
  md  "H:\Location\%%D\Binder"
  pushd  "%%D\Binder"
  xcopy/e  *  "H:\Location\%%D\Binder"
  del/s/q  *
  popd
)

Put in a batch file and execute:

REM moveFilesAndFolders.bat

cd /d h:\legal\Location\
for /d %%i in (*) do call :moveOneFolder %%i
goto:EOF

:moveOneFolder
mkdir h:\Location\%1\Binder
cd h:\legal\Location\%1\Binder\
for %%j in (*) do move /-Y %%j h:\Location\%1\Binder\
cd h:\legal\Location\
goto:EOF

Open in new window

Avatar of CFAIT

ASKER

Thank you for your responses - knightEknight, I would rather not use xcopy, and instead use 'move' because of the large amounts of data that we are working with on the network.

sjklein42:  This is almost perfect - but I also need to move the contents of the Binder folder to the new location and remove the Binder folder and its contents from the old location.

Thank you again!
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
It should already be moving the contents of the Binder folder.  I added the rmdir line to delete the old Binder folder after contents have been moved.

REM moveFilesAndFolders.bat

cd /d h:\legal\Location\
for /d %%i in (*) do call :moveOneFolder %%i
move 
goto:EOF

:moveOneFolder
mkdir h:\Location\%1\Binder
cd h:\legal\Location\%1\Binder\
for %%j in (*) do move /-Y %%j h:\Location\%1\Binder\
cd h:\legal\Location\
rmdir h:\legal\Location\%1\Binder\
goto:EOF 

Open in new window

Avatar of CFAIT

ASKER

knightEknight - thank you, that worked perfectly!

sjklein42 - for some reason, that script did not copy the contents of the Binder folder down.

Thank you both so much!
@sjklein42: Your script does not switch to the H: drive. You forgot to change the drive letter after "cd" to  just H: then simply "CD" after the fact.
Russell_Venable,

I think the /D on cd causes it to switch drives as well as folders.

Cheers.
I guess so cheers!