目錄
- 一、Xcopy參數(shù)介紹
- 二、Xcopy命令實(shí)例介紹
- 1、本機(jī)復(fù)制文件或文件夾的實(shí)例
- 2、在局域網(wǎng)中的應(yīng)用實(shí)例
- 3、結(jié)合/d標(biāo)簽,實(shí)現(xiàn)只復(fù)制比目標(biāo)文件新的文件
- 4、移動(dòng)A目錄的所有文件到B目錄
- 5、xcopy 排除文件拷貝
- 6、xcopy與EXCLUDE參數(shù)的用法
XCOPY是COPY的擴(kuò)展,可以把指定的目錄連文件和目錄結(jié)構(gòu)一并拷貝,但不能拷貝系統(tǒng)文件;使用時(shí)源盤符、源目標(biāo)路徑名、源文件名至少指定一個(gè);選用/S時(shí)對源目錄下及其子目錄下的所有文件進(jìn)行COPY。除非指定/E參數(shù),否則/S不會拷貝空目錄,若不指定/S參數(shù),則XCOPY只拷貝源目錄本身的文件,而不涉及其下的子目錄;選用/V參數(shù)時(shí),對的拷貝的扇區(qū)都進(jìn)行較驗(yàn),但速度會降低。
我們不可數(shù)典忘祖,該學(xué)習(xí)的還是要學(xué)習(xí),不該忘記的還是不能忘記,尤其是DOS批處理命令,在開發(fā)軟件或項(xiàng)目的時(shí)候,都離不開這些基本的原始的命令。本文,我們來學(xué)習(xí)Xcopy的參數(shù)及實(shí)例
一、Xcopy參數(shù)介紹
命令格式:XCOPY source [destination] 一堆可選的參數(shù)
參數(shù)介紹
source 指定要復(fù)制的文件。
destination 指定新文件的位置和/或名稱。
/A 只復(fù)制有存檔屬性集的文件, 但不改變屬性。
/M 只復(fù)制有存檔屬性集的文件, 并關(guān)閉存檔屬性。
/D:m-d-y 復(fù)制在指定日期或指定日期以后改變的文件。如果沒有提供日期,只復(fù)制那些源時(shí)間比目標(biāo)時(shí)間新的文件。
/EXCLUDE:file1[+file2][+file3]...
指定含有字符串的文件列表。如果有任何字符串與要被復(fù)制的文件的絕對路徑相符,那個(gè)文件將不會得到復(fù)制。
例如,指定如 \obj\ 或 .obj 的字符串會排除目錄 obj 下面的所有文件或帶有 .obj 擴(kuò)展名的文件。
/P 創(chuàng)建每個(gè)目標(biāo)文件前提示。
/S 復(fù)制目錄和子目錄,除了空的。
/E 復(fù)制目錄和子目錄,包括空的。 與 /S /E 相同。可以用來修改 /T。
/V 驗(yàn)證每個(gè)新文件。
/W 提示您在復(fù)制前按鍵。
/C 即使有錯(cuò)誤,也繼續(xù)復(fù)制。
/I 如果目標(biāo)不存在,又在復(fù)制一個(gè)以上的文件, 則假定目標(biāo)一定是一個(gè)目錄。
/Q 復(fù)制時(shí)不顯示文件名。
/F 復(fù)制時(shí)顯示完整的源和目標(biāo)文件名。
/L 顯示要復(fù)制的文件。
/G 允許將沒有經(jīng)過加密的文件復(fù)制到不支持加密的目標(biāo)。
/H 也復(fù)制隱藏和系統(tǒng)文件。
/R 改寫只讀文件。
/T 創(chuàng)建目錄結(jié)構(gòu),但不復(fù)制文件。不包括空目錄或子目錄。/T /E 包括空目錄和子目錄。
/U 只復(fù)制已經(jīng)存在于目標(biāo)中的文件。
/K 復(fù)制屬性。一般的 Xcopy 會重設(shè)只讀屬性。
/N 用生成的短名復(fù)制。
/O 復(fù)制文件所有權(quán)和 ACL 信息。
/X 復(fù)制文件審核設(shè)置(隱含 /O)。
/Y 禁止提示以確認(rèn)改寫一個(gè)現(xiàn)存目標(biāo)文件。
/-Y 導(dǎo)致提示以確認(rèn)改寫一個(gè)現(xiàn)存目標(biāo)文件。
/Z 用重新啟動(dòng)模式復(fù)制網(wǎng)絡(luò)文件。
二、Xcopy命令實(shí)例介紹
1、本機(jī)復(fù)制文件或文件夾的實(shí)例
Xcopy d:\UpdateFiles e:\123 /s /e /y
命令解釋:將D盤的UpdateFiles文件夾中包含的所有東西,全部復(fù)制到E盤的123文件夾內(nèi);/s /e /y 參數(shù)說明:在復(fù)制文件的同時(shí)也復(fù)制空目錄或子目錄,如果目標(biāo)路徑已經(jīng)有相同文件了,使用覆蓋方式而不進(jìn)行提示。
2、在局域網(wǎng)中的應(yīng)用實(shí)例
Xcopy \\192.168.0.168\UpdateFiles e:\123 /s /e /y
命令解釋:將192.168.0.168這臺計(jì)算機(jī)的名稱為UpdateFiles的文件夾內(nèi)的所有東西,全部復(fù)制到本機(jī)的e:\123 文件夾;參數(shù)說明:在復(fù)制文件的同時(shí)也復(fù)制空目錄或子目錄,如果目標(biāo)路徑已經(jīng)有相同文件了,使用覆蓋方式而不進(jìn)行提示。
3、結(jié)合/d標(biāo)簽,實(shí)現(xiàn)只復(fù)制比目標(biāo)文件新的文件
假如現(xiàn)在c:\a文件夾下的所有文件和d:\a下的文件相同(修改日期及內(nèi)容)
c:\a下的一個(gè)文件被別人修改了或者添加了一個(gè)文件!
能否寫個(gè)bat,判斷一下c:\a文件夾下的每個(gè)文件的最后修改日期是不是在前兩天內(nèi)!如果是,則復(fù)制到d:\a下的相應(yīng)目錄(要是原來有,則覆蓋),如果不是,則不復(fù)制了.
@echo off
echo y|xcopy c:\a /d /e /r /k d:\a\
pause
經(jīng)測試如果目標(biāo)文件與源文件相同則不復(fù)制。確實(shí)一定程度了減少了對硬盤的讀寫。
4、移動(dòng)A目錄的所有文件到B目錄
移動(dòng)A目錄的所有文件到B目錄,且保留A目錄的目錄結(jié)構(gòu);如果A下面有子目錄,則在B中也創(chuàng)建相同目錄,并把對應(yīng)目錄的文件移動(dòng)到B的相同目錄結(jié)構(gòu)位置;(不移動(dòng)系統(tǒng)文件和隱藏文件)
@echo off
rem 復(fù)制指定目錄的目錄結(jié)構(gòu)到目標(biāo)目錄,并移動(dòng)的文件到目標(biāo)目錄,且保留目錄結(jié)構(gòu)(空目錄也復(fù)制),不保留系統(tǒng)文件和隱藏文件
set ObjPath=\\192.168.1.186\Temp Files\技術(shù)部\BPD\sourceFiles\ZOFUND
set DestPath=E:\EastFax\FFPsourceFiles\sourceFiles\ZOFUND
set DestExt=*
xcopy /e /c /y "%ObjPath%\%DestExt%" "%DestPath%"
del /a /s /q /f "%ObjPath%\"
5、xcopy 排除文件拷貝
@echo off
::xcopy /y /s /e /I /exclude:uncopy.txt E:\NetWorkCMDA\CMDA0418\Portals\0 E:\Mu\Portals\0
::xcopy /y /s /e /I /exclude:uncopy.txt C:\Docume~1\wangchao\桌面\MyTest C:\Docume~1\wangchao\桌面\aaa
echo 返回默認(rèn)情況下應(yīng)用程序存儲數(shù)據(jù)的位置=%APPDATA%
echo 返回當(dāng)前目錄字符串=%CD%
echo 返回計(jì)算機(jī)的名稱=%COMPUTERNAME%
echo 返回命令行解釋器可執(zhí)行程序的準(zhǔn)確路徑=%COMSPEC%
echo 返回當(dāng)前登錄的用戶的名稱=%USERNAME%
echo 返回包含用戶帳戶的域的名稱=%USERDOMAIN%
echo 返回當(dāng)前用戶的配置文件的位置=%USERPROFILE%
set myExist=%windir%\TestUncopy
set myCommand=%windir%\TestUncopyecho TestUncopy
if not exist %myExist% mkdir %myCommand%
set myExist1=%windir%\TestUncopy\a
set myCommand1=%windir%\TestUncopy\aecho TestUncopy\a
if not exist %myExist1% mkdir %myCommand1%
set myExist2=%windir%\TestUncopy\aa
set myCommand2=%windir%\TestUncopy\aaecho TestUncopy\aa
if not exist %myExist2% mkdir %myCommand2%
set myExist3=%windir%\TestUncopy\a\b
set myCommand3=%windir%\TestUncopy\a\becho TestUncopy\a\b
if not exist %myExist3% mkdir %myCommand3%
set myExist4=%windir%\TestUncopySource
set myCommand4=%windir%\TestUncopySourceecho TestUncopySource
if not exist %myExist4% mkdir %myCommand4%
set myExist5=%windir%\TestUncopy\aaa
set myCommand5=%windir%\TestUncopy\aaaecho TestUncopy\aaa
if not exist %myExist5% mkdir %myCommand5%
set copyFile=%windir%\system\COMMDLG.DLL
if not exist %myExist1%\m1.dll copy %copyFile% %myExist1% /y
if not exist %myExist2%\m2.dll copy %copyFile% %myExist2% /y
if not exist %myExist3%\m3.dll copy %copyFile% %myExist3% /y
if not exist %myExist5%\m5.dll copy %copyFile% %myExist5% /y
if not exist %myExist1%\m1.dll ren %myExist1%\COMMDLG.DLL m1.dll
if not exist %myExist2%\m2.dll ren %myExist2%\COMMDLG.DLL m2.dll
if not exist %myExist3%\m3.dll ren %myExist3%\COMMDLG.DLL m3.dll
if not exist %myExist5%\m5.dll ren %myExist5%\COMMDLG.DLL m5.dll
if not exist %myExist5%\txt1.exe copy %windir%\system32\notepad.exe %myExist5% /y
if not exist %myExist5%\txt1.exe ren %myExist5%\notepad.exe txt1.exe
if not exist %myExist5%\txt2.exe copy %windir%\system32\notepad.exe %myExist5% /y
if not exist %myExist5%\txt2.exe ren %myExist5%\notepad.exe txt2.exe
if not exist %myExist5%\txt3.exe copy %windir%\system32\notepad.exe %myExist5% /y
if not exist %myExist5%\txt3.exe ren %myExist5%\notepad.exe txt3.exe
set OutFile=uncopy.txt
::MyDir=\aa\ 排除的文件夾
::MySubDir=\a\b\ 排除的子文件夾
::MyFile=m3.dll 排除的文件
::set MyFile1=.exe 排除的類型文件
set MyDir=\aa\
set MySubDir=\a\b\
set MyFile=m3.dll
set MyFile1=.exe
echo %MyDir% >uncopy.txt
echo %MySubDir% >>uncopy.txt
echo %MyFile% >>uncopy.txt
echo %MyFile1% >>uncopy.txt
xcopy /y /s /e /I /exclude:uncopy.txt %windir%\TestUncopy %windir%\TestUncopySource
::if exist %windir%\TestUncopy del %windir%\TestUncopy /s /qrd %windir%\TestUncopy /s /q echo del_rd %windir%\TestUncopy
::if exist %windir%\TestUncopySource del %windir%\TestUncopySource /s /qrd %windir%\TestUncopySource /s /q echo del_rd %windir%\TestUncopySource
::del uncopy.txt
start %myExist%
start %myExist4%
echo 是否刪除(Y/N)
set /p shuru=
::if %shuru:~0,1%==y (echo %shuru:~0,1%goto del)
::if %shuru:~0,1%==Y (echo %shuru:~0,1%goto del)
::if %shuru:~0,1%==n (echo %shuru:~0,1%goto End)
::if %shuru:~0,1%==N (echo %shuru:~0,1%goto End)
if %shuru:~0,1%==y (clsgoto del)
if %shuru:~0,1%==Y (clsgoto del)
if %shuru:~0,1%==n (clsgoto End)
if %shuru:~0,1%==N (clsgoto End)
pause
:del
if exist %windir%\TestUncopy del %windir%\TestUncopy /s /qrd %windir%\TestUncopy /s /q echo del_rd %windir%\TestUncopy
if exist %windir%\TestUncopySource del %windir%\TestUncopySource /s /qrd %windir%\TestUncopySource /s /q echo del_rd %windir%\TestUncopySource
if exist uncopy.txt del uncopy.txt
:End
echo. ::換行
echo 操作已經(jīng)完成
timeout /t 3 /nobreak
cls
echo 是否重新調(diào)用(Y/N)--Y:調(diào)用 N:退出
set /p shuru=
if %shuru:~0,1%==y (clsgoto MyCall)
if %shuru:~0,1%==Y (clsgoto MyCall)
if %shuru:~0,1%==n (clsgoto EndExit)
if %shuru:~0,1%==N (clsgoto EndExit)
:EndExit
exit
:MyCall
uncopy.bat
pause
6、xcopy與EXCLUDE參數(shù)的用法
示例一:例如,我要將"WebAppBuytoy"目錄備份一份到"WebAppBuytoy_bak",但要排除其下面的
"WebAppBuytoy\product\ProdImages"子目錄,因?yàn)樗笄覜]有變化沒有必要備份.
步驟一,新建一個(gè)文本文件叫"EXCLUDE.txt",內(nèi)容如下:
product\ProdImages
步驟二,命令(包含子目錄,不提示覆蓋):
xcopy WebAppBuytoy WebAppBuytoy_bak /e /EXCLUDE:EXCLUDE.txt /y
示例二: 發(fā)布.net的Web項(xiàng)目批處理
deploy_web.cmd
xcopy G:\WebSites\BuytoySolutionV2\WebAppBuytoy web /EXCLUDE:web_exclude.txt /i /e /y
@rem 將排除的*.css文件補(bǔ)回
xcopy G:\WebSites\BuytoySolutionV2\WebAppBuytoy\App_Themes web\App_Themes /i /e /y
@pause
web_exclude.txt:
.cs
.vb
.pdb
.csproj
.csproj.user
.Publish.xml
.___
.cmd
.rar
\App_Themes\Theme1
\App_Themes\Theme1\
\obj
\obj\
\Properties
\Properties\
\App_Data\
\App_Classes\
示例三: 發(fā)布.net的Web項(xiàng)目批處理,但排除商品圖片目錄,因?yàn)樗笄也怀8拢?/strong>
重復(fù)使用上面那個(gè)排除文件“web_exclude.txt”,再加一個(gè)排除商品圖片的排除文件
“web_exclude_prodImages.txt”,這樣要在一個(gè)命令中使用多個(gè)排除文件列表,用到加號“+”:
deploy_web_排除商品圖片.cmd
xcopy G:\WebSites\BuytoySolutionV2\WebAppBuytoy web
/EXCLUDE:web_exclude.txt+web_exclude_prodImages.txt /i /e /y
@rem 將排除的*.css文件補(bǔ)回
xcopy G:\WebSites\BuytoySolutionV2\WebAppBuytoy\App_Themes web\App_Themes /i /e /y
@pause
web_exclude_prodImages.txt
\product\ProdImages
\product\ProdImages\
到此這篇關(guān)于Xcopy命令參數(shù)使用介紹的文章就介紹到這了,更多相關(guān)Xcopy內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- XCOPY只拷貝當(dāng)天文件的實(shí)現(xiàn)代碼
- XCOPY的高級使用:指定日期并且用EXCLUDE排除指定文件
- bat+xcopy實(shí)現(xiàn)只復(fù)制比目標(biāo)文件更新的文件
- 利用xcopy命令實(shí)現(xiàn)本地文件復(fù)制到遠(yuǎn)程服務(wù)器的方法
- xcopy 實(shí)現(xiàn)批處理拷貝文件或文件夾
- 談?wù)剎copy中的排除copy
- Xcopy 復(fù)制文件和目錄,包括子目錄。
- 批處理中Copy與Xcopy命令的區(qū)別小結(jié)