Batch file 我创建了一个windows批处理文件,用于将文件从源文件夹复制到备份文件夹,但子例程无法正常工作?

Batch file 我创建了一个windows批处理文件,用于将文件从源文件夹复制到备份文件夹,但子例程无法正常工作?,batch-file,Batch File,我有一个简单的备份脚本 基本上将文件从源文件夹复制到备份文件夹,但子例程只是在一个恒定循环中运行。 我想让它做的是询问我是否要一个接一个地复制每个文件,以及何时所有文件都复制完成 这是剧本 @echo off echo. echo The time is %time% on %date%. echo. echo.Backup Starting... echo. echo Checking

我有一个简单的备份脚本 基本上将文件从源文件夹复制到备份文件夹,但子例程只是在一个恒定循环中运行。 我想让它做的是询问我是否要一个接一个地复制每个文件,以及何时所有文件都复制完成 这是剧本

@echo off
echo.                                   
echo The time is %time% on %date%.                  

echo.
echo.Backup Starting...
echo.
echo Checking if the source folder exists...
echo.
if exist "%C:\Users\Lee\Project\%" (                REM checks if the source folder exists
  set formerDir=%cd%                        REM sets the former directory
  echo source folder exists.
) else (                            REM if the source folder doesnt exist user is notified
  echo it doesn't exist
)


echo.
echo Checking if the Backup folder exists...
echo.
if exist "%C:\Users\Lee\Backup\%" (             REM checks if the backup folder exists
  set formerDir=%cd%                        REM sets the former directory
  echo Backup folder exists.
) else (                            REM if the backup folder doesnt exist user is notified
  echo Backup folder doesn't exist.
  echo.
  echo Creating Backup folder...
  md%C:\Users\Lee\Backup\%                  REM backup folder is created
  echo.
  echo Folder has been created.                 REM user is notified
)

:Subroutine                             REM the subroutine
echo.
echo %C:\Users\Lee\Project
dir "%C:\Users\Lee\Project"
echo.
set /p p="Do you want to copy this file(Y/N)?"              REM user is asked if they want to copy the file
echo.
echo %p%
if "%p%" == "y" (                           REM is the user replies yes the file is copied to the backup folder
  echo.
  xcopy /s C:\Users\Lee\Project C:\Users\Lee\Backup
  attrib +r "C:\Users\Lee\Backup"
  echo.
  echo File has been copied.
) else (
  echo.
  echo Skipped
  echo.
)

call :Subroutine
echo.
echo Backup Complete!
echo.
echo Press enter to exit.
pause >nul                              REM results are paused on screen for the user
exit

非常感谢任何帮助

其中一个问题是,在大多数情况下,REM被放在自己的线路上

这有一些变化,但未经测试:

@echo off
echo.                                   
echo The time is %time% on %date%.                  

echo.
echo.Backup Starting...
echo.
echo Checking if the source folder exists...
echo.
if exist "C:\Users\Lee\Project\" (
                REM checks if the source folder exists
  set "formerDir=%cd%"
                REM sets the former directory
  echo source folder exists.
) else (
                REM if the source folder doesnt exist user is notified
  echo it doesn't exist
)


echo.
echo Checking if the Backup folder exists...
echo.
if exist "C:\Users\Lee\Backup\" (
             REM checks if the backup folder exists
  set "formerDir=%cd%"
             REM sets the former directory
  echo Backup folder exists.
) else (
             REM if the backup folder doesnt exist user is notified
  echo Backup folder doesn't exist.
  echo.
  echo Creating Backup folder...
  md "C:\Users\Lee\Backup\"
             REM backup folder is created
  echo.
  echo Folder has been created.
             REM user is notified
)


echo.
echo C:\Users\Lee\Project

for %%a in ("C:\Users\Lee\Project\*.*") do (
echo.
set "p="
set /p "p=Do you want to copy %%~nxa [N or enter for Yes]? "
             REM user is asked if they want to copy the file
echo.

if not defined p (
             REM if the user presses enter the file is copied to the backup folder
  echo.
  copy "%%a" "C:\Users\Lee\Backup" >nul
  attrib +r "%%a"
  echo.
  echo File has been copied.
) else (
  echo.
  echo "%%a" Skipped
  echo.
)

)
echo.
echo Backup Complete!
echo.
echo Press enter to exit.
pause >nul
            REM results are paused on screen for the user
exit

这里有一个无限循环(
调用
命令每次都会创建一个新上下文,无法退出)。您使用的
xcopy
命令一次复制所有内容。您还错误地使用了
dir
命令(
dir
仅显示当前目录列表)。在正确的方向上检查这个问题:您是否尝试在子例程末尾使用GOTO EOF。你的代码让我想起了一个无限循环:循环一些代码。后藤:循环。通常例程放在文件的末尾。