Skip to content

Commit

Permalink
Version 1.2
Browse files Browse the repository at this point in the history
- Support of network-shared folder in UNC form ('\\...') is available
- Support of non-admin users is provided by 'runas /savecred'
- Remove support of UNC-unsupported error-checking cases
  • Loading branch information
wandersick committed Aug 4, 2020
1 parent d10f6c8 commit 1d1dcf2
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 137 deletions.
144 changes: 58 additions & 86 deletions FolderTemplateProvisioner.bat
Original file line number Diff line number Diff line change
@@ -1,52 +1,83 @@
:: Name: ws-folder-template-provisioner
:: Version: 1.1
:: Date: 20200719
:: Version: 1.2
:: Date: 20200730
:: GitHub Repository: https://github.com/wandersick/ws-folder-template-provisioner
:: Description:
:: This Windows batch script provisions (copies) new folders with exact permissions and content
:: from a specified existing folder (template) based on the information (first name & last name)
:: inputted by a user via its command-line interface.
::
:: It solves a problem using 'robocopy /MIR /COPYALL' in which folders copied using Windows
:: Explorer (a.k.a. File Explorer) may not retain unique permissions and inherit permissions
:: from parent folder.
::
:: The script has been designed with care to improve usability and avoid accidental deletion.
:: It solves a problem using 'robocopy /MIR /COPYALL /ZB' (built-in) where folders copied using
:: Windows Explorer (a.k.a. File Explorer) may not retain unique permissions and inherit
:: permissions from parent folder.
::
:: What's New:
:: Support of network-shared folder in UNC form ('\\...') is available
:: - In other words, for drives mapped using a drive letters, they are supported by specifying
:: the UNC path within this script. See 'How to Set up the Scripts' section below.
:: - To test or use this script for non-network cases (local drives) from v1.2 and on, specify
:: \\127.0.0.1\... or \\localhost\... where required
::
:: Support of non-admin users, provided by 'runas /savecred' (built-in)
:: - If users do not have admin rights, _runasAdmin.bat (included optional script) can be edited
:: to leverage 'runas /savecred' to run FolderTemplateProvisioner.bat (main script) as admin
:: without entering admin credentials
::
:: Features: Refer to README.md
::
:: Requirements:
:: 1. Windows OS with robocopy
:: 2. Administrator rights (required by robocopy /COPYALL)
::
:: - Windows OS with robocopy
:: - Non-admin rights (partially supported with an optional setup on _runasAdmin.bat)
:: - admin rights (with or without UAC)
::
:: Script Filenames:
:: 1. FolderTemplateProvisioner.bat (main script)
:: 2. _elevate.vbs (optional, for UAC elevation if admin rights are unavailable)
:: 2. _elevate.vbs (optional, for admin users with UAC turned on, trigger UAC elevation prompt)
:: 3. _runasAdmin.bat (optional, for non-admin users to leverage 'runas /savecred' to run as admin)
::
:: Setting up the Scripts:
:: 1. Edit `templateName` variable at the upper area of 'FolderTemplateProvisioner.bat' script
:: How to Set up the Scripts:
:: 1. (Optional - in case users executing the script would not have admin rights):
:: Edit the UNC path in _runasAdmin.bat setting it to the the script path, e.g.
:: \\serverName\d$\Human Resources\01_Personnel-Files\FolderTemplateProvisioner.bat
:: - It must be a UNC path starting with "\\" instead of a drive letter
:: - This script (runas /savecred) needs to be run once on PCs of users who need
:: to use the script without admin credentials. (The first run involves prompting for
:: admin credentials where admin needs to be there to input admin password once)
:: 2. Edit 'encPath' variable at the upper area of 'FolderTemplateProvisioner.bat' script
:: by setting it to the network folder containing the script, e.g.
:: \\serverName\d$\Human Resources\01_Personnel-Files
:: 3. Edit 'templateName' variable at the upper area of 'FolderTemplateProvisioner.bat' script
:: by setting it to the folder acting as the template, e.g.
:: 'ZZ IT_do not use\01 Template Folder', with required files and permissions inside
:: 2. Place both scripts ('FolderTemplateProvisioner.bat' and optionally '_elevate.vbs') inside
:: a folder containing 'A,B,C...Z' sub-folders, sitting beside them. The 'A-Z' folders contains
:: the template folder and provisioned folders named 'LASTNAME, Firstname' copied by the script
:: from the template folder
:: 4. Place all scripts ('FolderTemplateProvisioner.bat', optionally '_elevate.vbs' and
:: '_runasAdmin.bat') inside a folder containing 'A,B...Z' sub-folders, sitting aside.
:: The 'A-Z' folders contains the template folder and provisioned folders named
:: 'LASTNAME, Firstname' copied by the script from the template folder
::
:: Folder Hierarchy: Refer to README.md
::
:: How to Provision a New Folder:
:: 1. Double-click FolderTemplateProvisioner.bat and follow on-screen instructions
:: - Note: for non-admin users, they should run "_runasAdmin.bat" (never run "_elevate.vbs")
:: 2. Input last name and first name
:: 3. Review the input
:: 4. Wait for robocopy file copy (folder template provisioning)
:: 5. Verify the created folder (which pops up optionally at the end)
:: 5. Verify the created folder
::
:: Screenshots: Refer to README.md

