【发布时间】:2013-09-26 03:05:00
【问题描述】:
我正在尝试在 Windows 7 计算机上合并两个目录,这些目录具有一些相同的子文件夹(但不是全部)和一些相同的文件(但不是全部)。我要复制
例如,我有以下目录结构,并想组合(复制或移动)“Dir_A”和“Dir_Z”。如果文件存在于目标目录中,我想比较文件大小并保留两个文件中较大的一个。
Dir A
Dir B
file 1.txt
file 2.txt
file 3.txt
Dir C
file 4.txt
Dir D
file 5.txt
file 6.txt
Dir_Z
Dir Y
file 8.txt
Dir C
file 4.txt
Dir D
file 6.txt
我很喜欢使用 cmd 文件或 powershell。先感谢您。
编辑:修改以在导演和文件名中包含空格并添加我到目前为止的内容。
这是我目前能够做到的。除非目录或文件名中有空格,否则它似乎可以工作。
@echo off
echo :: Combine two folders, Keep the larger of any given files, Delete the other
if "%1"=="" goto :NoParam
if "%2"=="" goto :NoParam
setlocal enabledelayedexpansion
set SOURCE_DIR=%1
set DEST_DIR=%2
for /R "%SOURCE_DIR%" %%S in (*) do (
echo Source: "%%S"
@echo off
for /f "delims=" %%R in ('call MakeRelative.cmd "%%S" "%SOURCE_DIR%"') do (
set FILE_REL="%%R"
set filename=%DEST_DIR%\%%R
For %%D in ("!filename!") do (
Set Name=%%~nxD
if exist %DEST_DIR%\%%R (
echo File Exists
if %%~zD lss %%~zS (
echo Destination %%~zD is smaller than Source %%~zS
call robocopy %%~dpS %%~dpD "!Name!" /MOV
) else del %%S && Echo File is not larger, Deleting %%S
) else call robocopy %%~dpS %%~dpD "!Name!" /MOV
)
)
)
Pause
goto :eof
:NoParam
echo.
echo Syntax: %0 [Source_DIR] [Dest_DIR]
goto :eof
我使用 MakeRelative.cmd 如下所述:http://www.dostips.com/DtCodeCmdLib.php#MakeRelative
我很抱歉这不优雅。
【问题讨论】:
-
看看
FORFILES命令 -
cmd 只能比较小于
2^31(2147483648) 的数字。 -
你的意思是你的脚本除了文件名或路径中有空格时可以工作吗?
-
NYCdotNet - 是的。我澄清了我的原始帖子。谢谢。
-
在
"path\filename"和"filename"周围使用引号来处理空格。
标签: windows batch-file cmd compare