0

i have a simple backup script basically copies the files from the source folder to the backup folder but the subroutine is just running in a constant loop. what i want it to do is ask if i want to copy each file one by one and when all of them are copied finish here is the script.

@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

any help greatly appreciated

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • 1
    What you have here is an infinite loop (the `call` command creates a new context each time with no way to exit). The `xcopy` command you're using is copying everything at once. You're also using the `dir` command incorrectly (`dir` only _displays_ the current directory list). Check out this question for a nudge in the right direction: [batch scripting iterating over files in a directory](http://stackoverflow.com/questions/138497/batch-scripting-iterating-over-files-in-a-directory) – Jonah Bishop Dec 16 '13 at 15:20
  • Did you try to use GOTO EOF at the end of subroutine. your code reminds me an infinite loop. :loop rem some code. goto :loop. Usually routines are placed at the end of file. – mihai_mandis Dec 16 '13 at 16:41

1 Answers1

0

One of the issues is that REM are placed on their own line, in most cases.

This has a few changes but is untested:

@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
foxidrive
  • 40,353
  • 10
  • 53
  • 68