【问题标题】:Batch: Copy file form source to destination - Cannot find file specified error批处理:将文件从源复制到目标 - 找不到指定的文件错误
【发布时间】:2018-02-27 04:18:01
【问题描述】:

我有一个文件夹结构,其中每个文件夹都有一个我想复制到新目标的 Excel 文件。

例子:

源路径:C:\Data\0。我的文件夹\1。模板\00。文件夹 0\File.xlsb

目标路径:C:\Data\0。我的文件夹\新文件夹\00。文件夹 0\File.xlsb

“00.Folder 0”是存储在数组中的名称。因此,我使用 for 循环根据数组中的名称创建一个新目录,并创建新的类似结构。

我收到消息“系统找不到指定的文件”。当试图将一个文件从一个文件夹复制到另一个文件夹时。

当我打印文件的路径时,它似乎是正确的。我做错了什么?

代码如下:

@echo off
@break off
@title Generate Subfolders
@color 0a
@cls

setlocal EnableDelayedExpansion

SET "batch_path=%~dp0"
SET "first_folder=01. Folder1"
SET "second_folder=02. Folder2"
SET "third_folder=03. Folder3"

:: Create the new Working Data folder
SET /p new_folder_name= Enter Directory Name: 
SET "full_path=%batch_path%%new_folder_name%"

ECHO Working...

IF NOT EXIST ("%full_path%") (
  MKDIR %new_folder_name%
  IF "!errorlevel!" EQU "0" (
    ECHO Folder created successfully.
  ) ELSE (
    ECHO Error while creating folder.
  )
) ELSE (
  ECHO Folder already exists.
)

SET "folders_list="%first_folder%" "%second_folder%" "%third_folder%""
SET "templates_folder=C:\Data\0. MyFolder\1. Templates"

FOR %%f in (%folders_list%) DO (
    SET "updated_full_path=%full_path%\%%f"
    SET "template_full_path_file=!templates_folder!\%%~f\file.xlsb"
    :: Displays the path file correctly
    ECHO !template_full_path_file!
    MKDIR "!updated_full_path!"
    :: However I cannot copy the file to new destination
    COPY template_full_path_file updated_full_path
    PAUSE
)
PAUSE
EXIT

【问题讨论】:

  • 不连续的报价处理。 %%f 包含已引用的字符串,它应该是 SET "updated_full_path=%full_path%\%%~f" 并且@SAhmad 已经指出 ! 缺失,但由于路径包含空格,它们也必须被引用:COPY "!template_full_path_file!" "!updated_full_path!"
  • @LotPings 非常感谢! :)

标签: batch-file for-loop copy


【解决方案1】:

您没有在copy 命令中解析路径。

COPY "!template_full_path_file!" "!updated_full_path!"

【讨论】:

  • 非常感谢!我还按照@LotPings 的建议添加了双引号。
  • 不错的收获!更新了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多