[Answered] Help learning windows scripting

Discussion in 'Scripting' started by goodflood, Sep 23, 2018.

  1. goodflood

    goodflood MDL Member

    Feb 16, 2011
    109
    10
    10
    #1 goodflood, Sep 23, 2018
    Last edited by a moderator: Sep 23, 2018
    So, I have a simple script.

    When I right click and say, run as admin, it does not do as intended.

    When, I run it from command prompt with the filename.extension, it does do what the script asks.

    Here is the script:
    Code:
    @echo off
    for /f %%A in ('dir /b *.msu') do (
    echo == Installing Updates == "%%A" ...
    timeout /t 3
    echo %%A
    )
    echo.
    echo ########################################
    echo.
    echo == Updates installed ==
    echo.
    pause
    exit
    
    Why the difference? I know this is a little complicated in that I am listing contents of a directory. Does this have anything to do with how it is run?
     
  2. goodflood

    goodflood MDL Member

    Feb 16, 2011
    109
    10
    10
    with bash, you simply ./filename.sh

    Here, right clicking and using run as admin, on filename.cmd produces different results than opening elevated command prompt the manually executing the script with filename.cmd. I know I am just echoing the file, but this is for testing only.
     
  3. mxman2k

    mxman2k MDL Developer

    Jun 20, 2007
    5,799
    19,382
    180
    #3 mxman2k, Sep 23, 2018
    Last edited: Sep 23, 2018
    It is because when you right-click and run a script as Admin it defaults usually to the 'C:\Windows\System32' folder which is not where the files you are looking for are located!

    Code:
    @echo off
    PUSHD "%~dp0"
    for /f %%A in ('dir /b *.msu') do (
    echo == Installing Updates == "%%A" ...
    timeout /t 3
    echo %%A
    )
    echo.
    echo ########################################
    echo.
    echo == Updates installed ==
    echo.
    POPD
    pause
    exit
    The PUSHD puts the current folder/script location onto the stack then changes to it, the POPD removes the Stacked folder/location from the list and returns the stack to how it was before the script was run.

    As long as the script is in the folder where the files you are looking for are, then this should work.

    Obviously the script needs to be in the same folder/location where the .msu files are.

    For loops are very powerful but they have to be told exactly how to work. I'm still learning them myself.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. mxman2k

    mxman2k MDL Developer

    Jun 20, 2007
    5,799
    19,382
    180
    You could also use CD /D "%~dp0" to do the same thing, but the PUSHD/POPD works just as good. :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. goodflood

    goodflood MDL Member

    Feb 16, 2011
    109
    10
    10
    I was going to say that this method is not needed in bash, but but after reading this: https://unix.stackexchange.com/questions/77077/how-do-i-use-pushd-and-popd-commands, PUSHD and POPD is used there (linux) as well.

    So in windows, script need to explicitly say...? (I don't have the words, because I don't understand it still).

    I guess this has nothing to do with the PATH environment variable... This reminds of HEAD in git. Moving up and down, back and forth in the directory structure before staging, committing, reverting, etc.

    But, what I don't get is why it is needed... not sure what a STACK refers to? Master OR another branch?
     
  6. goodflood

    goodflood MDL Member

    Feb 16, 2011
    109
    10
    10
    I was going to ask this:

    So, with CD /D "%~dp0"

    cd is change directory
    /D is directory
    and
    %~dp0 is current working directory
    ???​

    But I went here: https://stackoverflow.com/questions...-mean-by-command-cd-d-dp0-in-windows#18310141

    and then, I read this:
    LightBulb went ON : )

    Thank you both!
     
  7. mxman2k

    mxman2k MDL Developer

    Jun 20, 2007
    5,799
    19,382
    180
    Stack = a index of where the OS is at any given time. If you put something on the stack it has to be removed in the same order, Last in First out.

    Linux uses similar stack system, i guess any OS would too.

    Most of the time the stack is transparent to the user, however in programming you have to be aware of it at times.

    you could really get funky with the script you have...

    for /f %%A in ('dir /b "%~dp0*.msu') do (

    or

    CD /D "<path-to-where-to-check>"
    for /f %%A in ('dir /b *.msu') do (

    or

    for /f %%A in ('dir /b "<path-to-where-to-check>\*.msu"') do (

    example: "D:\Updates\*.msu"

    there is many ways, i was just trying to keep it simple.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. mxman2k

    mxman2k MDL Developer

    Jun 20, 2007
    5,799
    19,382
    180
    %~dp0 is the current location yes. Only available in scripts, typing it in a command prompt such as echo %~dp0 you would see %~dp0 not the location. But same within script would show the scripts location.

    Also be aware that %~dp0 will add the ending back slash \ so if you not careful in a script you could get an error as you might put a extra \ thinking that how it is. I was caught out with that.

    What i mean is for example in a script:

    SET "Loc=%~dp0"

    IF EXIST "%Loc%\myfile.txt" echo Found.

    would be seen by the script as IF EXIST "%Loc%\\myfile.txt" echo Found. Note the two \\ which would either give an error or a false result!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. goodflood

    goodflood MDL Member

    Feb 16, 2011
    109
    10
    10
    I appreciate the detailed explanation.

    I understand STACK better now. I know it as Present Working Directory (pwd) and when referencing it, I just use a . (period) in bash. I guess in a bash script, I am always mindful, so make sure to spell out the <pathtofile> from the current location.
     
  10. mxman2k

    mxman2k MDL Developer

    Jun 20, 2007
    5,799
    19,382
    180
    When learning Windows scripting it can get a bit overwhelming, plus if you are used to another OS's way of doing things then you find that the Windows way is different in some aspects.

    Its taken me almost 2 years to learn what i have at the moment and that is by no means everything. I still get tripped up on certain things.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...