HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Batch file to copy specific folders

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
filebatchfoldersspecificcopy

Problem

This is the batch file i have created which copy the specific folders which I want. I use the specific server folder name of which I want to copy. Please suggest any improvements.

@echo off
:: variables
echo This script takes the backup of file SwiftALM Important folders
set /P source=Enter source folder Example D:\jboss6.1\server\swift:
set /P destination=Enter Destination folder:
set /P Folder=Enter Folder name:
@echo folder=%folder%
mkdir %destination%\%folder%
set xcopy=xcopy /E/V/Q/F/H/I 
echo echo conf folder will be copied
%xcopy% %source%\conf %destination%\%folder%\conf
echo conf folder is copied
echo lib folder will be copied
%xcopy% %source%\lib %destination%\%folder%\lib
echo lib folder is copied
echo deploy folder will be copied
%xcopy% %source%\deploy %destination%\%folder%\deploy
echo deploy folder is copied
echo deployers folder will be copied
%xcopy% %source%\deployers %destination%\%folder%\deployers 
echo deplyers folder is copied
echo files will be copy press enter to proceed
pause

Solution

SETLOCAL ensures that any environment variables you set don't affect the calling process's environment. It's like a sandbox for variables, directory changes, and other shell settings.

I usually use @ECHO instead of plain ECHOs so that if the @ECHO OFF is ever temporarily disabled (say, for debugging purposes), the echo statements don't produce needless noise in the output.

I also suggest using a loop to repeat the nearly-duplicate statements:

@ECHO OFF
SETLOCAL
:: variables
@ECHO This script takes the backup of file SwiftALM Important folders
SET /P source=Enter source folder Example D:\jboss6.1\server\swift:
SET /P destination=Enter Destination folder:
SET /P folder=Enter Folder name:

@ECHO folder=%folder%

MKDIR %destination%\%folder%
SET xcopy=xcopy /E/V/Q/F/H/I

FOR /F %%s IN (conf lib deploy deployers) DO (
echo %%s folder will be copied
%xcopy% %source%\%%s %destination%\%folder%\%%s
IF NOT ERRORLEVEL 1 @ECHO %%s folder has been copied.
)

@ECHO Files have been copied. Press enter to proceed.
PAUSE

Context

StackExchange Code Review Q#70787, answer score: 4

Revisions (0)

No revisions yet.