Sunday, September 20, 2009

How to copy a directory set to another location with user prompt

Hey everyone been a while since I wrote a new post. So here it is. I had a client come to me with a request of prompting a user(system admin) for a folder name and have it copy a set directory set for example:

C:\mainCopySet
C:\mainCopySet\MyDocs
C:\mainCopySet\MyVids
C:\mainCopySet\MyPics
C:\mainCopySet\Desktop

Sys admin types in JOHN

D:\copyLocation\users\JOHN
D:\copyLocation\users\JOHN\MyDocs
D:\copyLocation\users\JOHN\MyVids
D:\copyLocation\users\JOHN\MyPics
D:\copyLocation\users\JOHN\Desktop

So of course the challenge when he said he wanted to use network drives. I told him that he would definitely need to make sure that he has the network folders pre done. He explained that is not a problem. So I simply said I can check to see if the original directories exist. So like the previous tutorial with the .BAT for SQL Express and SQL Database Back Up I will break the code down for you to understand.

@echo off
@REM ** AUTHOR: GrafiX
@REM ** 09-20-09

SET copyDirectory=D:\users\_copyUser
SET newRootDirectory=D:\users\
So here of course right away I place the echo to the off state so that the beginning lines of the cmd do not show as well as the current directory location. Then authoring the .BAT. Below the authoring I set the initial copy locaiton. Notice I do not use a "\" at the end. This is due to how XCOPY wishes for the set to be parsed. After that I set the final destination root. In the clients case the users folder root. If you wanted to take these via command line IE you modify the DSA.msc snapin so that when it creates a new user it runs this script and takes in the user var. All you would have to do is take out hte D:\users\_copyUser and the other directory and simply place %1 and %2.

NOTE: !!! Big note as to BATCH and CMD being sensitive you will need to make sure no spaces are within the directory names. If you wish to have spaces I can modify the code for you but it would be a bit of work to get it to work correctly.

IF EXIST %copyDirectory% GOTO CHECK2
ECHO PLEASE MAKE SURE COPY DIRECTORY IS CORRECT
EXIT
:CHECK2
IF EXIST %newRootDirectory% GOTO PROMPTUSER
ECHO PLEASE MAKE SURE THAT YOU HAVE THE COPY DIRECTORY SET RIGHT
EXIT
So as stated before we check to see if the directories exist if the first one exists then go to the second check if that exists then go to prompting the user for the folder name. if not prompt the admin to make the directories.
:PROMPTUSER
SET /p newDirectory= What folder name do you want?

IF NOT EXIST %newRootDirectory%%newDirectory% GOTO MAKEDIR

GOTO COPYFOLDERS
:MAKEDIR
mkdir %newRootDirectory%%newDirectory%

:COPYFOLDERS
xcopy /E /O /X /Y %copyDirectory% %newRootDirectory%%newDirectory%

ECHO -----FOLDER STRUCTURE SUCCESSFULLY COPIED-----
PAUSE
Again as noted before: The user can not put a space in. Will not work.

Here we check to see if the directory exists first if it does not we create it. else we just skip to copying files. When we copy the files we use XCOPY for several reasons. 1. to copy recursively (copy all sub directories) 2. to preservepermissions and author rights. 3. to say yes to all over write prompts.

Well this concludes this tutorial hope it was helpful. If you have any further questions. Just write me an e-mail.

Thanks for reading,
-GrafiX

No comments:

Post a Comment