3
\$\begingroup\$

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
\$\endgroup\$
4
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Except @ECHO OFF, the @ for the rest are unnecessary, right? \$\endgroup\$ – janos Nov 25 '14 at 16:21
  • 1
    \$\begingroup\$ @janos Yes. I include them as the Proper Way to Do Things™ so that if you ever comment out @ECHO OFF, the @ECHO statements don't produce noisy output. \$\endgroup\$ – mojo Nov 25 '14 at 17:48
  • \$\begingroup\$ Interesting +1. It would be good to mention this in your post. \$\endgroup\$ – janos Nov 25 '14 at 18:14
2
\$\begingroup\$

Some minor improvements:

  • It looks like you doubled echo in this command by mistake: echo echo conf folder will be copied

  • It would be good to add some blank lines to separate related blocks and improve readability

With the above suggestions applied it becomes:

@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:
set xcopy=xcopy /E/V/Q/F/H/I 

echo folder=%folder%
mkdir %destination%\%folder%

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
\$\endgroup\$
  • 1
    \$\begingroup\$ @VaibhavVeralkar Use a loop, too. \$\endgroup\$ – mojo Nov 25 '14 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.