COPY、XCOPY、ROBOCOPY - 将整个文件路径复制到完整的文件夹路径

COPY, XCOPY, ROBOCOPY - copy entire file path to a full folder path

我正在尝试使用完整文件路径列表(大部分是唯一的)(例如 "C:\Folder1\file_1.xls")复制到完整文件夹路径,(例如“C:\DifferentFolder”)。

完整文件路径和完整文件夹路径因行而异。通过将所有路径放在引号中,我已经使用 COPY 成功地完成了这项工作。

COPY "E:\California\CA_restaurants.pdf" "E:\Placestoeat\"
COPY "E:\California\CA_hotels.pdf" "E:\Placestostay\"
COPY "E:\Arizona\AZ_hotels.pdf" "E:\Placestostay\"

继续使用它的问题是,我发现当我查看 .bat 文件完成前后的文件数量时,我的成功率只有大约 95%。我不确定问题出在哪里,但我正在考虑切换到 Robocopy 或 xcopy,因为我听说它们可以提供有关成功的反馈以及重命名重复文件的选项?

问题是,我似乎无法让 xcopy 或 robocopy 基于完整的文件名和完整的文件夹名称工作,它们都会更改每一行。

谁能解释一下我做错了什么或者给我一个示例脚本让我试试?我不是技术人员,只是想找到一种方法让我平凡的历史数据项目更有效率。

如果您打开 cmd.exe 并输入 xcopy /? 您将看到所有有效的开关,我不确定您尝试过哪些 xcopy 命令,但这应该毫无疑问地工作:

xcopy /Y "E:\folder paths\file.pdf" "E:\Placetostay"

不过我想知道为什么您需要将每一行都添加到要复制的文件中?这是大量的手动工作,以至于手动复制它可能更容易。那么为什么不自动化呢?

这里有一些想法。此脚本将在您的 E:\ 驱动器上进行递归搜索,如果任何文件与名称 hotel 匹配,它将把它复制到相关目标,除非搜索的文件在目标中,仅仅是因为我们不想再次将文件复制到它自己的路径,显然第二个选项也会对 restaurants 执行此操作:

酒店:

@echo off
for /f %%i in ('dir /s /b /a-d *.pdf ^| findstr /i hotel') do (
  if not "%%~dpi"=="E:\Placestostay\" xcopy "%%~fi" "E:\Placestostay"
)

餐厅:

@echo off
for /f %%i in ('dir /s /b /a-d *.pdf ^| findstr /i restaurant') do (
  if not "%%~dpi"=="E:\Placestostay\" xcopy /Y "%%~fi" "E:\Placestoeat"
)

显然上面的也可以组合。

然后,我们可以这样做,但让您通过提示用户输入搜索条件来选择要搜索的内容:

@echo off
set /p "search=Select search type (hotel, restaurant etc.): "
set /p "destination=Enter the destination to copy to (E:\placestoeat\ etc.): "
for /f %%i in ('dir /s /b /a-d *.pdf ^| findstr /i %search%') do (
  if not "%%~dpi"=="%destination%" xcopy /Y "%%~fi" "%destination%"
)

或者类似地,根据搜索预先定义目标文件夹:

@echo off
set /p "search=Select search type (hotel, restaurant etc.): "
if /i "%search%"=="hotel" set "destination=E:\placestostay\"
if /i "%search%"=="restaurant" set "destination=E:\placestoeat\"
for /f %%i in ('dir /s /b /a-d *.pdf ^| findstr /i %search%') do (
  if not "%%~dpi"=="%destination%" xcopy /Y "%%~fi" "%destination%"
)