开发者

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.


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)


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


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


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