Sharing folder via batch script on french windows

后端 未结 1 983
有刺的猬
有刺的猬 2020-12-22 07:50

This is my batch script which I use to automatically share specific folder with \"Everyone\". As You might have noticed I have to first extract name in current installed lan

相关标签:
1条回答
  • 2020-12-22 08:17

    It would have been helpful if you'd shown the actual wmic output you are receiving. Based on my English-language version, I'd suggest

    Remove the space between the = and " in the delims clause.

    @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION 
    set MySid=S-1-1-0
    for /f "delims=" %%a in ('"wmic path win32_account where SID='%MySid%' get name"') do (
     SET "line=%%a"
     CALL :striptrailing
     if not "!line!"=="Name" (
      set "myvar=!line!"
      goto :loop_end
     )
    )
    :loop_end
    
    SET my
    ECHO ==="%myvar%"---
    
    GOTO :EOF
    
    :striptrailing
    IF NOT DEFINED line GOTO :EOF 
    SET "line=%line:~0,-1%"
    IF NOT DEFINED line GOTO :EOF 
    if "%line:~-1%"==" " GOTO striptrailing
    GOTO :eof
    

    The wmic output lines are terminated by CRCRLF, not CRLF as is convention. Consequently, the last character of %%a when delims is empty is CR - and this confuses conventional processing.

    With Space as a delimiter, %%a will acquire Name on the first line - but that is deceptive as the actual output is Name [CR] hence delims correctly selects Name.

    On the next output line, in English you get Everyone [CR] which again "correctly" selects Everyone. This would fail if the user was Atak Snajpera as it would select just Atak.

    So - we need to turn delims off to get the entire line, then manipulate line by removing the last character (which will be CR) and any trailing spaces. This must be done via a user-variable (I chose line) since substringing isn't allowed on metavariables.

    In order to access the run-time value of the uservariable within the code-block, you need to invoke delayedexpansion.

    The :striptrailing routine removes the last character unconditionally (we know it is a CR) and then continues while ever the last character of line is a space.

    On reviewing the code, I believe it would be possible to make a couple of alterations.

    First, since we know the first line will be Name, we can access the second line directly using skip=1 in the for, which removes the requirement for the if and also for the delayedexpansion - all we need to do is move the routine to after loop_end to dispose of the terminal CR and spaces. And then there's no need to have line - myvar can be used instead.

    Which I'll leave as an exercise for those interested...

    0 讨论(0)
提交回复
热议问题