0

基本上,我们正在将大约 50 个桌面从 XP 迁移到 7。我无法安装任何额外的程序来完成这项任务。我正在编写一个脚本,将桌面、收藏夹和我的文档以及一些特定文件类型从原始计算机复制到用户的共享驱动器。稍后将能够将所有文件移动到他们正在获取的新机器上。我正在尝试通过 Windows 递归搜索并获取您将在脚本中看到的所有 .pst 文件和其他文件,并将它们备份到共享上的文件夹中(但不是在目录结构中,我只想要所有文件在一个目录,无论它们来自哪里),但我的文档除外。他们在“我的文档”中放置的任何内容都应在搜索中排除。无论如何,这就是我开始的:

@echo off
cls
set USRDIR=
set SHARE=
set /P USRDIR=Enter Local User Directory:  
set /p SHARE=Enter Shared Drive Name:  

set UPATH="c:\Documents and Settings\%USRDIR%"
set SPATH="g:\!MIGRATION"

set ESRI="%UPATH%\Application Data\ESRI"

net use g: /delete
net use g: \\server\%SHARE%
md %SPATH% %SPATH%\GIS %SPATH%\Outlook %SPATH%\Desktop %SPATH%\Documents %SPATH%\Favorites
if exists %ESRI% md %SPATH%\ESRI
md %SPATH%\misc %SPATH%\misc\GISfiles %SPATH%\misc\XMLfiles %SPATH%\misc\CSVfiles

for /R %%x in (*.mxd) do copy "%%x" "%SPATH%\GIS\"
for /R %%x in (*.dbf) do copy "%%x" "%SPATH%\misc\GISfiles\"
for /R %%x in (*.xml) do copy "%%x" "%SPATH%\misc\XMLfiles\"
for /R %%x in (*.csv) do copy "%%x" "%SPATH%\misc\CSVfiles\"
for /R %%x in (*.pst) do copy "%%x" "%SPATH%\Outlook\"
if exist %ESRI% xcopy /y /d /s /i /z %ESRI% %SPATH%\ESRI && echo ESRI YES || ESRI NO
xcopy /y /d /s /i /z "%UPATH%\Desktop" "%SPATH%\Desktop" && echo DESK YES || DESK NO
xcopy /y /d /s /i /z "%UPATH%\My Documents" "%SPATH%\Documents" && echo DOCS YES || DOCS NO
xcopy /y /d /s /i /z "%UPATH%\Favorites" "%SPATH%\Favorites" && echo FAVS YES || FAVS NO

echo "Script Complete!"
pause

然后我决定我想排除我的文档以防万一,这样我就不会得到一堆重复的东西,他们放在我的文档中的任何东西最终都会被复制两次。所以我把那个递归块改成这样:

for /R %%x in (*.mxd) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\GIS\"
for /R %%x in (*.dbf) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\misc\GISfiles\"
for /R %%x in (*.xml) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\misc\XMLfiles\"
for /R %%x in (*.csv) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\misc\CSVfiles\"
for /R %%x in (*.pst) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\Outlook\"

无论如何,我的问题是,这是最有效的方法吗?有没有更好的办法?我很好奇,因为我这里没有人可以提出想法或任何东西,我是这里唯一的现场支持。如果有人看到更好的方法或认为这是他们会这样做的方式,请告诉我。那里还有更多的文件类型,但我删除了其中的大部分,所以代码示例没有那么长,足以让大家明白这一点。

编辑:为了清楚起见,我不是这个组织的 IT 部门。我是该部门到一个单独的 IT 部门的技术支持联络员。我们实际的 IT 部门将此称为迁移,但它或多或少是一种简单的“让新机器停止工作而不做任何准备”之类的汤三明治操作。我无法以任何方式安装、删除或更改系统,我只能备份文件。

4

3 回答 3

1

试试微软的用户状态迁移工具——我用它在不到 30 天的时间内迁移了大约 400 个系统。脚本需要一些设置才能正常运行,但它确实做得非常好(并且没有要安装的程序)。

在默认模式下,它会获取所有 PST 文件、文档、桌面、收藏夹和大量其他文件夹/注册表设置。只要运行它的用户是本地管理员,它也会对计算机上的所有用户执行此操作(可能受上次登录日期或配置文件数量的限制)。

它可能会也可能不会满足您的需求,但在设计迁移时它是一个不错的选择。听起来你想要做的事情可以通过 USMT 完成,只需删除 ini 文件中的额外代码。

于 2013-10-23T18:58:43.867 回答
1

Usually, the best option is reduce processing in batch files to the minimum, leaving as much as possible to commands. But if you have to iterate over the file system several times, it is better to do only one pass and process in batch.

Adapted from a more general batch. I have made changes to adjust to what you need, but somethings more specific (your ESRI folder) are not added. Adapt as needed.

