1

I was able to create this batch file to move certain files from one folder to another. But I want to be able to use it also on different folders. For instance here I'm only moving files from UTS16. I want to use this batch file also for other folders like UTS15, UTS14, UTS13, UTS12, etc.

What do I need to change in code to ask the batch user on which folder to run? What am I missing?

@echo off
SET /P letter=Please give your drive letter and press ENTER:
ECHO %letter%
PAUSE

SET Datefolder="%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%%time:~3,2%"

MD "%Datefolder%"

mkdir %letter%:\UTS16\Database\"RTBackup%Datefolder%"

move /-y "%letter%:\UTS16\Database\*.dbf" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.cdx" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.~cd" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.~db" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.fpt" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\RTBackup%Datefolder%\zipdata.dbf" "%letter%:\UTS16\Database\"
pause

start "" %letter%:\UTS16/dbrepair.exe
Mofi
  • 38,783
  • 14
  • 62
  • 115
  • 2
    Same code you used for the drive letter. – Squashman Jan 14 '17 at 04:09
  • Ok so I was able to determine the year of the folder by using the same code as I did for the drive letter. Thanks for that tip. Sorry but I'm really new at this and I'm trying to figure it out on the fly. Now I can't get the "%Datefolder%" to display the date and time at the end of the RTBackup folder that it creates. I just want it to display the date and time that it was created. – Willmar Lorente Jan 14 '17 at 16:48
  • Select this command line being mainly from your batch file: `echo %date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%%time:~3,2%` and copy it with Ctrl+C to clipboard. Open a command prompt window, paste the copied line by right clicking into the console window and left clicking on __Paste__ in context menu and run this command with pressing key ENTER or RETURN. It is not the output you expect, run the command `echo %DATE% %TIME%` and look on output date and time. – Mofi Jan 14 '17 at 17:04
  • I think I got it to work now. This is what I have: – Willmar Lorente Jan 14 '17 at 17:15

1 Answers1

1

I suggest for your task following commented batch file:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
goto UserPrompt

rem Define environment variable BaseFolder with a double quote character as
rem value in case of the user enters nothing on prompt in which case the
rem variable BaseFolder is still defined with the double quote as value.

rem Then let the user enter the folder path or drag and drop
rem the folder over the console window to enter the path.

rem Next remove all double quotes from folder path and test
rem if the variable BaseFolder still exists with a value.

rem Last replace forward slashes by backslashes in case of user entered
rem the path with forward slashes, make sure the folder path does not
rem end with a backslash and test if the folder really exists in case of
rem user has made a typing mistake on entering manually the folder path.

rem Run the backup and repair operation if entered folder exists as expected.

:UserPrompt
cls
echo/
echo Please type the database base folder path and press ENTER.
echo/
echo Or alternatively drag ^& drop the folder from Windows
echo Explorer on this console window and press ENTER.
echo/

set "BaseFolder=""
set /P "BaseFolder=Path: "
set "BaseFolder=!BaseFolder:"=!"
if "!BaseFolder!" == "" goto UserPrompt
set "BaseFolder=!BaseFolder:/=\!"
if "!BaseFolder:~-1!" == "\" set "BaseFolder=!BaseFolder:~0,-1!"
if "!BaseFolder!" == "" goto UserPrompt
echo/

if not exist "!BaseFolder!\Database\*" (
    echo There is no folder "!BaseFolder!\Database".
    echo/
    choice "Do you want to enter the path once again "
    if errorlevel 2 goto ExitBatch
    goto UserPrompt
)

set "BackupFolder=%BaseFolder%\Database\RTBackup%DATE:~10,4%_%DATE:~4,2%_%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%"

rem For German date/time format which is for DATE TIME: dd.mm.yyy hh:mm:ss,xx
rem set "BackupFolder=%BaseFolder%\Database\RTBackup%DATE:~-4%_%DATE:~-7,2%_%DATE:~-10,2%_%TIME:~0,2%%TIME:~3,2%"

if exist "%BackupFolder%\*" goto MakeBackup

md "%BackupFolder%"
if errorlevel 1 (
    echo/
    echo Error: Failed to create backup folder !BackupFolder!
    echo/
    choice "Repair without making a backup "
    if errorlevel 2 goto ExitBatch
    goto RunRepair
)

:MakeBackup
echo Making a backup to folder !BackupFolder! ...
move /-y "%BaseFolder%\Database\*.dbf" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.cdx" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.~cd" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.~db" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.fpt" "%BackupFolder%" 2>nul
move /-y "%BackupFolder%\zipdata.dbf" "%BaseFolder%\Database\" 2>nul

:RunRepair
echo/
echo Running database repair ...
"%BaseFolder%\dbrepair.exe"

:ExitBatch
endlocal

Please read first answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? explaining the difference between set variable="value" and set "variable=value".

The environment variable Datefolder was created in batch file in question with double quotes included in environment variable value resulting in expanding

"%letter%:\UTS16\Database\RTBackup%Datefolder%"

for example to

"C:\UTS16\Database\RTBackup"2017_01_13_1250""

which of course is not good. Double quotes inside a double quoted string is in general not correct.

And the command line

mkdir %letter%:\UTS16\Database\"RTBackup%Datefolder%"

expanded for example to

mkdir C:\UTS16\Database\"RTBackup"2017_01_13_1250""

Error correction of Windows must do overtime to fix the folder paths.

The date/time format of the environment variables DATE and TIME depends on Windows region and language settings of current user. I needed a different line to define the backup folder with date and time in name for my German Windows machine. There are region independent solutions posted for example at How to get current datetime on Windows command line, in a suitable format for using in a filename? However, if the faster command line using the environment variable DATE and TIME work on the computers where this batch file is used, there is no need to replace that line with a region independent solution.

The batch file in this answer uses delayed expansion of environment variables mainly to prevent an exit of batch processing in case of user enters by mistake a path string which results without usage of delayed expansion in a syntax error.

The user can drag & drop the folder also for example from Windows Explorer over the console window to enter the folder path on prompt.

echo/ is used to output a blank line which is better than echo. as explained by DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/

The ampersand & is interpreted by Windows command interpreter as operator for executing multiple commands in one command line in an unquoted string. For that reason it is necessary to escape this character with the caret character ^ to be interpreted as literal character to echo into the console window. See Single line with multiple commands using Windows batch file for details on meaning of &, && and || in a command line in an unquoted string and not escaped with ^.

The command choice is used on asking if the user wants to proceed on error or exit the batch file. This command appends to prompt text in square brackets the keys to press for Yes or No in language of Windows and a question mark, i.e. [Y,N]? on English Windows or [J,N]? on German Windows. choice does not allow any other key before exiting. The exit code assigned to errorlevel is 2 on No and 1 on Yes.

The Microsoft support article Testing for a Specific Error Level in Batch Files explains the usage of if errorlevel to test on exit code of previous command or application. In this case it is enough to test on errorlevel being greater or equal 2 to exit batch processing on an error in case of user chooses No.

The batch file does not check if "%BaseFolder%\dbrepair.exe" really exists before it tries to execute this application. It would be good if that additional check with appropriate error message for the user is added, best before creating the backup folder.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • choice /?
  • cls /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • md /?
  • move /?
  • set /?
  • setlocal /?

And read also the Microsoft TechNet article Using command redirection operators for an explanation of 2>nul.

Mofi
  • 38,783
  • 14
  • 62
  • 115