批处理:将文件格式源复制到目标 - 找不到指定文件的错误

时间:2017-09-18 14:59:30

标签: batch-file for-loop copy

我有一个文件夹结构,其中每个文件夹都有一个我要复制到新目的地的excel文件。

示例:

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

目标路径:C:\ Data \ 0。 MyFolder文件\ NewFolder \ 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

1 个答案:

答案 0 :(得分:1)

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

COPY "!template_full_path_file!" "!updated_full_path!"