@echo off

    setlocal enableextensions enabledelayedexpansion

    rem Ask for share name
    set /P share=Enter Shared drive name:
    if "%share%"=="" (
        call :error "No share name provided"
        goto endProcess
    )

    rem Configure target of copy
    set target=g:

    rem Connect to target
    net use %target% /delete 
    net use %target% \\server\%share%

    if not exist %target% (
        call :error "No connection to server"
        goto endProcess
    )   

    rem Configure directory by extension
    set extensions=.mxd .dbf .xml .csv .pst
    set .mxd=GIS
    set .dbf=misc\GISFiles
    set .xml=misc\XMLFiles
    set .csv=misc\CSVFiles
    set .pst=Outlook

    rem adjust target of copy to user name
    set target=%target%\!MIGRATION\%username%
    if not exist "%target%" (
        mkdir "%target%"
    )

    rem Configure source of copy
    set source=%userprofile%
    if not exist "%source%" (
        call :error "User profile directory not found"
        goto endProcess
    )

    rem Resolve My Documents folder
    call :getShellFolder Personal
    set myDocPath=%shellFolder%

    if not exist "%myDocPath%" (
        call :error "My Documents directory not found"
        goto endProcess
    )

    rem Ensure target directories exists
    mkdir "%target%" > nul 2>nul
    for %%e in ( %extensions% ) do mkdir "%target%\!%%e!" >nul 2>nul

    rem We are going to filter file list using findstr. Generate temp file
    rem with strings, just to ensure final command line is in limits
    rem This will contain not desired folders/files or anything that will be
    rem copied later

    set filterFile="%temp%\filter.temp"
    (
        echo %myDocPath%
        echo \Temporary Internet Files\
        echo \Temp\
    ) > %filterFile%

    rem Process user profile, excluding My Documents, IE Temp and not needed extensions
    for /F "tokens=*" %%f in ('dir /s /b /a-d "%source%" ^| findstr /v /i /g:%filterFile% ^| findstr /i /e "%extensions%" ') do (
        call :processFile "%%~xf" "%%f"
    )

    rem Now, process "especial" folders

    mkdir "%target%\Documents"
    xcopy /y /d /s /i /z "%myDocPath%" "%target%\Documents"

    call :getShellFolder Desktop
    if exist "%shellFolder%" (
        mkdir "%target%\Desktop"
        xcopy /y /d /s /i /z "%shellFolder%" "%target%\Desktop"
        if errorlevel 1 (
            call :error "Failed to copy desktop files"
        )
    )

    call :getShellFolder Favorites
    if exist "%shellFolder%" (
        mkdir "%target%\Favorites"
        xcopy /y /d /s /i /z "%shellFolder%" "%target%\Favorites"
        if errorlevel 1 (
            call :error "Failed to copy favorites"
        )
    )

    rem Finish
    goto :endProcess

rem ** subroutines *******************************************

:processFile
    rem retrieve parameters 
    rem : %1 = file extension
    rem : %2 = file 
    set ext=%~1
    set file=%~2

    rem manage .something files
    if "%ext%"=="%file%" (
        set file=%ext%
        set ext=
    )

    rem manage no extension files
    if "%ext%"=="" (
        rem WILL NOT COPY
        goto :EOF
    )

    rem determine target directory based on file extension
    set extCmd=%%%ext%%%
    for /F "tokens=*" %%d in ('echo %extCmd%^|find /v "%%" ' ) do set folder=%%d

    if "%folder%"=="" (
        rem file extension not in copy list
        goto :EOF
    )

    copy /y /z "%file%" "%target%\%folder%" >nul 2>nul
    if errorlevel 1 (
        call :error "Failed to copy [%file%]"
    ) else (
        echo %file%
    )

    goto :EOF

:getShellFolder
    set _sf=%~1
    set shellFolder=
    if "%_sf%"=="" goto :EOF
    for /F "tokens=2,*" %%# in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "%_sf%" ^| find "%_sf%"') do (
        set shellFolder=%%$
    )
    goto :EOF


:error
    echo.
    echo ERROR : %~1
    echo.
    goto :EOF   


:endProcess

    endlocal
    exit /b
于 2013-10-24T09:17:54.820 回答
0

这会检查并排除任何以documents\(或包含它,因此也包含子目录)结尾的文件夹,因为 IIRC 该文件夹可能被称为\documents\\my documents\不区分大小写。

setlocal enabledelayedexpansion
for /R %%x in (*.mxd) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\GIS\"
for /R %%x in (*.dbf) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\misc\GISfiles\"
for /R %%x in (*.xml) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\misc\XMLfiles\"
for /R %%x in (*.csv) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\misc\CSVfiles\"
for /R %%x in (*.pst) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\Outlook\"
endlocal
于 2013-10-24T11:51:16.990 回答