0

I have a parent folder that contains multiple folders within it. Then, each of these nested folders contains 4 files that make up a GIS shapefile and have different extensions (i.e., ".dbf", ".prj", ".shp", and ."shx"). I am new to coding (outside of R) and do not know whether this can be automated with Python or if I need to run a shell script (I'm working on a windows). I have very rudimentary coding schools so documentation would be great (and/or suggestions of "dummy" sites to read).

Here is an example of the current file structure (showing the four files I want to rename with the subfolder name):

Parent Folder:  "Raptors"
   Subfolder:  "Falco_peregrinus"
       File 1:  "ra03310.dbf"
       File 2:  "ra03310.prj"
       File 3:  "ra03310.shp"
       File 4:  "ra03310.shx"

Here is what I would like the four files to be renamed to:

File 1:  Falco_peregrinus.dbf
File 2:  Falco_peregrinus.prj
File 3:  Falco_peregrinus.shp
File 4:  Falco_peregrinus.shx

Thanks.

1

For a batch file solution

@echo off
    for /d %%a in ("c:\...\Raptors\*") do ren "%%~fa\*.*" "%%~na.*"

For each folder inside the parent one, rename all the files inside the folder to the name of the folder but keeping the extension

for command is used to iterate over the list of folders (/d) under the parent folder. For each of the folders, the replaceable parameter %%a will hold a reference to the subfolder and the code in the do clause is executed for each one.

The code in the do clause executes a ren command, for all the files under the subfolder (%%~fa is the folder being processed with full path), changing its name to the name of the folder (%%~na).

edited The answer is not completely correct. While the basic idea of using only one ren command to rename all the files under each folder is probably the fastest way, the way ren command handles wildcards makes this code fail if the folder name contains dots. To be sure the code will not fail, it is necessary to iterate over the files, renaming each one

for /d %%a in ("c:\...\Raptors\*") do for %%b in ("%%~fa\*") do ren "%%~fb" "%%~nxa%%~xb"

For each folder (%%a), for each file inside the folder (%%b), rename the file to the name of full folder name (%%~nxa) with the extension of the file (%%~xb)

| improve this answer | |
  • 1
    +1, but this will not work if the folder name contains an extension. Probably safer to use ren "%%~fa\*" "%%~na.*" – dbenham Feb 16 '15 at 22:53
  • 1
    @dbenham, at the end, no. I can remove the extension but if the folder name contains a dot it will become a new extension for the rename command and the code will also fail. I liked the brevity of the code, but it is not safe. – MC ND Feb 17 '15 at 7:04
  • If anybody needs a batch line for first subdirectories as well, I tweaked the above into this: for /d %%a in ("C:\...\*") do for /d %%c in ("%%~fa\*") do for %%b in ("%%~fc\*") do ren "%%~fb" "%%~nxa%%~xb" – James Koss Dec 16 '18 at 21:31
1

You could use almost any programming language (probably including R) to do this. Python is a good choice here because it has such friendly syntax.

A extremely simple script that will solve your problem might look like this

import os
import os.path
'''
Given a file name, returns a pair with the name and extension (hello.txt => [hello,txt])
'''
def split_name(file_name):
    return file_name.rsplit('.',1)

'''
Recursively renames files in subdirectories of base_directory so each file is named the subdirectory name plus the extension
WARNING! You will be very sad if you have multiple files with the same extension in any of those folders
def rename_file(base_directory):

    #Get the folder name for base_directory (c:\users\foobar => foobar)
    directory_name = os.basename(base_directory)
    #List the files in base_directory
    for path in os.listdir(base_directory):
        old_name = base_directory + '/' + path
        #If the path points to a file, rename it directory name + '.' + extension
        if os.path.isfile(old_name):
           new_name = base_directory + '/' + directory_name + '.' + split_name(path)[1]
           if not os.path.exists(new_name):
                os.rename(old_name,new_name)
           else:
                print("ERROR:"+new_name+" exists")

        else:
           #If it's a subfolder, recursively call rename files on that directory.
           rename_files(old_name)

Also, I stongly suggest Learn Python The Hard Way by Zed Shaw and Dive Into Python by Mark Pilgrim

| improve this answer | |
0

a simple windows command can solve your problem. Read HELP FOR and the try this in the Windows command line:

for /d %a in (*) do @for %b in (%a\*) do @ren %a\%b %a%~xb

let's analyze it

the first for will iterate over all (*) the directories /d and for each found, passed in %a the second for will iterate over all the files it contains (%a\*) and for each file found %b it will do rename ren it %a\%b with the name of the folder it is contained in %a keeping the same extension it had %~xb.

| improve this answer | |
0

This can be done with batch only, I publish this script only to demonstrate how easy this is in Ruby

# enumerate all subfolders of raptors
Dir.glob("raptors/**/*/") do |folder|
  # remember the prefix
  pre = File.basename(folder)[/.+_/]
  # enumerate all files under this folder
  Dir.glob("#{folder}*.*") do |file|
    File.rename(file,  "#{File.dirname(file)}/#{pre}#{File.basename(file)}")
  end
end
| improve this answer | |
0

There is another answer with python code, this code changed my GIS file names based on folder name very well: Thanks @Martin Evans.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.