<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Using Stata for Windows shell scripting</title>
	<atom:link href="http://enoriver.net/index.php/2009/06/11/using-stata-for-windows-shell-scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://enoriver.net/index.php/2009/06/11/using-stata-for-windows-shell-scripting/</link>
	<description>computing for fun and profit</description>
	<lastBuildDate>Wed, 11 Aug 2010 00:24:38 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Gabi Huiber</title>
		<link>http://enoriver.net/index.php/2009/06/11/using-stata-for-windows-shell-scripting/comment-page-1/#comment-1106</link>
		<dc:creator>Gabi Huiber</dc:creator>
		<pubDate>Wed, 17 Jun 2009 14:47:17 +0000</pubDate>
		<guid isPermaLink="false">http://enoriver.net/?p=728#comment-1106</guid>
		<description>Thank you for a fine example of the use of `c(os)&#039;. I&#039;ve been experimenting with both Linux and FreeBSD with a mind to switching away from the Windows desktop one of these days. The impetus for that came from the early reviews of Vista. But it&#039;s a tough call. My Windows XP Pro machine works fine: it has required by far the least tinkering. BitDefender keeps is safe and Diskeeper has eliminated hard drive fragmentation. That took care of the only two complaints I ever had about Windows. In addition, there are a few things that I must run under Windows right now: iTunes and Skype come to mind. But the Unix command line opened up a world of options and I&#039;m drawn. So the compromise I cooked up after I wrote this post would be this: Instead of &quot;shelling out&quot; piecemeal with !, I made Stata write one .bat file and ran the whole thing all at once with winexec. That worked, so I decided that the next step would be to make Stata write a shell script that I could run via Cygwin somehow. I&#039;ll post it here when it&#039;s done. I do agree with your reasons to favor Stata alternatives over shell commands when they&#039;re available.</description>
		<content:encoded><![CDATA[<p>Thank you for a fine example of the use of `c(os)'. I've been experimenting with both Linux and FreeBSD with a mind to switching away from the Windows desktop one of these days. The impetus for that came from the early reviews of Vista. But it's a tough call. My Windows XP Pro machine works fine: it has required by far the least tinkering. BitDefender keeps is safe and Diskeeper has eliminated hard drive fragmentation. That took care of the only two complaints I ever had about Windows. In addition, there are a few things that I must run under Windows right now: iTunes and Skype come to mind. But the Unix command line opened up a world of options and I'm drawn. So the compromise I cooked up after I wrote this post would be this: Instead of "shelling out" piecemeal with !, I made Stata write one .bat file and ran the whole thing all at once with winexec. That worked, so I decided that the next step would be to make Stata write a shell script that I could run via Cygwin somehow. I'll post it here when it's done. I do agree with your reasons to favor Stata alternatives over shell commands when they're available.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric A. Booth</title>
		<link>http://enoriver.net/index.php/2009/06/11/using-stata-for-windows-shell-scripting/comment-page-1/#comment-1101</link>
		<dc:creator>Eric A. Booth</dc:creator>
		<pubDate>Wed, 17 Jun 2009 01:04:30 +0000</pubDate>
		<guid isPermaLink="false">http://enoriver.net/?p=728#comment-1101</guid>
		<description>I use Stata do-files for linux scripting (or Mac OSX via Terminal) much in the same way that you describe for Windows....just replace your !xcopy commands with !cp and change the !rmdir command to !rm (or !rm -d) .   Also, instead of copying the file and then deleting the file, you could use the !mv command in to simply &#039;move&#039; the files.

One thing I&#039;d found is that as Stata and Stata users have expanded the files &amp; directory tools, you can do most of this stuff within Stata without &#039;shelling out&#039; to the OS.  The advantage of using the Stata commands, rather than the OS commands, would be that your do-file would be cross-platform compatible (except for the file paths, which I&#039;ll talk about later).....not that this is something that everyone is worried about, but I work in a multi-platform environment, so I like to think about it for these types of set-ups...so, I guess your posting gives me a reason to think this through a bit.

