Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for robomove.bat

(view source code of robomove.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Enable delayed variable expansion
  6. SETLOCAL ENABLEDELAYEDEXPANSION
  7.  
  8. :: Check command line arguments
  9. ECHO.%* | FINDSTR /R /C:"[?\*]" >NUL && GOTO Syntax
  10. IF     "%~2"=="" GOTO Syntax
  11. IF NOT "%~3"=="" IF /I NOT "%~3"=="/ROBOCOPY" GOTO Syntax
  12. IF NOT EXIST "%~f1.\" GOTO Syntax
  13. IF NOT EXIST "%~f2.\" GOTO Syntax
  14.  
  15. :: Initialize variables
  16. SET Files1=
  17. SET Files2=
  18. SET Temp1=
  19. SET Temp2=
  20. SET Title=
  21.  
  22. :: Save the current title if possible
  23. FOR /F "tokens=*" %%A IN ('GetTitle2.exe /NC /NP 2^>NUL') DO SET Title=%%A
  24.  
  25. :: Navigate to the first folder, giving it a drive letter on the fly in case of a UNC type path
  26. PUSHD "%~f1" >NUL 2>&1 || GOTO Syntax
  27. FOR %%A IN ("%CD%") DO SET Files1="%%~dpA*.*"
  28. POPD
  29.  
  30. :: Navigate to the second folder, giving it a drive letter on the fly in case of a UNC type path
  31. PUSHD "%~f2" >NUL 2>&1 || GOTO Syntax
  32. FOR %%A IN ("%CD%") DO SET Files2="%%~dpA*.*"
  33. POPD
  34.  
  35. :: Navigate to the TEMP folder
  36. PUSHD "%Temp%" >NUL 2>&1 || GOTO Syntax
  37. FOR %%A IN ("%CD%") DO (
  38. 	SET Temp1="%%~dpA%~n0.tmp"
  39. 	SET Temp2="%%~dpA%~n0.tmp"
  40. )
  41. POPD
  42.  
  43. :: Create 2 lists of files to be uodated
  44. XCOPY %Files1% %Files2% /S /D /H /R /Y /L | FIND "\" > %Temp1%
  45. XCOPY %Files2% %Files1% /S /D /H /R /Y /L | FIND "\" > %Temp2%
  46.  
  47. :: Replicate the directory structure of source on target
  48. XCOPY %Files1% %Files2% /T /E /Y >NUL 2>&1
  49.  
  50. :: Count the number of files moved and directories created
  51. SET FilesMoved=0
  52.  
  53. :: For each file in the first list ...
  54. FOR /F "tokens=*" %%A IN ('TYPE %Temp1%') DO (
  55. 	TITLE %%A
  56. 	REM :: ... check for duplicates on either drive
  57. 	SET Error=0
  58. 	TYPE FIND /C /I "%%~nxA" %Temp1% | FINDSTR /R /X /C:"1" >NUL || SET Error=1
  59. 	TYPE FIND /C /I "%%~nxA" %Temp2% | FINDSTR /R /X /C:"1" >NUL || SET Error=1 
  60. 	REM :: If no duplicates were found ...
  61. 	IF !Error! EQU 0 (
  62. 		REM ... find the location of the file to be moved ...
  63. 		FOR /F "tokens=*" %%B IN ('FINDSTR /R /E /I /C:"\\%%~nxA" %Temp2%') DO (
  64. 			REM :: ... move the file to its new location
  65. 			SET /P "=%%~fB -> %%~dB%%~pA    	" < NUL
  66. 			MOVE "%%~fB" "%%~dB%%~pA"
  67. 			SET /A FilesMoved += 1
  68. 		)
  69. 	) ELSE (
  70. 		ECHO Skipped "%%~A" ^(duplicates or not found^)
  71. 	)
  72. )
  73.  
  74. IF %FilesMoved% EQU 1 (
  75. 	ECHO Moved 1 file.
  76. 	TITLE Moved 1 file.
  77. ) ELSE (
  78. 	ECHO Moved %FilesMoved% files.
  79. 	TITLE Moved %FilesMoved% files.
  80. )
  81.  
  82. :: Run ROBOCOPY if specified
  83. IF /I "%~3"=="/ROBOCOPY" (
  84. 	SET Options=
  85. 	SET Counter=0
  86. 	FOR %%A IN (%*) DO (
  87. 		SET /A Counter += 1
  88. 		IF !Counter! GTR 3 (
  89. 			IF /I NOT "%%~A"=="/MIR" (
  90. 				SET Options=!Options! %%A
  91. 			)
  92. 		)
  93. 	)
  94. 	ROBOCOPY "%~f1" "%~f2" /MIR !Options!
  95. )
  96.  
  97. :Clean up
  98. IF DEFINED Title TITLE %Title%
  99. DEL %Temp1% >NUL 2>&1
  100. DEL %Temp2% >NUL 2>&1
  101. ENDLOCAL
  102. GOTO:EOF
  103.  
  104.  
  105. :Syntax
  106. ECHO.
  107. ECHO RoboMove.bat,  Version 0.30 beta
  108. ECHO Move files in the target folders to natch their location in the source folder.
  109. ECHO If run before RoboCopy, the latter won't have to delete moved files and then
  110. ECHO copy them again to their new location (most useful on network drives).
  111. ECHO.
  112. ECHO Usage:  ROBOMOVE  source_folder  target_folder
  113. ECHO         ROBOCOPY  source_folder  target_folder  /MIR  [ robocopy_options ]
  114. ECHO.
  115. ECHO or:     ROBOMOVE  source_folder  target_folder  /ROBOCOPY  [ robocopy_options ]
  116. ECHO.
  117. ECHO Where:  source_folder     the source folder, used as a template for the target
  118. ECHO         target_folder     the target folder, where the files will be moved to
  119. ECHO                           match the source folder as closely as possible
  120. ECHO         /ROBOCOPY         run ROBOCOPY /MIR on the specified folders afterwards
  121. ECHO         robocopy_options  are the switches passed on to ROBOCOPY
  122. ECHO.
  123. ECHO Notes:  Folders MUST be specified as fully qualified paths, either in
  124. ECHO         drive:\path or UNC format; do not specify files.
  125. ECHO.
  126. ECHO Written by Rob van der Woude
  127. ECHO http://www.robvanderwoude.com
  128.  
  129. IF "%OS%"=="Windows_NT" ENDLOCAL
  130. IF "%OS%"=="Windows_NT" EXIT /B 1
  131.  

page last modified: 2024-04-16; loaded in 0.0057 seconds