Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 批量排除递归副本中的文件夹_Batch File_Recursion_Copy_Xcopy - Fatal编程技术网

Batch file 批量排除递归副本中的文件夹

Batch file 批量排除递归副本中的文件夹,batch-file,recursion,copy,xcopy,Batch File,Recursion,Copy,Xcopy,基本上,我们正在将大约50台台式机从XP迁移到7。我无法安装任何额外的程序来完成此任务。我所做的是编写一个脚本,将桌面、收藏夹和我的文档以及一些特定的文件类型从原始计算机复制到用户的共享驱动器。之后,它将能够将所有文件移动到新机器上。我正在尝试递归搜索Windows,获取脚本中的所有.pst文件和其他文件,并将它们备份到共享上的文件夹(但不是目录结构,我只希望所有文件都位于一个目录中,无论它们来自何处),但我的文档除外。他们放在我文件里的任何东西都应该被排除在搜索范围之外。不管怎么说,这就是我开

基本上,我们正在将大约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部门称之为迁移,但这或多或少是一种简单的“让新机器在不做任何准备的情况下停下来”的汤三明治操作。我无法以任何方式安装、删除或更改系统,我只能备份文件。

从Microsoft尝试-我使用它在不到30天的时间内移动了400个系统。脚本需要一些设置才能正常运行,但它做得非常好(而且没有要安装的程序)

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


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

通常,最好的选择是将批处理文件中的处理减少到最低限度,尽可能多地使用命令。但是,如果必须对文件系统进行多次迭代,最好只进行一次遍历,然后进行批处理

改编自更通用的批次。我已根据您的需要进行了更改,但没有添加更具体的内容(您的ESRI文件夹)。根据需要进行调整

@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

这将检查并排除以
文档\
结尾的任何文件夹(或包括它,也包括子目录),因为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

我不能。IT部门到处都是白痴,他们实际上并没有迁移任何东西。他们希望最终用户自己备份所有内容。这就是为什么我要这么做,试图让这个过程对组织中不那么精明的人来说不那么痛苦。我无法控制电脑,除了复制文件,我什么也做不了。或者你可以让他们更改,然后不停地抱怨,直到他们修复。我在一个IT部门工作——如果他们不迁移至少一些基本的东西,他们就没有做好自己的工作。这就是说,这只是一个你运行的程序(没有启动盘,可以只为当前用户运行而没有管理员权限)。值得一看。大约8年前,我为一家大型汽车制造商做了这件事,但没有管理员权限,效果非常好。哈哈。是的,这些新机器有UAC,我不再是本地管理员了。因此,每一个需要对计算机进行的小改动现在都必须通过它。我计划向帮助台发送大量请求,直到他们发现需要让我拥有一个管理员帐户来处理Win7机器。这些小丑真的是另一回事,它是我在政府部门遇到的最无能的it部门。我能说的最简单的方法是创建一个批处理文件,将其主文件夹(文档、桌面…)中的所有内容移动到网络共享。我会向您的管理层提出这一点,因为这会限制您选择他们不喜欢的选项(使用大量NAS存储,具体取决于文件大小或网络利用率)。同样,如果IT人员不移动您需要的东西,他们也会很糟糕。我讨厌手动迁移,但我仍然会手动移动他们需要的任何东西,这样他们就可以完成他们的工作。如果您查看脚本,您将看到我的文档、收藏夹和桌面都已备份。问题是,如果他们在计算机上有两次文件(比如说在正常位置之外的随机文件夹),我会捕捉到它们,但没有重复的。我想确保在搜索计算机的其余部分以备份未被用户移动到文档的散乱者时忽略“我的文档”文件夹。如果没有人回答,我可以在几个小时内提供帮助。那太好了,半小时后我就离开了,但是早上我会用这个,到时候我就能测试任何建议并给出答案。事实上,这个看起来很不错。可惜的是,每种文件类型都需要一个不同的目标文件夹,或者你可以一次完成所有任务。这看起来很合理,但有一个例外,就是查找失败