本站动态:

copydir.bat: 拷贝目录及各级子目录下的文件到一个目录的工具

背景:工作中,UI工程师提供图片资源是分目录放置的,可是提交给CM的时候却需要在一个目录下的,如果手动拷贝非常麻烦,而如果直接用COPY 或 XCOPY命令把文件从A目录拷贝到B目录,B目录还是会维持A目录的目录结构。昨天晚上手动写了一根批处理工具,正是用来解决该问题的。具体用法代码中也有,这里不赘述。

copydir.bat代码如下:

  1. :: Auther: Shosh
  2. :: Site:  http://www.wscxy.com
  3. :: Date:  2010.03.13
  4. :: This bat copies all the files under a directory and all levels of subdirectories to another folder(no subfolder)
  5. @ECHO OFF
  6.  
  7. SET DefSrcRootDir=.
  8. SET DefDestDir=.
  9.  
  10. :FunBegin
  11. SET bConfirm=NO
  12. IF "%1" == "/?" GOTO FunUsage
  13.  
  14. SET bHaveOption=FALSE
  15. IF "%1" == "/c" SET bHaveOption=TRUE
  16. IF "%1" == "/C" SET bHaveOption=TRUE
  17. IF "%1" == "-c" SET bHaveOption=TRUE
  18. IF "%1" == "-C" SET bHaveOption=TRUE
  19.  
  20. IF bHaveOption == TRUE (
  21.         SET bConfirm=YES
  22.         IF "%2" == "" (
  23.                 SET srcRootDir=%DefSrcRootDir%
  24.                 SET destDir=%DefDestDir%
  25.         ) ELSE (
  26.                 IF NOT "%4" == "" GOTO FunErrorUsing
  27.                 SET srcRootDir=%2
  28.                 SET destDir=%3
  29.         )
  30. ) ELSE (
  31.         SET bConfirm=NO
  32.         IF "%1" == "" (
  33.                 SET srcRootDir=%DefSrcRootDir%
  34.                 SET destDir=%DefDestDir%
  35.         ) ELSE (
  36.                 IF NOT "%3" == "" GOTO FunErrorUsing
  37.                 SET srcRootDir=%1
  38.                 SET destDir=%2
  39.         )
  40. )
  41. IF "%destDir%" == "" GOTO FunErrorUsing
  42.  
  43. ::confirm settings
  44. ECHO ================================================================
  45. ECHO   Source Root Dir: %srcRootDir%
  46. ECHO          Dest Dir: %destDir%
  47. ECHO Overwrite Confirm: %bConfirm%
  48. ECHO ================================================================
  49. SET /P choice=Start executing? [Y/N] 
  50. if NOT %choice% == Y (
  51.         if NOT %choice% == y GOTO FunCancel
  52. )
  53. ::check input directories settings
  54. IF NOT EXIST %srcRootDir% ECHO [%srcRootDir%] not found!!!
  55. IF NOT EXIST %destDir% ECHO [%destDir%] not found, we will create it.
  56.  
  57. ::check source root dir before starting coping
  58. IF NOT EXIST %srcRootDir% GOTO FunPause
  59. IF %bConfirm% == YES (
  60. :: confirm overwriting
  61.         FOR /R %srcRootDir% %%i IN (.) DO XCOPY "%%i" "%destDir%" /-Y /V /F /I /K
  62. ) ELSE (
  63. :: no confirm
  64.         FOR /R %srcRootDir% %%i IN (.) DO XCOPY "%%i" "%destDir%" /Y /V /F /I /K
  65. )
  66.  
  67. ECHO ================================================================
  68. ECHO Finish!!! 
  69. ECHO Thank you for using copydir.bat written by Shosh!!!
  70. ECHO Welcome to visit my website http://www.wscxy.com
  71. GOTO FunPause
  72.  
  73. :FunErrorUsing
  74. ECHO !!! Error using copydir.bat !!!
  75. GOTO FunUsage
  76.  
  77. :FunUsage
  78. ECHO USAGE:    copydir [/C] [source root dir] [dest dir]
  79. ECHO           [/C] Confirm if a file needs overwriting. if not set, it will overwrite without confirm.
  80. ECHO If set both src root dir and dest dir to ., that means all the files in all levels of subfolders will be copied to the current folder.
  81. goto FunPause
  82.  
  83. :FunCancel
  84. ECHO You've canceled executing.
  85. GOTO FunPause
  86.  
  87. :FunPause
  88. ECHO ================================================================
  89. SET /P a=Press ENTER key to continue... 


[本日志由 shosh 于 2010-03-14 11:26 PM 编辑]
文章来自: Shosh原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 442
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