[Closed] create and share a folder with maxscript
You can pipe the window batch command output to a text file and load the text file into max script.
in windows batch its looks like this:
chcp > "C:\ path o\some\file.txt"
to read the result in max :
fn get_cmd_chcp_output =
(
--create temp file
tempFile=(getdir #temp)+"\\cmdoutput.txt"
--pass stdout to temp file
hiddendoscommand ("chcp > \""+tempfile+"\"")
--read temp file
cmdData=#()
f=openfile tempfile
while not eof f do( append cmdData( readLine f ))
close f
--return cmd output
cmdData
)
I use this method to get information from perforce commands.
Mambo4, tell me please, what page code you receive in cmd? 437?
I have a very interesting idea. I will try tomorrow and let you know.
i just can’t understand why .net version doesn’t work. it looks like we do exactly the same as ‘successful’ snippets found on inet
It is because dot net gives Windows Code Page, but I need DOS Code Page.
I wanted to change the codepage, but changing it to 437 doesn't translate groupnames. this is the problem.
Thats why we need to get the list of groupnames.
One solution is to get it in cmd with this command: whoami /groups /NH
But it gives a full table with all groupnames.
After investigation I found out that each group name have its unique SID which does not depend on language. But using cacls, you can not specify a SID as a group name, only the name itself.
The SID for the group "everyone" is always "S-1-1-0"
Denis, Pleas try to translate this C# code to maxscript and print the result.
using System.Security.Principal;
SecurityIdentifier sid = new SecurityIdentifier("S-1-1-0");
NTAccount acct = (NTAccount)sid.Translate(typeof(NTAccount));
Console.WriteLine(acct.Value);
I came up with using Windows PowerShell code wich allows to run cmd commands with functions.
If I send commands from maxscripts, then I loose nonEnglish characters.
My code is working now, however, I think it could be replaced by C# (if using send cmd commands from inside C#).
So here is my working code:
fn RunShellFile ShellFile =
(
local ShellPath = "C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"
local procInfo = dotnetobject "System.Diagnostics.ProcessStartInfo" ShellPath
procInfo.Arguments = "-noprofile -executionpolicy bypass -File " + "\"" + ShellFile + "\""
procInfo.CreateNoWindow = on
procInfo.useShellExecute = off
local DotNetHandle = dotnetclass "System.Diagnostics.Process"
local SharingProcess = undefined
try
(
SharingProcess = DotNetHandle.Start procInfo
SharingProcess.StartInfo.WindowStyle=(DotNetClass "System.Diagnostics.ProcessWindowStyle").Hidden
)
catch
(
print "error launching Shell"
return false
)
SharingProcess.WaitForExit()
if not ( SharingProcess.ExitCode == 0 ) then
(
print "Sharing was not successful, please Share the Folder Manually!"
return false
)
else
(
print "Successfuly Shared"
return true
)
SharingProcess.Close()
)
fn create_share folderPath =
(
makeDir folderPath
ShellFile=(folderPath +"\\powershell_share.ps1")
ShellCommandBuffer =
#(
"function Get-GroupName",
"{",
" param ($SID)",
" $objSID = New-Object System.Security.Principal.SecurityIdentifier($sid)",
" $objUser = $objSID.Translate([System.Security.Principal.NTAccount])",
" $objUser.Value",
"}",
"cmd /c $(" + "\"" + "net share " + (pathConfig.stripPathToLeaf folderPath) + "=" + folderPath + " /UNLIMITED /GRANT:"+ "\"" + "\"" +"$(Get-GroupName -SID 'S-1-1-0')"+ "\"" + "\"" +",FULL" + "\"" + ")",
"cmd /c $(" + "\"" + "cacls " + folderPath + " /e /p " + "\"" + "\"" +"$(Get-GroupName -SID 'S-1-1-0')"+ "\"" + "\"" + ":f" + "\"" + ")"
)
(dotnetclass "System.IO.File").WriteAllLines ShellFile ShellCommandBuffer
RunShellFile ShellFile
deleteFile ShellFile
)
create_share (sysInfo.tempdir + "test_folder")
PS: Edit the word powershell.exe. I don’t know why, here appears some spaces.