I am recovering Stata following a Windows upgrade. I have a list of my packages generated from ado dir in the following format:
[1] package mdesc from http://fmwww.bc.edu/RePEc/bocode/m
'MDESC': module to tabulate prevalence of missing values
[2] package univar from http://fmwww.bc.edu/RePEc/bocode/u
'UNIVAR': module to generate univariate summary with box-and-whiskers plot
[3] package tabmiss from http://www.ats.ucla.edu/stat/stata/ado/analysis
tabmiss. Shows tabulation of number of missing and non-missing values
I have many packages and would like to reinstall them without having to designate each directory/url via net cd. While using net cd along with net install or ssc install along with package names in a loop is trivial (as below), it would seem that an automated method for this task might be available.
net cd http://www.ats.ucla.edu/stat/stata/ado/analysis
local ucla tabmiss csgof powerlog ldfbeta
foreach x of local ucla {
net install `x'
}
To my knowledge, there is no built-in or automated method of tracking and managing your installed packages outside of what is available through ado or net.
I would also tend to agree with #Nick Cox that this task seems strange and I can't imagine how a new Stata install or reinstall could know what was installed previously, but I find the question interesting for other reasons.
The main reason being for users who have Stata installed on multiple machines who need the same packages on both machines. I faced a similar issue when I purchased a new computer and installed Stata but wanted all of the packages I use to be available as well. Outside of moving the ado directory or selected contents I'm not aware of any quick solution.
Here it would be possible to use the output of ado dir on one machine to determine what you need to install on a second machine with a new Stata install.
The method you propose using a foreach loop could save you time from having to type in or copy/paste a lot of packages and URLs. At the same time however, this is only beneficial if you have many packages from only a few repositories because you will need to net cd to the URL each time as you show in your example.
An alternative solution is the programmatic solution. As you know, ado dir will list each installed package, the URL and a short description of the package. Using this, a log file, and the built in I/O functionality, a short program could be written to automate the process and dynamically build a do file that contains the commands to install the already installed packages.
The code below generates a do file containing commands (in this case, net describe package, from(url)) for each package I have installed on my computer.
clear *
tempfile log1
log using "`log1'", text name(mylog)
ado dir
log close mylog
tempname logfile
file open `logfile' using "`log1'", read
file read `logfile' line
file open dfh using "path/to/your/dofile.do", write replace
local pckage "package"
while r(eof) == 0 {
if `: list pckage in line' {
local packageName : word 3 of `line'
local dirName : word 5 of `line'
di "`packageName' `dirName'"
file write dfh "net describe `packageName', from(`dirName')"
file write dfh _newline
}
file read `logfile' line
}
file close `logfile'
file close dfh
In the above code, I create a temp file to write a .txt log file to and store the contents of ado dir in that file.
Then, I open the log file using file open and read it line by line in the while loop.
Above the loop, I'm creating a do file at /path/to/your/dofile.do to hold the output of the loop - the dynamically created commands relating to the installed packages on my machine.
The loop will iterate so long as r(eof) = 0, where r(eof) is an end of file marker. I use an if statement to sort out lines of the log file which contain the word package, as I'm only interested in those lines with the package name and URL in them.
Inside of the if block, I parse the local macro line to pull the package name and the URL/directory name.
this is important: this section of code assumes that the 3rd and 5th words in the macro will always be the package name and URL respectively - Confirm this from the output of ado dir before executing.
You will also need to change the command that is being written to the file handle dfh inside of the loop to what you want (net install, etc) when you are ready to execute.
For more help on using file, locals, and tempfiles execute any of the following in Stata:
help file
help extended_fcn
help macrolists
There may be nicer ways to parse the contents of ado dir but this has worked for me. And of course I'd always advise that you take the time to understand what the code is doing so that you can make any necessary tweaks to fit your particular situation.
So I know there are lots of answers on this already, though it seemed like all of them basically suggested that the command should be broken up in some way. In my case I am not sure how this is possible... it is not even a long command. So does anyone have some suggestions on this?
:myLoop
call timeout 3600
cd c:\automation
call ant_env.bat
call ant -f regression.xml upgrade_sequence REM apparently this line is too long...
GOTO myLoop
I have even tried shortening the file name to just regr.xml.
This is on Windows Sever 2008 R2 Enterprise
This really depends on what you have inside ant_env.bat, you have to keep in mind that PATH is limited to 260 characters, in case you append it eg.
set PATH=%PATH%;%JAVA_HOME%
and the loop is called continuously, it can lead into a situation where the limit is reached.
You can use (if not defined) as can be seen here : "Input line is too long" error in BAT File
I am looking for a solution i have been trying to research for a little while.
I am making a .bat program to use at my work for templates to be placed in a certification document. Trying to speed up the process of copying and pasting .
Currently i am using notepad, highlighting and CRTL+C / V to copy the template.
I have built a .bat program that will do it in a selection window (enter 1-8 ect ect)
The only problem i am having is i was wondering if i can place the template inside the scripting of the .bat, and it will copy it to the clipboard.
example of template (must have spaces and breaks):
~~~~~~~~~~~~~~~~~~~~~~~
Processed OK:
Surface Hardness Tested HR:
Name
03/21/2015
Company
~~~~~~~~~~~~~~~~~~~~~~~~~~
i know of the "clip" command, but i can only find ways to copy stuff from a .txt. document, and i would like to avoid having 8+ .txt documents for every template. (e.g: clip > temp1.txt)
So is there a way to copy a prefab'ed template from within the .bat
example:
if %TYPE%==1 goto temp1
:temp1
clip ???????
Any help would be great!
Thanks!
Sure you can. You can create arbitrarily complex streams to be fed into the clip program, such as:
#echo off
(
echo hello
echo goodbye
) | clip
If you run a cmd file containing those commands, then open up a notepad document and use CTRL-V, it will paste the text:
hello
goodbye
Just replace the two echo commands with whatever code you need to generate the template.
I'm trying to read Windows CMD's stdout with AutoHotkey. For example, I'd like to have the output of the setconsole command inside AHK stored in a variable. I already achieved it a while ago, which makes me all the more perplex why it's not working now.
In the AHK forums, there's a rather old thread about CMDret, a DLL based functionality to do exactly what I want. The first problem was to find a working download for it, since all the links in the post were dead. Google gave me another site, hosting v3.1.2. Altough there seems to be a newer one (v3.2.1 respectively 4d Beta), I checked it out and tested a simple example:
msgbox % CMDret(COMSPEC " /C set")
CMDret(CMD)
{
VarSetCapacity(StrOut, 10000)
RetVal := DllCall("cmdret.dll\RunReturn", "str", CMD, "str", StrOut)
Return, %StrOut%
}
Unfortunately, the MsgBox contained nothing. I then checked out RetVal which had a value of 0; and the attached readme says:
If the function fails, the return value is zero.
Further down, it says:
Note: only 32 bit console applications will currently work with the
this dll version of CMDret (v3.1.2 or lower). Calls that require
command.com will likely not produce any output and may crash. To avoid
this I have included a file named "cmdstub.exe" with the download (in
the Win9x folder). This file should be used when calling 16 bit
console applications to enable returning output.
In conclusion, I am not sure what the problem is. My machine is running on 64 bit. But is the corresponding clause in the readme supposed to solely exclude 16 bit systems or does it rather only include 32 bit?
If the computing architecture is probably not the problem, then what could be?
What I am looking for is either one of the following:
Can I fix the problem and keep using v3.1.2?
Has anyone a working source (or even a local copy) of a newer version I could check out?
Is there another approach [library, .ahk code, etc.] I could use for my purpose? (preferably similar, because CMDret seems very straightforward)
If you don't need a live output, you could use the cmd box itself to save a text file of itself and then you could have autohotkey detect when the console's PID finished (using the returned result of runwait and read the outputted file into memory.
So you could do this in your run command (just a regular cmd parameter):
ipconfig > myoutput.txt
exit
Now you have a text file with the ipconfig output in it.
OR
you could do the same thing, but instead of outputting to a text file, you could output to the clipboard, like this:
ipconfig | clip
Then, since the output is on the clipboard, you can easily grab it into autohotkey.
New recommended 2 ways of doing as of Nov 2019 - https://www.autohotkey.com/docs/FAQ.htm#output:
How can the output of a command line operation be retrieved?
Testing shows that due to file caching, a temporary file can be very fast for relatively small outputs. In fact, if the file is deleted immediately after use, it often does not actually get written to disk. For example:
RunWait %ComSpec% /c dir > C:\My Temp File.txt
FileRead, VarToContainContents, C:\My Temp File.txt
FileDelete, C:\My Temp File.txt
To avoid using a temporary file (especially if the output is large), consider using the
Shell.Exec() method as shown in the examples for the Run command.
Example for the Run command - https://www.autohotkey.com/docs/commands/Run.htm#StdOut:
MsgBox % RunWaitOne("dir " A_ScriptDir)
RunWaitOne(command) {
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /C " command)
return exec.StdOut.ReadAll()
}
Note: the latter method (shell.Exec) will cause quick display of a cmd window.
You can reduce the duration of its appearance by putting these lines at the top of your script, which will also cause the flickering to happen only once the first time you call the cmd command. From https://autohotkey.com/board/topic/92032-how-to-hide-a-wscript-comspec-window/:
;Following 2 lines : the cmd window will flash only once and quickly
DllCall("AllocConsole")
WinHide % "ahk_id " DllCall("GetConsoleWindow", "ptr")
How about this script, StdoutToVar ?
It has support for 64bit consoles.
http://www.autohotkey.com/board/topic/15455-stdouttovar/page-7
This has been bugging me for some time now - and finally this works !
The only prerequisite for this is MS sqlcmd.exe, a database called AHK_Dev
and of course AHK_DBA to read the value when you wish to make use of it.
PS. make sure you replace {yourDir} and {yourServer} with you own values!
USE AHK_DEV
CREATE TABLE [dbo].[AHK_DOS]([dos_out] [varchar](max) NULL) ON [PRIMARY];
insert into ahk_dos select 'empty'
Create the follow script ... call it dos_out.bat
#echo off
if "%1" == "" (
set v_cmd=""
) else (
set v_cmd=%1
)
set v_cmd=%v_cmd:~1,-1%
SETLOCAL ENABLEDELAYEDEXPANSION
if "!v_cmd!" == "" (
set v_cmd="echo ... %COMPUTERNAME% %USERNAME% %DATE% %TIME%"
set v_cmd=!v_cmd:~1,-1!
)
set v_data=""
FOR /F "usebackq delims=¬" %%i in (`!v_cmd!`) do (
set v_data="!v_data:~1,-1!%%i~"
)
set q_cmd="set nocount on;update ahk_dos set dos_out=N'!v_data:~1,-1!'"
"{yourDir}\Microsoft SQL Server\90\Tools\Binn\sqlcmd.exe" -S {yourServer} -E -d ahk_dev -Q !q_cmd! -W
set q_cmd="set nocount on;select len(dos_out) as out_len, dos_out from ahk_dos"
"{yourDir}\Microsoft SQL Server\90\Tools\Binn\sqlcmd.exe" -S {yourServer} -E -d ahk_dev -Q !q_cmd! -W -w 8000
pause
you can run it from AHK using...
dosCmd2db(c) {
runwait, {yourDir\}dos_out.bat "%c%", , , dospid
msgbox %dospid% closed
}
dosCmd2db("")
dosCmd2db("echo This is a test")
dosCmd2db("dir")
As the same field is being updated each time, you would clearly need to do something between each one to make this example useful!
Try it, and let me know how you get on
Regards, Geoff
Just an update to #amynbe answer.
MsgBox % RunWaitOne("dir " A_ScriptDir)
RunWaitOne(command) {
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /C " command)
return exec.StdOut.ReadAll() }
Note: the latter method (shell.Exec)
will cause quick display of a cmd window. You can reduce
> the duration of its appearance by putting these lines at the top of
> your script, which will also cause the flickering to happen only once
> the first time you call the cmd command.
You can just do this below to hide cmd and avoid flashing.
MsgBox % RunWaitOne("dir " A_ScriptDir)
RunWaitOne(command) {
DetectHiddenWindows On
Run %ComSpec%,, Hide, pid
WinWait ahk_pid %pid%
DllCall("AttachConsole", "UInt", pid)
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /C " command)
DllCall( "FreeConsole" )
return exec.StdOut.ReadAll()
}
I found a script only solution that works for AutoHotKey L 64bit at:
http://www.autohotkey.com/board/topic/67687-ahkahk-lusing-wsh-to-interact-with-command-line-progs/
After playing with it a bit I was able to capthre the entire output of a 40k text file that I listed using the DOS Type command. There is a demo that shows how you can interact with time command, which is nice if you need limited two way interaction with a dos command or batch script.
I read through the docs and used the commands outlined however for some reason I dont seem to be getting any symbols, just a series of "unknowns" in the function column of the summary table for everything except the topmost set of the app I was trying to debug.... I set the enviromental variable to the microsoft server and the direcories containg the pdb's for the app. I also made sure to select the "Load Symbols" item before bringing up the summary table.
Arcording to the info I was reading it should take some time for the tabl to display while it loads the symbols, however for me the table displayed almost instantly and only the top most items in sprite.exe->sprite.exe had function names, the othe rows for function were either blank or "unknown"
Im using Vista SP1.
This is the batch file I used. Did I do anything that would prevent the sybols being loaded?
REM start profiler
xperf -on PROC_THREAD+LOADER+INTERRUPT+DPC+PROFILE^
-stackwalk profile -minbuffers 16 -maxbuffers 1024 -flushtimer 0^
-f tmp.etl
REM run the app we want to profile
sprite.exe
REM stop
xperf -d profile.etl
REM set symbol path
set _NT_SYMBOL_PATH = ^
C:\Projects\C++\fl lib\bin;^ REM dlls
C:\Projects\C++\fl lib\samples\bin;^ REM main exe
SRV*c:\symbols*http://msdl.microsoft.com/download/symbols
REM display profile
xperf profile.etl
What version of dbghelp.dll is it using? I've had trouble getting PDB symbols working with kernrate and other utilities when using the version of dbghelp.dll that is installed in %SystemRoot%\system32.
You may need to copy dbghelp.dll and symsrv.dll from a recent version of Debugging Tools for Windows into a directory where XPerf can find it.
Also, you need to remove the space before the '=', or else you're defining the "_NT_SYMBOL_PATH " variable (including the trailing space in the name).
Including comments at the end of a line is not going to work either. A line continuation character such as '^' typically needs to be the last character on the line. When I ran that statement (on XP), I ended up with "_NT_SYMBOL_PATH " equal to " C:\Projects\C++\fl lib\bin; REM dlls" and an error about the next line.
Try setting the
TRACE_FORMAT_SEARCH_PATH
environment variable. If that doesn't work, you may have to manually extract the TMF files from your PDBs using TracePdb.exe (or at least use a regular path instead of a SYM* path). This is by far the most annoying part of using ETL traces / XPerf