<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Script to Copy File in Modelbuilder in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584563#M45867</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif; color: #3d3d3d;"&gt;The most straightforward way would be something like the following, although you may run into trouble with /" (untested):&lt;/P&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif; color: #3d3d3d;"&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import shutil
index = arcpy.GetParameterAsText(0) # get the first input parameter entered into the tool dialog
shutil.copy("c:/chris/Test1/CitizenComments.mxd" , "c:/chris/test2/" + index + "CitizenComments.mxd")&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To better work with paths, use os.path.join:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import shutil, os
index = arcpy.GetParameterAsText(0) # get the first input parameter entered into the tool dialog
shutil.copy("c:/chris/Test1/CitizenComments.mxd" , os.path.join("c:/chris/test2", index + "CitizenComments.mxd")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 01:08:10 GMT</pubDate>
    <dc:creator>DarrenWiens2</dc:creator>
    <dc:date>2021-12-12T01:08:10Z</dc:date>
    <item>
      <title>Script to Copy File in Modelbuilder</title>
      <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584561#M45865</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I have a very simple python script to copy a file from one location to another:&lt;/P&gt;&lt;P&gt;import shutil&lt;/P&gt;&lt;P&gt;shutil.copy("c:/chris/Test1/CitizenComments.mxd" , "c:/chris/test2/CitizenComments.mxd")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I take this script and put it in a model builder model, what change would I have to make to the code so that I could have a parameter in the model where a user could enter an index string to be appended to the front of the file name?&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/194414_pastedImage_0.png" style="max-height: 900px; max-width: 1200px;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;Chrs&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Apr 2016 22:07:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584561#M45865</guid>
      <dc:creator>ChrisHolmes</dc:creator>
      <dc:date>2016-04-08T22:07:23Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Copy File in Modelbuilder</title>
      <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584562#M45866</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Oh ya, as you can tell: new to python &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Apr 2016 22:08:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584562#M45866</guid>
      <dc:creator>ChrisHolmes</dc:creator>
      <dc:date>2016-04-08T22:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Copy File in Modelbuilder</title>
      <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584563#M45867</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif; color: #3d3d3d;"&gt;The most straightforward way would be something like the following, although you may run into trouble with /" (untested):&lt;/P&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif; color: #3d3d3d;"&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import shutil
index = arcpy.GetParameterAsText(0) # get the first input parameter entered into the tool dialog
shutil.copy("c:/chris/Test1/CitizenComments.mxd" , "c:/chris/test2/" + index + "CitizenComments.mxd")&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To better work with paths, use os.path.join:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import shutil, os
index = arcpy.GetParameterAsText(0) # get the first input parameter entered into the tool dialog
shutil.copy("c:/chris/Test1/CitizenComments.mxd" , os.path.join("c:/chris/test2", index + "CitizenComments.mxd")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:08:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584563#M45867</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-12T01:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Copy File in Modelbuilder</title>
      <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584564#M45868</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Darren. I'll play around with your suggestions.&lt;/P&gt;&lt;P&gt;Have a good weekend.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chris&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Apr 2016 22:27:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584564#M45868</guid>
      <dc:creator>ChrisHolmes</dc:creator>
      <dc:date>2016-04-08T22:27:49Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Copy File in Modelbuilder</title>
      <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584565#M45869</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I suggest reading the help about how to &lt;A href="http://desktop.arcgis.com/en/arcmap/latest/analyze/creating-tools/setting-script-tool-parameters.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;set up input and output parameters to scripts.&lt;/A&gt;​ Once that is set up, you can right-click your script tool to expose its parameters in the model. The script needs to handle input and output parameters like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
string = arcpy.GetParameterAsText(0)
import shutil
shutil.copy("c:/chris/Test1/CitizenComments.mxd",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "c:/chris/test2/{}CitizenComments.mxd".format(string))
arcpy.SetParameterAsText(1,&amp;nbsp; "c:/chris/test2/{}CitizenComments.mxd".format(string)))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then, set your script tool with an &lt;EM&gt;input parameter&lt;/EM&gt; of type String and a &lt;EM&gt;derived parameter&lt;/EM&gt; of type File. Then re-add your tool to ModelBuilder and I think you'll get what you're looking for.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Or, you can avoid having to set up a separate script tool by using the very useful Calculate Value model-only tool -- a little Python goes a long way in ModelBuilder! You can get this tool added to your model with a right click.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Expression:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;CopyFile(r"%String%")&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Code Block&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import shutil
def CopyFile(string):
&amp;nbsp; shutil.copy("c:/chris/Test1/CitizenComments.mxd",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "c:/chris/test2/{}CitizenComments.mxd".format(string))
&amp;nbsp; return "c:/chris/test2/{}CitizenComments.mxd".format(string)&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Data Type&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;File&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Set your parameter String as type String and use the magic wand tool to make it a precondition to Calculate Value, and you're good to go!'&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;ChrisTool_tbx("Trump")&lt;/STRONG&gt; will copy your map document to the path&lt;/P&gt;&lt;P&gt;C:\chris\test2\TrumpCitizenComments.mxd&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:08:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584565#M45869</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-12T01:08:13Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Copy File in Modelbuilder</title>
      <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584566#M45870</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Curtis, I'll check this out.&lt;/P&gt;&lt;P&gt;Chris&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Apr 2016 13:20:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584566#M45870</guid>
      <dc:creator>ChrisHolmes</dc:creator>
      <dc:date>2016-04-12T13:20:38Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Copy File in Modelbuilder</title>
      <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584567#M45871</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Curtis, following your advice I was able to get the copy file working using:&lt;/P&gt;&lt;P&gt;&lt;IMG __jive_id="194689" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/194689_pastedImage_0.png" style="max-height: 900px; max-width: 1200px;" /&gt;&lt;/P&gt;&lt;P&gt;If I wanted to modify the code block to first create a folder with the same name as what is appended to the copied CitizenComments.mxd file what would need to be changed? So for example the new file path would be C:\chris\105\105CitizenComments.mxd&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I had tried the following based on the previous copy file code, but obviously I'm missing some stuff:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os
import shutil
def CopyFile(string):
&amp;nbsp;&amp;nbsp; os.mkdir("c:/chris/{}.format(string))
&amp;nbsp;&amp;nbsp; shutil.copy("c:/chris/Test1/CitizenComments.mxd",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "c:/chris/{}/{}CitizenComments.mxd".format(string))
&amp;nbsp;&amp;nbsp; return "c:/chris/{}/{}CitizenComments.mxd".format(string)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:08:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584567#M45871</guid>
      <dc:creator>ChrisHolmes</dc:creator>
      <dc:date>2021-12-12T01:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Copy File in Modelbuilder</title>
      <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584568#M45872</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is how you use a value twice in a format string. The way to debug this kind of thing is to find a Python prompt (the one in ArcMap will do) and type stuff at it:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; "{} {}".format("foo")
Traceback (most recent call last):
&amp;nbsp; File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;
IndexError: tuple index out of range
&amp;gt;&amp;gt;&amp;gt; "{} {}".format("foo", "foo")
'foo foo'
&amp;gt;&amp;gt;&amp;gt; "{} {}".format("foo", "bar")
'foo bar'
&amp;gt;&amp;gt;&amp;gt; "{0} {0}".format("foo")
'foo foo'&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os
import shutil
def CopyFile(string):
&amp;nbsp;&amp;nbsp; os.mkdir("c:/chris/{}.format(string))
&amp;nbsp;&amp;nbsp; shutil.copy("c:/chris/Test1/CitizenComments.mxd",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "c:/chris/{0}/{0}CitizenComments.mxd".format(string))
&amp;nbsp;&amp;nbsp; return "c:/chris/{0}/{0}CitizenComments.mxd".format(string)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.python.org/2/library/string.html#format-specification-mini-language" title="https://docs.python.org/2/library/string.html#format-specification-mini-language" rel="nofollow noopener noreferrer" target="_blank"&gt;7.1. string — Common string operations — Python 2.7.11 documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This is really helpful guide to Python string formatting I have bookmarked:&lt;/P&gt;&lt;P&gt;&lt;A href="https://mkaz.tech/python-string-format.html" title="https://mkaz.tech/python-string-format.html" rel="nofollow noopener noreferrer" target="_blank"&gt;mkaz.tech – Python String Format Cookbook&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:08:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584568#M45872</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-12T01:08:18Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Copy File in Modelbuilder</title>
      <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584569#M45873</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks again for the help Curtis, this is super. Also thanks for the link!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Apr 2016 14:17:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584569#M45873</guid>
      <dc:creator>ChrisHolmes</dc:creator>
      <dc:date>2016-04-13T14:17:30Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Copy File in Modelbuilder</title>
      <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584570#M45874</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So I have the above code working to copy multiple files, but it doesn't work to copy a file geodatabase. Any suggestions for how to do this?&lt;/P&gt;&lt;P&gt;Thanks very much for all the help,&lt;/P&gt;&lt;P&gt;Chris&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Apr 2016 17:02:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584570#M45874</guid>
      <dc:creator>ChrisHolmes</dc:creator>
      <dc:date>2016-04-14T17:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Copy File in Modelbuilder</title>
      <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584571#M45875</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;either shutil.copytree() or arcpy.Copy_management() should work.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Apr 2016 17:36:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584571#M45875</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2016-04-15T17:36:15Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Copy File in Modelbuilder</title>
      <link>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584572#M45876</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Curtis!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I first tried arcpy.Copy_management() and was able to get it working but my efforts to get it to use the parameter so the destination could change based on the user input failed.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So then I tried shutil.copytree() and it looks like it works:&lt;/P&gt;&lt;P&gt;shutil.copytree("x:/GIS/LUAM/DEV/TEMPLATE/data_for_item.gdb","c:/chris/{0}/data_for_item.gdb".format(string))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Have a good weekend.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Apr 2016 22:06:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-copy-file-in-modelbuilder/m-p/584572#M45876</guid>
      <dc:creator>ChrisHolmes</dc:creator>
      <dc:date>2016-04-15T22:06:31Z</dc:date>
    </item>
  </channel>
</rss>

