0

When our users create a new Project folder, I would like it to automatically copy a template folder structure and the template's permissions. So far the only thing I've come up with is a batch script that asks the user for a project name and then calls robocopy to copy the folder template. This worked well enough in my testing but is not very user friendly. I also ran into a small problem when I actually fleshed out the complete folder structure instead of testing the template and destination in a flat test folder. I'm looking for a better way of doing this and if that's not possible then a solution to my robocopy script to handle {current folder}{folder named in choice} destinations.

The root folder is a network share located on a Windows server with Active Directory authentication.

This is the location of the template folder: \ServerName\Common\Projects_Template

This is where new project folders are created: \ServerName\Common\Projects\Region\State{NewProject}

Here is the robocopy script I have so far broken down by sections. A copy of the script is kept in each State folder so the user just navigates to the correct state and runs the script file in it. This first section asks the user to name the new project folder and stores that name in a variable. A short error check will re-ask the question if the user mistyped it or decided on a different name.

SET /P fold= Enter the project name: 
CHOICE /M "Is this correct: %fold%"
IF %ERRORLEVEL% equ 2 CLS & GOTO start

This section is supposed to copy the template folder to the current state folder but name it with the name entered in the choice section. During original testing I just used hard locations but I don't want to have to create a separate batch file for each state folder. The ".\%fold%" section is not working as I hoped and I suspect this is due in part to the batch file being run from a network location. As is, it creates the named folder in the C:\Windows\%fold% location on the local computer of the user running the script. The template folder is hidden which is why the /A-:H is in there, the /XX is to prevent any damage from happening if the user accidentally enters the exact name of an existing project folder that has data in it, and the /MIR /SEC and /SECFIX are to copy the exact permissions from the template folder.

robocopy "\\ServerName\Common\Projects\_Template\\" .\%fold% /MIR /SEC /SECFIX /XX /A-:H >nul 2>nul

Ideally, if the user is in a State folder and hits "New Folder" it would do all this for him but I don't know of a way to build that in to the Windows File Explorer functionality. Also, there's no existing tag for "Folder-Structure"?

Fiernaq
  • 41
  • 7

1 Answers1

0

The answer, it turns out, is to use the command line argument with parameter extensions to include the current drive and path as part of your robocopy destination argument. That argument is %~dp0.

Here's an excerpt from http://ss64.com/nt/syntax-args.html which helped me figure this out:

Links relative to the Batch Script

You can get the pathname of the batch script itself with %0, parameter extensions can be applied to this so %~dp0 will return the Drive and Path to the batch script e.g. W:\scripts\ and %~f0 will return the full pathname W:\scripts\mybatch.cmd

You can refer to other files in the same folder as the batch script by using this syntax:

CALL %0..\SecondBatch.cmd

This can even be used in a subroutine, Echo %0 will give the call label but, echo "%~nx0" will give you the filename of the batch script.

When the %0 variable is expanded, the result is enclosed in quotation marks.

It looks like this in the batch script:

SET /P fold= Enter the project name: 
CHOICE /M "Is this correct: %fold%"
IF %ERRORLEVEL% equ 2 CLS & GOTO start

robocopy "\\ServerName\Common\Projects\_Template\\" "%~dp0\%fold%" /MIR /SEC /SECFIX /XX /A-:H >nul 2>nul

Effectively, this says:

Robocopy the _Template folder to the current drive\path as a new folder named with the user's input from the SET command. The entire folder structure will be mirrored to include data, attributes, timestamps, and security. Prevent any deletions from the destination (in case the folder already exists and has files in it this should prevent the /mir from wiping out those files). Remove the hidden attribute from the folder while copying it and prevent error messages from being displayed by sending them to nul.

Other notes: if your source or destination path are surrounded by double quotes (to allow for spaces) then you cannot end the path with a single \ because it is treated as an escape character and thus causes the trailing " to be considered part of the path and not a trailing " which of course causes the path to get split at the space. The solution (assuming you can't always ensure that the path will never contain a space) is to either leave off the trailing \ entirely or double it up so that it is escaping itself (as seen in the above code snippet). I am unclear as of yet on what the best practice is for this since I've always been told that a trailing \ indicates a folder while leaving off the trailing \ indicates a file.

Fiernaq
  • 41
  • 7
  • Also, while I initially thought this question pertained directly to Robocopy, I now realize that the answer is a direct batch script question which would place it more in line with the coding style of questions on StackOverflow but I don't know enough about using these sites to move it if that really is where it should go. – Fiernaq Sep 28 '15 at 18:54