我创建了一个Windows批处理文件,将文件从源文件夹复制到备份文件夹,但子程序无法正常工作?

时间:2013-12-16 14:40:45

标签: batch-file

我有一个简单的备份脚本 基本上将文件从源文件夹复制到备份文件夹,但子程序只是在一个常量循环中运行。 我想要它做的是询问我是否要逐个复制每个文件,并且当它们全部复制完成时 这是脚本。

@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

任何帮助非常感谢

1 个答案:

答案 0 :(得分:0)

其中一个问题是,在大多数情况下,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