So, you could use the Stata commands -copy- , -erase-, -rmdir- directly from the do-file rather than the OS-specific commands after a &quot;!&quot;  . 
The remaining issue for cross-platform compatibility of your script above would be the file paths.  This makes me think that you could ask Stata to detect the OS (Windows or Mac) at the start of the do-file and then adjust accordingly, here&#039;s an example using your code:

// FIRST, DETECT THE OS, THEN CHOOSE THE APPROPRIATE FILE PATHS
if &quot;`c(os)&#039;&quot; == &quot;MacOSX&quot; {
       local data_from &quot;/users/username/data/&quot;
       local data_to &quot;\\nairobi\archive\\&quot;
       local data_folder1 experiment
}
if &quot;`c(os)&#039;&quot; == &quot;Windows&quot; {
         local data_from &quot;D:\data\\&quot;
         local data_to &quot;\\nairobi\archive\\&quot;
         local data_folder1 experiment
}


// assemble `from_here&#039;, `to_here&#039; file paths
foreach where in from to {
  local `where&#039;_here &quot;`data_`where&#039;&#039;`data_folder1&#039;&quot;  
/*
                    ---&gt;        I think you could use the -pathjoin()- command too..but I havent tried it (?)  
*/
  di &quot;path `where&#039;: ``where&#039;_here&#039;&quot; // this is just for checking
}

// now move stuff
copy &quot;`from_here&#039;&quot; &quot;`to_here&#039;&quot; , replace    //--&gt; using the Stata command instead of OS command
erase &quot;`from_here&#039;&quot;  // --&gt;  You can use -rmdir- if the folder is empty

//Now, it should work on both MacOS/Linux &amp; Windows.</description>
		<content:encoded><![CDATA[<p>I use Stata do-files for linux scripting (or Mac OSX via Terminal) much in the same way that you describe for Windows....just replace your !xcopy commands with !cp and change the !rmdir command to !rm (or !rm -d) .   Also, instead of copying the file and then deleting the file, you could use the !mv command in to simply 'move' the files.</p>
<p>One thing I'd found is that as Stata and Stata users have expanded the files &amp; directory tools, you can do most of this stuff within Stata without 'shelling out' to the OS.  The advantage of using the Stata commands, rather than the OS commands, would be that your do-file would be cross-platform compatible (except for the file paths, which I'll talk about later).....not that this is something that everyone is worried about, but I work in a multi-platform environment, so I like to think about it for these types of set-ups...so, I guess your posting gives me a reason to think this through a bit.</p>
<p>So, you could use the Stata commands -copy- , -erase-, -rmdir- directly from the do-file rather than the OS-specific commands after a "!"  .<br />
The remaining issue for cross-platform compatibility of your script above would be the file paths.  This makes me think that you could ask Stata to detect the OS (Windows or Mac) at the start of the do-file and then adjust accordingly, here's an example using your code:</p>
<p>// FIRST, DETECT THE OS, THEN CHOOSE THE APPROPRIATE FILE PATHS<br />
if "`c(os)'" == "MacOSX" {<br />
       local data_from "/users/username/data/"<br />
       local data_to "\\nairobi\archive\\"<br />
       local data_folder1 experiment<br />
}<br />
if "`c(os)'" == "Windows" {<br />
         local data_from "D:\data\\"<br />
         local data_to "\\nairobi\archive\\"<br />
         local data_folder1 experiment<br />
}</p>
<p>// assemble `from_here', `to_here' file paths<br />
foreach where in from to {<br />
  local `where'_here "`data_`where''`data_folder1'"<br />
/*<br />
                    ---&gt;        I think you could use the -pathjoin()- command too..but I havent tried it (?)<br />
*/<br />
  di "path `where': ``where'_here'" // this is just for checking<br />
}</p>
<p>// now move stuff<br />
copy "`from_here'" "`to_here'" , replace    //--&gt; using the Stata command instead of OS command<br />
erase "`from_here'"  // --&gt;  You can use -rmdir- if the folder is empty</p>
<p>//Now, it should work on both MacOS/Linux &amp; Windows.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
