Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7791757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:59:17+00:00 2026-06-01T21:59:17+00:00

I am trying to: Loop through a bunch of files makes some changes Copy

  • 0

I am trying to:

  1. Loop through a bunch of files
  2. makes some changes
  3. Copy the old file to a sub directory. Here’s the kicker I don’t want to overwrite the file in the new directory if it already exists. (e.g. if “Filename.mxd” already exists, then copy and rename to “Filename_1.mxd”. If “Filename_1.mxd” exists, then copy the file as “Filename_2.mxd” and so on…)
  4. save the file (but do a save, not a save as so that it overwrites the existing file)

it goes something like this:

for filename in glob.glob(os.path.join(folderPath, "*.mxd")):
    fullpath = os.path.join(folderPath, filename)

    mxd = arcpy.mapping.MapDocument(filename)

    if os.path.isfile(fullpath):
        basename, filename2 = os.path.split(fullpath)


    # Make some changes to my file here

    # Copy the in memory file to a new location. If the file name already exists, then rename the file with the next instance of i (e.g. filename + "_" + i)

    for i in range(50):
        if i > 0:
            print "Test1"
            if arcpy.Exists(draftloc + "\\" + filename2) or arcpy.Exists(draftloc + "\\" + shortname + "_" + str(i) + extension):
                print "Test2"
                pass
            else:
                print "Test3"
                arcpy.Copy_management(filename2, draftloc + "\\" + shortname + "_" + str(i) + extension)
    mxd.save()

So, 2 things I decided to do, was to just set the range of files well beyond what I expect to ever occur (50). I’m sure there’s a better way of doing this, by just incrementing to the next number without setting a range.

The second thing, as you may see, is that the script saves everything in the range. I just want to save it once on the next instance of i that does not occur.

Hope this makes sense,

Mike

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:59:19+00:00Added an answer on June 1, 2026 at 9:59 pm

    thanks to Maty suggestion above, I’ve come up with my answer. For those who are interested, my code is:

        result_name = filename2
        print result_name
        i = 0
    
        # Check if file exists
        if arcpy.Exists(draftloc + "\\" + result_name):
            # If it does, increment i by 1
            i+=1
            # While each successive filename (including i) does not exists, then save the next filename
            while not arcpy.Exists(draftloc + "\\" + shortname + "_" + str(i) + extension):                
                mxd.saveACopy(draftloc + "\\" + shortname + "_" + str(i) + extension)            
        # else if the original file didn't satisfy the if, the save it.
        else:           
            mxd.saveACopy(draftloc + "\\" + result_name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through some 50-odd files in a directory. Each file
I am trying to loop through a directory of text files and combine them
I am trying to loop through files in a specified directory. But I can't
I am trying to loop through each line in a file and find and
I am trying to loop through this external JSON file(locally stored), but I cannot
I'm trying to loop through a text file with a list of urls and
So I have an XML file that I am trying to loop through in
I am trying to loop through only a specific sub array in PHP with
Im trying to loop through a json files' object array to access its variables'
I'm trying to loop through a bunch of checkboxes i have created on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.