3

I'm trying to automate a simple but repetitive task for a non-it-literate friend. I'm a TV editor, but I have a simple working knowledge of this, so I appreciate your skill and knowledge.

On a Windows PC, with PowerShell available, I'd like a script (I'd really like a GUI but that would be dreaming), to

  1. Create a new directory in a specific hard coded location - checking that no existing directory of the same name exists, etc.

  2. The new directory is shared with everyone, full read/write permission

  3. The new directory is mapped, as persistent with the next available drive letter, skipping optical media / card reader drive letters.

NB: As mentioned, the network share has to be persistent after a reboot.

Research suggests for 1 (but needs more to check for duplicates, etc.):

# PowerShell creates a folder
$Location = "X:\Clients\"
New-Item -Path $Location -Name "My_New_Folder" -ItemType "directory"
#Invoke-Item $Location

Research suggests for 2: ? No Idea where to start

Research suggests for 3:

@echo off
setlocal

call :freedrive mydriveletter && goto :cont
echo ERROR: No free drive letter found.
goto :exit
:cont
echo Found drive letter: %mydriveletter%

goto :exit

rem Finds a free drive letter.
rem
rem Parameters:
rem     %1 = Output variable name.
rem
rem Example:
rem     call :freedrive mydriveletter && goto :cont
rem     echo ERROR: No free drive letter found.
rem     goto :EOF
rem     :cont
rem     echo Found drive letter: %mydriveletter%
:freedrive
setlocal EnableDelayedExpansion
set exitcode=0
set "output_var=%~1"
for %%i in (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) do (
    set "drive=%%i:"
    rem If 'subst' fails, the drive letter is already in use.
    rem This way we can even detect optical drives that have a drive
    rem letter but no media in them, a case that goes undetected when
    rem using 'if exist'.
    subst !drive! %SystemDrive%\ >nul
    if !errorlevel! == 0 (
        subst !drive! /d >nul
        goto :freedrive0
    )
)
set exitcode=1
set drive=
:freedrive0
endlocal & set "%output_var%=%drive%" & exit /b %exitcode%

:exit
pause
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ogriff
  • 41
  • 1
  • 3
  • Just noticed for 3 I've suggested 'subst' but I'd prefer 'net use', for persistent after reboot unless anyone can advise otherwise. Thanks. – Ogriff Jul 04 '15 at 22:32

2 Answers2

1
  1. It's unnecessary. Just create the folder, if it already exists it will fail.

    Use the MD aka mkdir command in batch.

    md c:\somepath\somefolder
    

    This is faster as there is only one disk access and uses less system resources.

  2. You have both file/folder permissions and share permission.

    icacls c:\somepath\somefolder /grant everyone:F
    
    net share sharename=c:\somefolder /grant:everyone,FULL
    
  3. To map a drive

    But I'm puzzled - what good does a mapped drive do the user?

    net use * \\server\share\folder /persistant:yes
    

And of course mkdir /?, icacls /?, net help share, net help use,

user5071892
  • 147
  • 2
  • It would be great to have a script where "new folder name goes here" is edited and then running takes care of the rest. The reason for a shared folder is to mount it with a drive letter so that my friend's video editing system gets a discreet volume per job. The software only stores media to its own location on each volume so it's a neat way to keep things tidy. A drive for each project. Thanks. – Ogriff Jul 05 '15 at 15:45
  • Could the above be rationalised so that the script takes the name of the new directory and then passes it through the remaining stages automatically? It's beyond me but I can see the logic of how it could. – Ogriff Jul 05 '15 at 15:50
  • `set newfolder=c:\somefolder` or even `set /p newfolder=c:\somefolder` then to use - `mkdir %newfolder%` see `set /?` and you might as well type `help` and see a list of most commands. – user5071892 Jul 05 '15 at 21:31
  • I feel that my original request based on my experience level isn't quite answered. Self study though doubtlessly beneficial will take me an age or end in failure. I belive $name = Read-Host 'What is your filer name?' could be useful to my original post. – Ogriff Jul 06 '15 at 20:08
  • ok I'm getting somewhere # PowerShell creates a folder $folder = Read-Host 'Type the name of Your new directory here' $Location = "C:\Clients\" New-Item -Path $Location -Name "$folder" -ItemType "directory" #Invoke-Item $Location ......so what's the correct way to add $Location and $folder to set the path in icacls c:\somepath\somefolder /grant everyone:F net share sharename=c:\somefolder /grant:everyone,FULL – Ogriff Jul 06 '15 at 20:47
0

So I think I have solved my own problem, the syntax / grammar was confusing for a newbie to pass variables along...

# create a folder with user input
$folder = Read-Host 'Name a new directory to be created in the Clients folder'
$Location = "C:\Clients\"
New-Item -Path $Location -Name "$folder" -ItemType "directory"
# set permission
icacls "$Location$folder" /grant everyone:F
# share folder
net share $folder="$Location$folder" /GRANT:EVERYONE`,FULL
# map folder as drive
net use * /persistent:yes \\localhost\$folder 
Ogriff
  • 41
  • 1
  • 3