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?
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.
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.
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?
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!
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.
%~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!
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.
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.