【发布时间】:2015-07-07 21:11:23
【问题描述】:
我正在编写一个可以同步 2 个文件夹的批处理脚本。我所说的同步的意思是:脚本将比较每个文件夹的每个文件,并且它只会保留每个文件夹中最新的文件。它基本上是一个智能备份程序。
这是我遇到问题的代码部分(整个脚本可以在here找到):
for /d /r %drivePath% %%d in (*) do (
set currentDriveFolderPath=%%d
if not exist "%devicePath%!currentDriveFolderPath:~%driveLength%!" (
mkdir "%devicePath%!currentDriveFolderPath:~%driveLength%!"
)
for %%f in (*.*) do (
set currentFile=%%f
if exist "%devicePath%!currentDriveFolderPath:~%driveLength%!!currentFile!" (
set currentDriveFile="!currentDriveFolderPath!\!currentFile!"
set currentDeviceFile="%devicePath%!currentDriveFolderPath:~%driveLength%!!currentFile!"
call:compareModifiedDate !currentDriveFile! !currentDeviceFile!
if !recent! == file1 (
copy /y !currentDriveFile! !currentDeviceFile!
) else if !recent! == file2 (
copy /y !currentDeviceFile! !currentDriveFile!
)
) else (
set source
copy /y !currentDriveFile! !currentDeviceFile!
)
)
)
for /d /r %devicePath% %%d in (*) do (
set currentDeviceFolderPath=%%d
if not exist %drivePath%!currentDeviceFolderPath:~%deviceLength%! (
mkdir "%drivePath%!currentDeviceFolderPath:~%deviceLength%!"
for %%f in (*.*) do (
set currentFile=%%f
if not exist "%drivePath%!currentDeviceFolderPath:~%DeviceLength%!!currentFile!" (
copy /y !currentDriveFile! !currentDeviceFile!
)
)
)
)
我的问题是:在源路径和目标路径每次都在变化的情况下,我该怎么做才能在 FOR 循环中调用 COPY 函数?
【问题讨论】:
-
批处理还是 Bash?两个不同的东西
-
@UnknownOctopus - 好吧,脚本显然不是 bash,如果您对 Windows 批处理脚本有所了解,您会立即认出它。此外,OP 始终使用术语批处理和批处理脚本,并使用批处理文件标签。为什么您会推测问题可能不是 Windows 批处理脚本?
-
@dbenham 因为 OP 的链接使用 bash 突出显示。这就是为什么。而且标签并不总是正确的,它们被滥用了很多次。
标签: batch-file for-loop copy