简体   繁体   中英

Move nested folders and files to new location using a batch file

I have 200 users whose files and folders all reside in a random number of nested folders and files in their user area on our server. The only guarantee is that each user has the following three folders. My Music , My Pictures , My Videos .

I would like to write a batch file that moves:

H:\\My Music (and all its contents) to H:\\Music

H:\\My Pictures (and all its contents) to H:\\Pictures

H:\\My Videos (and all its contents) to H:\\Video

And finally all their remaining files and folders to the following to H:\\Documents

I have been fiddling with the following command and although it works and traverses the whole nested folder structure, it is limited to files and folder without spaces . I assume I need to add a token delims statement, but I can't get the syntax right.

FOR /f %%a IN ('dir "H:\My Music" /b') DO MOVE H:\My Music\%%a H:\Documents\Music

FOR /f %%a IN ('dir "H:\My Pictures" /b') DO MOVE H:\My Pictures\%%a H:\Documents\Pictures

FOR /f %%a IN ('dir "H:\My Videos" /b') DO MOVE H:\My Videos\%%a H:\Documents\Videos

FOR /f %%a IN ('dir "H:" /b') DO MOVE H:\%%a H:\Documents

How do I deal with the spaces in the file and folder names?

I know I can achieve this with XCOPY, but space is an issue. More importantly time is extremely limited. I intend to get all users to run this script, during a 30 minute training session. With most users having several GB's of files XCOPY would take more time than is available during a 30 minute session.

You can simplifu your code like this :

@echo off&cls
set "$Drive=h:\"
set "$folder=music Pictures Videos"

for %%a in (%$folder%) do echo MOVE "%$DRIVE%my %%a" "%$drive%Documents\%%a"
pause

I put an echo before the move that you can test if tje output is ok for you

You need to put quotes around names that contain spaces.

Something like this should work:

FOR /f %%a IN ('dir "H:\My Music" /b') DO MOVE "H:\My Music\%%a" H:\Documents\Music

FOR /f %%a IN ('dir "H:\My Pictures" /b') DO MOVE "H:\My Pictures\%%a" H:\Documents\Pictures

FOR /f %%a IN ('dir "H:\My Videos" /b') DO MOVE "H:\My Videos\%%a" H:\Documents\Videos

FOR /f %%a IN ('dir "H:" /b') DO MOVE "H:\%%a" H:\Documents

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM