Posts

Showing posts with the label Batch File

Batch Script To Run As Administrator

Answer : Solutions that did not work No: The - create a shortcut [ -> Compatibility -> "run this program as an administrator" ] solution does not work. This option is greyed out in Windows 7. Even with UAC disabled No: The runas /env /user:domain\Administrator <program.exe/command you want to execute> is also not sufficient because it will prompt the user for the admin password. Solution that worked Yes: Disable UAC -> Create a job using task scheduler, this worked for me. Create a job under task scheduler and make it run as a user with administrator permissions. Explicitly mark: "Run with highest privileges" Disable UAC so there will be no prompt to run this task You can let the script enable UAC afterwards by editing the registry if you would want. In my case this script is ran only once by creation of a windows virtual machine, where UAC is disabled in the image. Still looking forward for the best approach for a script run ...

Batch Not-equal (inequality) Operator

Answer : Try if NOT "asdf" == "fdas" echo asdf Use NEQ instead. if "asdf" NEQ "fdas" echo asdf I know this is quite out of date, but this might still be useful for those coming late to the party. (EDIT: updated since this still gets traffic and @Goozak has pointed out in the comments that my original analysis of the sample was incorrect as well.) I pulled this from the example code in your link: IF !%1==! GOTO VIEWDATA REM IF NO COMMAND-LINE ARG... FIND "%1" C:\BOZO\BOOKLIST.TXT GOTO EXIT0 REM PRINT LINE WITH STRING MATCH, THEN EXIT. :VIEWDATA TYPE C:\BOZO\BOOKLIST.TXT | MORE REM SHOW ENTIRE FILE, 1 PAGE AT A TIME. :EXIT0 !%1==! is simply an idiomatic use of == intended to verify that the thing on the left, that contains your variable, is different from the thing on the right, that does not. The ! in this case is just a character placeholder. It could be anything. If %1 has content, then the equality will be fals...

Batch File For PuTTY/PSFTP File Transfer Automation

Answer : You need to store the psftp script (lines from open to bye ) into a separate file and pass that to psftp using -b switch: cd "C:\Program Files (x86)\PuTTY" psftp -b "C:\path\to\script\script.txt" Reference: https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-option-b EDIT: For username+password: As you cannot use psftp commands in a batch file, for the same reason, you cannot specify the username and the password as psftp commands. These are inputs to the open command. While you can specify the username with the open command ( open <user>@<IP> ), you cannot specify the password this way. This can be done on a psftp command line only. Then it's probably cleaner to do all on the command-line: cd "C:\Program Files (x86)\PuTTY" psftp -b script.txt <user>@<IP> -pw <PW> And remove the open , <user> and <PW> lines from your script.txt . Reference: https://the.earth.li...

Assign Command Output To Variable In Batch File

Answer : A method has already been devised, however this way you don't need a temp file. for /f "delims=" %%i in ('command') do set output=%%i However, I'm sure this has its own exceptions and limitations. This post has a method to achieve this from (zvrba) You can do it by redirecting the output to a file first. For example: echo zz > bla.txt set /p VV=<bla.txt echo %VV% You can't assign a process output directly into a var, you need to parse the output with a For /F loop: @Echo OFF FOR /F "Tokens=2,*" %%A IN ( 'Reg Query "HKEY_CURRENT_USER\Software\Macromedia\FlashPlayer" /v "CurrentVersion"' ) DO ( REM Set "Version=%%B" Echo Version: %%B ) Pause&Exit http://ss64.com/nt/for_f.html PS: Change the reg key used if needed.

Batch Character Escaping

Answer : This is adapted with permission of the author from the page Batch files - Escape Characters on Rob van der Woude's Scripting Pages site. TLDR Windows (and DOS) batch file character escaping is complicated: Much like the universe, if anyone ever does fully come to understand Batch then the language will instantly be replaced by an infinitely weirder and more complex version of itself. This has obviously happened at least once before ;) Percent Sign % % can be escaped as %% – "May not always be required [to be escaped] in doublequoted strings, just try" Generally, Use a Caret ^ These characters "may not always be required [to be escaped] in doublequoted strings, but it won't hurt": ^ & < > | Example: echo a ^> b to print a > b on screen ' is "required [to be escaped] only in the FOR /F "subject" (i.e. between the parenthesis), unless backq is used" ` is "required [to be es...

Batch - If, ElseIf, Else

Answer : @echo off title Test echo Select a language. (de/en) set /p language= IF /i "%language%"=="de" goto languageDE IF /i "%language%"=="en" goto languageEN echo Not found. goto commonexit :languageDE echo German goto commonexit :languageEN echo English goto commonexit :commonexit pause The point is that batch simply continues through instructions, line by line until it reaches a goto , exit or end-of-file. It has no concept of sections to control flow. Hence, entering de would jump to :languagede then simply continue executing instructions until the file ends, showing de then en then not found . @echo off set "language=de" IF "%language%" == "de" ( goto languageDE ) ELSE ( IF "%language%" == "en" ( goto languageEN ) ELSE ( echo Not found. ) ) :languageEN :languageDE echo %language% This works , but not sure how your language variable is de...

Batch - Copy File Using Relative Path

Answer : if you start your path with \ , it's an absolute, not a relative path. Try copy "Debug\text.txt" "..\..\new" instead

Batch File Command PAUSE Does Not Work

Answer : If the last command fails pause won't work. You can fix it by putting "call" behind the command you are running (whatever command is before the pause) then the pause will work. So for example I had a phpunit batch file that looked like this: phpunit tests/sometests.php pause When phpunit failed it just exited without pausing. Changing it to this made it pause correctly: call phpunit tests/sometests.php pause Does the last command before pause execute successfully? Mind sharing your script - at least last few commands? Alternatively, since you seem to be using Windows7, try Timeout command and see if that is working. I was having issues even on echo... assuming it was caused by long batch file... Pause was executing but it was not pausing, it was almost like that it was pressing a key after Pause was executed. Tried suggested solutions above; none worked. So just for future reference, here is what I did: Basically just "pause > nul ...

'adb' Is Not Recognized As An Internal Or External Command, Operable Program Or Batch File

Answer : Set the path of adb into System Variables. You can find adb in " ADT Bundle/sdk/platform-tools " Set the path and restart the cmd n then try again. Or You can also goto the dir where adb.exe is located and do the same thing if you don't wanna set the PATH. If you wanna see all the paths, just do echo %PATH% From Android Studio 1.3, the ADB location is at: C:\Users\USERNAME\AppData\Local\Android\sdk\platform-tools. Now add this location to the end of PATH of environment variables. Eg: ;C:\Users\USERNAME\AppData\Local\Android\sdk\platform-tools If you want to use it every time add the path of adb to your system variables: enter to cmd (command prompt) and write the following: echo %PATH% this command will show you what it was before you will add adb path setx PATH "%PATH%;C:\Program Files\android-sdk-windows\platform-tools" be careful the path that you want to add if it contains double quote after you restart your cmd rewrite...

Batch Script Loop

Answer : for /l is your friend: for /l %x in (1, 1, 100) do echo %x Starts at 1, steps by one, and finishes at 100. Use two % s if it's in a batch file for /l %%x in (1, 1, 100) do echo %%x (which is one of the things I really really hate about windows scripting) If you have multiple commands for each iteration of the loop, do this: for /l %x in (1, 1, 100) do ( echo %x copy %x.txt z:\whatever\etc ) or in a batch file for /l %%x in (1, 1, 100) do ( echo %%x copy %%x.txt z:\whatever\etc ) Key: /l denotes that the for command will operate in a numerical fashion, rather than operating on a set of files %x is the loops variable (starting value, increment of value, end condition[inclusive] ) And to iterate on the files of a directory: @echo off setlocal enableDelayedExpansion set MYDIR=C:\something for /F %%x in ('dir /B/D %MYDIR%') do ( set FILENAME=%MYDIR%\%%x\log\IL_ERROR.log echo =========================== Search in !FILE...