0

I have found a manual way of doing this, where if you go to your default user folders (3D Objects, Desktop, Downloads, Documents, Favorites, Pictures, and Videos), and right click then properties, then Location, you can then add a OneDrive folder location, which then changes the path that Windows sets.

What I am trying to do is get a batch file that one by one changes the user folders to pre-made folders in OneDrive, which will also copy the user files and folders to the new destination automatically.

Since this process seems like it can only be ran one folder at a time, maybe multiple batch files are needed for this to work, which I am fine with.

So to clarify here are the steps that I am looking for:

  1. Change the user default user folders location to a predefined folder in OneDrive. Here are the exact paths of this change:
%USERPROFILE%\3D Objects> %USERPROFILE%\OneDrive - Olivet Nazarene University\LocalPC\3D Objects

%USERPROFILE%\Contacts"> %USERPROFILE%\OneDrive - Olivet Nazarene University\LocalPC\Contacts

%USERPROFILE%\Desktop"> %USERPROFILE%\OneDrive - Olivet Nazarene University\LocalPC\Desktop

%USERPROFILE%\Documents"> %USERPROFILE%\OneDrive - Olivet Nazarene University\LocalPC\Documents

%USERPROFILE%\Downloads > %USERPROFILE%\OneDrive - Olivet Nazarene University\LocalPC\Downloads

%USERPROFILE%\Favorites" > %USERPROFILE%\OneDrive - Olivet Nazarene University\LocalPC\Favorites

%USERPROFILE%\Links"> %USERPROFILE%\OneDrive - Olivet Nazarene University\LocalPC\Links

%USERPROFILE%\Music"> %USERPROFILE%\OneDrive - Olivet Nazarene University\LocalPC\Music

%USERPROFILE%\Pictures"> %USERPROFILE%\OneDrive - Olivet Nazarene University\LocalPC\Pictures

%USERPROFILE%\Videos"> %USERPROFILE%\OneDrive - Olivet Nazarene University\LocalPC\Videos

  1. If this is done manually, this will prompt if you want to move the files and folder to the new destination, I want this to automatically say yes and move the files to the new OneDrive location. If this process is hard to do execute due to the file move, it can be separated into different batch files.

  2. If this batch file is ran and this whole setup has already been accomplished, it will check the default OneDrive user folder location and if the user already has the user folder set as folders in OneDrive a message will inform them of this.

  3. If any file or folder move does not copy successful, a log of the file and or folder and its path will display in a log file will be generated and be saved in %USERPROFILE%\OneDrive - Olivet Nazarene University\LocalPC\

  4. If the process is successful, a successful message is displayed at the end of the whole procedure in CMD.

Io-oI
  • 9,237

2 Answers2

1

I work with @SSAdamT and this was the solution we came up with. Let us know if there is some sort of simpler way to accomplish the same goal.

@ECHO OFF
set list=Desktop Documents Downloads Favorites Music Pictures Videos
set baseLocation="%USERPROFILE%\OneDrive - Olivet Nazarene University\LocalPC\"
set "Key=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
set "Typ=REG_EXPAND_SZ"
set RegList="Desktop" "Personal" "{374DE290-123F-4565-9164-39C4925E467B}" "Favorites" "My Music" "My Pictures" "My Video"
set /a c=0
setLocal enableDelayedExpansion
for %%j in (%RegList%) do (
    set RegList[!c!]=%%j & set /a c=c+1
)
for %%i in (%list%) do (
    if not exist %baseLocation%%%i (
        mkdir %baseLocation%%%i
    ) else (
        echo %%i already exsists
    )
)
set baseLocation=%baseLocation:"=%
for %%i in (%list%) do (
    RoboCopy.exe "%USERPROFILE%\%%~i\." "%baseLocation%\%%~i\." *.* /MOV /FP /NP /IS /Z /E /NFL /NDL /NJH
)
set /a d=0
for %%k in (%list%) do (
    call set val=%%RegList[!d!]%% & Reg Add "%Key%" /f /V !val! /T %Typ% /D "%baseLocation%%%k" & set /a d=d+1
)  
echo DONE!
pause

Hope this helps someone if they need an automated way to move user folders. Just change the "baseLocation" variable to where you want the folders.

Edit: Forgot to thank @It_Wasn't_Me for the RoboCopy command.

0

You can try to use a simple for loop with RoboCopy:

  • In command-line
for %i in ("3D Objects",Contacts,Desktop,Documents,Favorites,Links,Music)do RoboCopy.exe "%USERPROFILE%\%~i\." "%OneDrive%\%~i\." *.* /MOV /FP /NP /IS /Z /E /L
  • In bat/cmd file:
@echo off

for %%i in ("3D Objects",Contacts,Desktop,Documents,Favorites,Links,Music)do RoboCopy.exe "%USERPROFILE%%%~i." "%OneDrive%%%~i." . /MOV /FP /NP /IS /Z /E /L


Obs.: 1 The use of /L serves for a preview of the executions of RoboCopy, to effectively execute the copy, remove /L.

enter image description here

  • In command-line
for %i in ("3D Objects",Contacts,Desktop,Documents,Favorites,Links,Music)do RoboCopy.exe "%USERPROFILE%\%~i\." "%OneDrive%\%~i\." *.* /MOV /FP /NP /IS /Z /E /L
  • In bat/cmd file:
@echo off

for %%i in ("3D Objects",Contacts,Desktop,Documents,Favorites,Links,Music)do RoboCopy.exe "%USERPROFILE%%~i." "%OneDrive%%%~i." . /MOV /FP /NP /IS /Z /E /L

Obs.: 2 You can add/remove the folders you need in this ("loop itens"), taking care to use double-quotes in the names where you have spaces: "3D Objects"

  • Some further reading:

[√] For

[√] For /f

[√] RoboCopy

[√] RoboCopy.doc (|Google/doc|)

[√] How to do a batch move and keep folder structure?

Io-oI
  • 9,237