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

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)

Related

Access Problem with \bin\x86\Debug\AppX\Assets\

the situation is as follows:
I want to put pictures i get to a file in my assets.
I want to get the folder with:
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(Directory.GetCurrentDirectory());
and the create a file there with:
File.Create(folder.Path + #"\" + "Assets" + #"\" + storageItem.DisplayName + storageItem.FileType);
But it throws an System.UnauthorizedAccessException at me when i try to create the file. I need a permanent fix for this as other users (some without administrative powers) will need to use the program i am writing.
Thank you in advance
that's correct because you can not alter or create something in installed dir only option available to this dir is reading files that exist there.
if you want to keep some file in-app local or temp directory then you can use
AppliacationData.Currunt.LocalFolder/TemproryFolder etc to keep your file
Its likely your current directory is set to somewhere which required administrator permission. You can change your current directory:
https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getcurrentdirectory?view=netcore-3.1
The below looks like a fairly solid overview on where you should store things and has plenty of code examples
https://www.codeproject.com/Tips/370232/Where-should-I-store-my-data

Exception: "Access to the path ... is denied"

I'm working on a program in C#, a part of which is to create a directory in the Application.StartupPath folder and then write a text file inside it using System.IO.File.WriteAllText(). My issue is that my program crashes, throwing an UnauthorizedAccessException and telling me that "Access to the path is denied", which is, well, odd, considering that it crashes regardless of the directory from which I am running the program, whether it be running from my cloud folders, Desktop, My Documents, etc, and even despite running it as Administrator in any of those directories.
The path from which I'm debugging it is C:\Users\Jeff\Google Drive\Documents\Visual Studio 2013\Projects\Palobo\Palobo\bin\Debug. It is using System.IO;, and the code I'm using includes:
Directory.CreateDirectory(Application.StartupPath);
File.WriteAllText(Application.StartupPath, "Password=" + x);
where x is some String data entered by the user.
The error I get is:
Access to the path 'C:\Users\Jeff\Google Drive\Documents\Visual Studio 2013\Projects\Palobo\mzdon29 is denied.
(mzdon29 being an encrypted result of jwalk96).
Does anyone have any ideas as to why I'm encountering this problem? Thanks!
Application.StartupPath is a folder (where your application is started from). Try to specify an exact filename inside that folder:
File.WriteAllText(Application.StartupPath + "\\MyFile.txt", "Password=" + x);
Let's look at this code:
Directory.CreateDirectory(Application.StartupPath);
File.WriteAllText(Application.StartupPath, "Password=" + x);
You're trying to create a directory that already exists, and then you're trying use the directory as a file name! You need to add something to end of the path, so that you're working with a new folder and file.
Also, using the StartupPath for this is poor practice in the first place. You can create a shortcut that sets the startup path to anywhere. But specifically, it's common for the default StartupPath to be somewhere under the Program Files folder. Items under this folder are read only to standard users by default. Instead, you should look at using the Application Data folder, like so:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Finally, this sure looks like it's saving a password in plain-text. Do I really need to go over how bad that is? You shouldn't even save passwords encrypted (hashing is different than encryption), and this is one of those things that's so important you shouldn't even do it for testing/learning/proof of concept code.

Environment.SpecialFolder.ApplicationData returns the wrong folder

I have a strange problem: my .NET 4.0 WPF application is saving data to the ApplicationData folder.
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\myProgram\\";
99.9% of the cases are working great, but on some computers it returns the wrong folder - instead of returning the user folder it returns another folder:
C:\Users\<user>\AppData\Roaming\myProgram\ --correct
C:\Users\s\AppData\Roaming\myProgram\ --wrong
The wrong folder has no write/read permission so my program doesn't work.
It seems the program is running under a different user, but if I check the Task Manager the user is the logged one.
The problem seems to be occurring with domain users with few permissions.
Do you also create a text file to write?
If so save a file such as:
String path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filePath = Path.Combine(path, "filetowrite.log"); // Handles whether there is a `\` or not.
if (File.Exists(filePath))
{
......................
}
Note also before doing any file operations, one should check if directory exists.

How to download the files from different machines within the local network using c#

I have requirement where in i want to download the files from different machines within the local network using c#, later i will do certain processing on the downloaded files and i want to upload the files back to the respective machine from where i downloaded the files, please can i know what would be the best generic approach to achieve this.
You can use normal windows shares to accomplish this. Just share the folder and use the UNC Path to copy the file to and from. i.e.
//Copy From the share to the processing machine and swap the paths around to copy back
File.Copy(#"\\ComputerX\Share\MyFileToCopy.dat",#"c:\MyDumpFolder\MyCopiedFile.Dat");
if you need to authenticate first you can run the below method stub (taken from this so question)
private void Open_Remote_Connection(string strComputer, string strUserName, string strPassword)
{
System.Diagnostics.ProcessStartInfo ProcessStartInfo = new System.Diagnostics.ProcessStartInfo();
ProcessStartInfo.FileName = "net";
ProcessStartInfo.Arguments = "use \\\\" + strComputer + "\\c$ /USER:" + strUserName + " " + strPassword;
ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
System.Diagnostics.Process.Start(ProcessStartInfo);
System.Threading.Thread.Sleep(2000);
}
if the files are just on there hard drive. Make the folder they are in a public drive then you can open the file using its address

How to Read texfile etch.. from Application folder where Application is Installed?

I Added textfile on my Application Folder where the application is also located.
I having a problem where to locate and read the content of the texfile regarding what location the users specify the application to be installed.
Example:
CASE 1: If Application Installed on C
Get the path of: C:\Textfile.txt
CASE 2:If Application Installed on Program files
Get the path of C:\Program Files\Default Company Name\Textfile.Text
Thanks in Regards.
i am not sure if this is what you wanted
string StartPath = Application.StartupPath; //or what SilverbackNet suggested
string FileName = "YourFileName.txt"
Textreader tr = new StreamReader(StartPath+ "\\" + FileName);
Console.WriteLine(tr.ReadLine());
tr.Close();
System.Windows.Forms.Application.ExecutablePath is pretty much the go-to for this particular situation, especially for loading libraries. Please look at System.Environment.GetFolderPath(SpecialFolder.*) for storing data, however, or you'll probably run afowl of stricter permission problems at some point.

Categories

Resources