Robocopy to pull directory from txt file

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Robocopy to pull directory from txt file

Post by MigrationUser »

11 Nov 2007 03:22
legoserver


Good evening,
I have a rather odd issue that i am working through, all be it slowly. I have gotten it down to where i have a path for example C:\documents and settings\

and i have a txt file with some user accounts
doejohn
suttallen

and i need to find a way to script it in the batch so that it copys

c:\documents and settings\doejohn\my documents\

and if there is more than one name in the txt file it will pull the next name and copy that?....

any ideas would be great... THANKS

----------------------------

#2 11 Nov 2007 06:12
daat99


The following batch file loop for "list.txt" file that contains a list of user names separated by ";".
It than deletes all the user profiles that aren't in the "list.txt":

Code: Select all

@echo off
setlocal
set VAR="before"
if %VAR%=="before" (
    set VAR=after
    if not "!VAR!"=="after" (
        echo Restarting '%~f0' using 'Command Extensions'
        set _first=cmd.exe
        call cmd.exe '%~f0' /V /C "%~f0"
        pause
        goto END
    )
)
if not exist "%~dp0list.txt" (
    echo Unable to locate list.txt in the batch file directory, please make sure you do not use a network path.
    echo You can try mapping the network drive to a local drive letter or folder.
    echo Exiting operation without performing cleanup, have a nice day...
    pause
    exit
)
set "log=c:\cleanup.log"
set "dir=c:\Documents and Settings"
if EXIST %log% echo %date% - %time% >> %log%
set problems="false"
@for /d %%i in ("%dir%\*") do (
    set delete="true"
    for /F "delims=;" %%G in ('type "%~dp0list.txt"') do (
        IF /I "%dir%\%%G"=="%%i" set delete="false"
        IF /I "%%i"=="%dir%\%USERNAME%" set delete="false"
    )
    if !delete!=="true" (
        echo Deleting..."%%i"
        if EXIST "%%i\ntuser.dat" (
            rem echo file "%%i\ntuser.dat"
        )
        rd "%%i" /q /s 2>> %log%
        if EXIST %%i (
            set problems="true"
            echo UNABLE TO DELETE: "%%i"
            echo.
        )
    )
)
:END
if !problems!=="true" (
    echo.
    echo Profile cleanup has encountered some minor problems, please restart the computer and try to run it again.
    echo If the problems persist please log in using "safe mode" and delete the problematic file/folder manually.
    echo for more information please refer to %log%
) else (
    echo Profile cleanup was completed without critical errors, have a nice day.
)
You should change the following code:

Code: Select all

@for /d %%i in ("%dir%\*") do (
    set delete="true"
    for /F "delims=;" %%G in ('type "%~dp0list.txt"') do (
        IF /I "%dir%\%%G"=="%%i" set delete="false"
        IF /I "%%i"=="%dir%\%USERNAME%" set delete="false"
    )
    if !delete!=="true" (
        echo Deleting..."%%i"
        if EXIST "%%i\ntuser.dat" (
            rem echo file "%%i\ntuser.dat"
        )
        rd "%%i" /q /s 2>> %log%
        if EXIST %%i (
            set problems="true"
            echo UNABLE TO DELETE: "%%i"
            echo.
        )
    )
)
In a similar way to this (untested):

Code: Select all

mkdir c:\backup
for /F "delims=;" %%G in ('type "%~dp0list.txt"') do (
    if EXIST "%dir%\%%G" (
        echo Backing up %%G
        mkdir "c:\backup\%%G"
        xcopy "%dir%\%%G\My Documents" "c:\backup\%%G\My Documents" /t /e /c /i /r /y
    )
)
I always try to help wink
Sometimes I don't know how sad

----------------------------

#3 12 Nov 2007 00:32
legoserver


Thank you very much for your help

The problem is the names are on seperate lines, i ended up having to do something similar to this

Code: Select all

set /P user=<C:\temp\Exits\lptusrs.txt
set usrchk=%user%

robocopy "C:\documents and settings\%user%\my documents" "E:\LaptopDocs\%user%\My Documents" /copy:DAT /Z /E /TEE /NP /X /v /FP /ETA /IS /XD /XF *.nsf *.mp3 *.mp4 *.m4v 

*.m4a *.pos *.cab *.bmp *.jpg *.dll *.exe *.msi *.gif *.png *.lnk *.swf *.flv *.bup *.ifo *.vob *.url *.ini *.wmdb *.mst *.eps *.mgc *.ntf *.box *.sys *.mpg *.avi *.wma 

*.wmv *.iso /log+:C:\temp\Exits\Logs\LPTLog.txt

type C:\temp\Exits\lptusrs.txt | find /v /i "%user%" > C:\temp\Exits\lptusrs2.txt

type C:\temp\Exits\lptusrs2.txt >C:\temp\Exits\lptusrs.txt
set /P user=<C:\temp\Exits\lptusrs.txt

if not "%user%"=="%usrchk%" goto sblptusrcpy

if "%user%"=="%usrchk%" goto done
its not the cleanest thing in the world but it does what is needed it take a list of user accounts previously created and systematically removes them one at a time until they are all copyd.....

Thanks again,
Nick C

----------------------------

#4 15 Nov 2007 20:12
avery_larry


Not tested--but I think you want to change this line:

Code: Select all

for /F "delims=;" %%G in ('type "%~dp0list.txt"') do (
to:

Code: Select all

for /f %%a in (list.txt) do (
which then runs the DO portion for each LINE in the list.txt (instead of colon separated).

If the lines contain spaces, then you need:

Code: Select all

for /f "tokens=*" %%a in (list.txt) do (
Ted

----------------------------

#5 16 Nov 2007 01:19
daat99


Personally I prefer to ignore white spaces and separate the names by the colon, but that's just me
Post Reply