使用Azure Data Factory按LastModifiedDate增量复制修改后的文件
仅复制已变更/最后修改文件到目标共享的实现方案
Windows 平台
方法1:PowerShell 脚本
适合精准筛选最后修改文件,支持网络共享路径:
# 替换为你的增量文件所在路径 $sourcePath = "C:\Your\Incremental\Files" # 替换为目标共享地址 $targetPath = "\\Server\Share\Destination" # 获取目录下所有文件,按最后修改时间降序排序,取最新的1个 $latestFiles = Get-ChildItem -Path $sourcePath -File | Sort-Object LastWriteTime -Descending | Select-Object -First 1 # 复制到目标共享 foreach ($file in $latestFiles) { Copy-Item -Path $file.FullName -Destination $targetPath -Force Write-Host "已复制文件: $($file.Name)" }
如果要筛选所有比目标目录中对应文件更新的文件,用这个:
$sourcePath = "C:\Your\Incremental\Files" $targetPath = "\\Server\Share\Destination" # 对比源和目标文件的最后修改时间,仅复制源文件更新的 Get-ChildItem -Path $sourcePath -File | ForEach-Object { $targetFile = Join-Path -Path $targetPath -ChildPath $_.Name if (-not (Test-Path $targetFile) -or $_.LastWriteTime -gt (Get-Item $targetFile).LastWriteTime) { Copy-Item -Path $_.FullName -Destination $targetPath -Force Write-Host "已复制更新文件: $($_.Name)" } }
方法2:批处理脚本
适合简单场景,按最后修改时间筛选:
@echo off set "source=C:\Your\Incremental\Files" set "target=\\Server\Share\Destination" :: 按最后修改时间降序排序,取最新的1个文件复制 for /f "delims=" %%f in ('dir /b /o-d /a-d "%source%"') do ( copy "%%f" "%target%" /y echo 已复制文件: %%f goto :end ) :end
Linux/macOS 平台
Shell 脚本
筛选最后修改文件或对比目标目录的变更文件:
#!/bin/bash source_path="/your/incremental/files" # 若为SMB共享需先挂载,比如 mount -t cifs //server/share /mnt/share target_path="/mnt/share/destination" # 取最后修改的1个文件复制 latest_file=$(ls -t "$source_path" | head -n 1) cp "$source_path/$latest_file" "$target_path" echo "已复制文件: $latest_file" # 对比目标目录,复制更新的文件 for file in "$source_path"/*; do filename=$(basename "$file") target_file="$target_path/$filename" if [ ! -f "$target_file" ] || [ "$file" -nt "$target_file" ]; then cp "$file" "$target_path" echo "已复制更新文件: $filename" fi done
注意事项
- 确保对源目录和目标共享有读写权限
- Linux/macOS 访问SMB共享需先完成挂载操作
- 可根据需求调整筛选逻辑,比如取多个最新文件、设置修改时间阈值等
内容的提问来源于stack exchange,提问作者Tarn Kanlayanee
相关产品推荐
相关产品推荐