@echo off

:: Clear UNC error message that can be ignored, e.g.
:: '\\path\to\somewhere'
:: CMD.EXE was started with the above path as the current directory.
:: UNC paths are not supported. Defaulting to Windows directory.
cls

setlocal enabledelayedexpansion

:: Define your template folder name here (without quotes)
:: Define the UNC path to file share and your template folder name here (without quotes)
set uncPath=\\127.0.0.1\d$\Dropbox (CSS)\CSS Main Folder (1)\07 Human Resources\01_Personnel-Files
set templateName=ZZ IT_do not use\01 Template Folder

:: Set the working directory where script is located by %~d0%~p0 (e.g. x:\...\here)
Expand Down Expand Up @@ -89,7 +120,7 @@ attrib -h "%windir%\system32" | find /i "system32" >nul 2>&1
if %errorlevel% EQU 0 (
REM only when UAC is enabled can this script be elevated. Otherwise, non-stop prompting will occur.
if "%UACenabled%" EQU "1" (
cscript //NoLogo "%WorkingDir%\_elevate.vbs" "%WorkingDir%" "%WorkingDir%\FolderTemplateProvisioner.bat" >nul 2>&1
cscript //NoLogo "%WorkingDir%_elevate.vbs" "%WorkingDir%" "%WorkingDir%FolderTemplateProvisioner.bat" >nul 2>&1
goto :EOF
) else (
echo.
Expand Down Expand Up @@ -136,7 +167,7 @@ set goAhead=
echo.
echo ___________________________________________________________________
echo.
echo :: Based on the entry, below folder will be created:
echo :: Please ensure the folder does not exist; otherwise, any existing data will be overwritten:
echo.
echo Folder name: "%lastName%, %firstName%"
echo.
Expand Down Expand Up @@ -181,59 +212,6 @@ set lastName1st=
REM Grab first letter from last name
set lastName1st=%lastName:~0,1%

dir "%templateName%">nul 2>&1
if %errorlevel% NEQ 0 (
echo.
echo ___________________________________________________________________
echo.
echo :: Error: Template folder name "%templateName%" does not exist in target location
echo.
echo Or it is defined wrongly in the script
echo.
endlocal
echo Press any key to quit . . .
pause >nul
goto :EOF
)

REM Enter the responsible single-letter folder
pushd %lastName1st%>nul 2>&1
if %errorlevel% NEQ 0 (
echo.
echo ___________________________________________________________________
echo.
echo :: Error: Target A-Z folder "%lastName1st%" is invalid or inaccessible. Please check and try again
echo.
popd
pause
goto :enterName
) else (
popd
)

if exist "%lastName1st%\%folderName%" (
echo.
echo ___________________________________________________________________
echo.
echo :: Warning: A folder with the same name as "%folderName%" already exists
if "!folderOpened!" NEQ "1" (
echo.
echo Please confirm it is unneeded . . . Opening the folder . . .
ping 127.0.0.1 -n 2 >nul 2>&1
explorer "%lastName1st%\%folderName%"
set folderOpened=1
)
echo.
set /p goAhead= :: Are you sure to DELETE it and replace it with a new one? ^(Answer 'N' to quit if unsure^) [Y,N]

if /i "!goAhead!" EQU "N" (
goto :enterName
) else if /i "!goAhead!" EQU "Y" (
goto :createFolder
) else (
goto :folderExistenceCheck
)
)

:: Create folder
:createFolder
Expand All @@ -248,7 +226,7 @@ ping 127.0.0.1 -n 2 >nul 2>&1

REM Copy template folder as a new folder
set robocopyError=
robocopy "%templateName%" "%lastName1st%\%folderName%" /MIR /COPYALL /TEE /LOG+:%Temp%\FolderCreator.log
robocopy "%uncPath%\%templateName%" "%uncPath%\%lastName1st%\%folderName%" /MIR /COPYALL /ZB /TEE /LOG+:%Temp%\FolderCreator.log
if %errorlevel% GTR 3 set robocopyError=%errorlevel%

:: Folder created
Expand All @@ -260,24 +238,18 @@ echo.
echo :: Completed
echo.
if defined robocopyError goto :robocopyError
set /p goAhead= :: Open the new folder now? [Y,N]
if /i "%goAhead%" EQU "N" (
goto :end
) else if /i "%goAhead%" EQU "Y" (
explorer "%lastName1st%\%folderName%"
goto :end
) else (
goto :folderCreated
)
echo Press any key to quit . . .
pause >nul
goto :end

:: End
:end
endlocal
cls
echo.
echo Thank you for using :^)
echo Don't forget to verify the created folder :^)
echo.
echo Have a good day!
echo Thank you for using!
echo.
ping 127.0.0.1 -n 2 >nul 2>&1
cls
Expand Down
Loading

0 comments on commit 1d1dcf2

Please sign in to comment.