Ben L
Ben L

Reputation: 7068

Renaming a file on a remote file server in C# / Python

I need to rename a whole heap of files on a Windows file server - I don't mind what language I use really as long it's quick and easy!

I know it's basic but just to clarify - in pseudo-code...

server = login (fileserver, creds)

foreach (file in server.navigateToDir(dir))
    rename(file)

I know how to do this in Python/C# if I was a local user but have no idea if it's even possible to do this remotely using Python. I've searched for code snippets/help but have found none yet.

Thanks.

Upvotes: 2

Views: 2361

Answers (4)

Georges Martin
Georges Martin

Reputation: 1208

Have a look at pyfilesytem, it provides a consistent interface for local and remote filesystems.

Upvotes: 1

Ian Mercer
Ian Mercer

Reputation: 39277

You could also use PSEXEC to execute the code remotely on the server if you need the performance of locally executed code. See http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

Upvotes: 1

Ben L
Ben L

Reputation: 7068

The following renames a file in each of the sub-directories of the folder path given. It renames the file from the given filename (eg."blah.txt") to foldername+extension.

NB. Z can be either a local or network drive (ie. if folder is on file server map network drive to it).

For example from a shell...

python renamer.py "Z:\\FolderCollectionInHere" blah.txt csv

... will rename a file 'blah.txt' in each immediate sub-directory of "Z:\FolderCollectionHere" to .csv.

import os
import sys

class Renamer:
    def start(self, args):
        os.chdir(args[1])
        dirs = os.listdir(".")

        for dir in dirs:
            try:
                os.rename(dir + "\\" + args[2], dir + "\\" + dir + "." + args[3])
                print "Renamed file in directory: " + dir
            except Exception:
                print "Couldn't find file to rename in directory: " + dir

Renamer().start(sys.argv)

Upvotes: 0

nos
nos

Reputation: 229058

Use \\servername\sharename\somefile.foo for filenames - provided you have access to connect to it and are running on windows.

You could also map up a network drive and treat it as any other local drive (y:\sharename\somefile.foo)

Upvotes: 1

Related Questions