Programmatically rename open file on Windows
Renaming requires that the file in question was opened with FileShare.Delete sharing.
If that share flag is missing, you can not rename/move the file while it is still open.
Rename a file that multiple processes are trying to use
Windows applications can do this using the SetFileInformationByHandle
function, which allows you to rename the file using the handle you already have open. You probably can't do this natively from Java.
However, a more straightforward solution would be to rename the file (to filename+processing
, for example) before you start processing it. Whichever process successfully renames the file in this way is the one responsible for processing it and eventually renaming it to filename+processed
.
Automatically rename a file if it already exists in Windows way
This will check for the existence of files with tempFileName and increment the number by one until it finds a name that does not exist in the directory.
int count = 1;
string fileNameOnly = Path.GetFileNameWithoutExtension(fullPath);
string extension = Path.GetExtension(fullPath);
string path = Path.GetDirectoryName(fullPath);
string newFullPath = fullPath;
while(File.Exists(newFullPath))
{
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
newFullPath = Path.Combine(path, tempFileName + extension);
}
Rename a file in C#
Take a look at System.IO.File.Move, "move" the file to a new name.
System.IO.File.Move("oldfilename", "newfilename");
Best way to programmatically copy, rename, and paste files in Windows
Hopefully I understood well and this is what you're looking for:
Function Rename-Stuff{
Get-ChildItem -Path "C:\HotFolders" | Foreach {Rename-Item $_.FullName -NewName $_.Name.Replace("_DG","_GWL")}
Foreach($file in (Get-ChildItem -Path "C:\HotFolders")){ Move-Item -Path $file.FullName -Destination "C:\New\Path\Here"}
Start-Sleep 360
&Rename-Stuff}
Please let me know if this is what you were looking for, and if you'd like to understand what happening in this short script block; ill be more than happy to explain it (just dont want to assume you dont already know is all).
Oops, just realized you wanted to copy the items:
Function Copee-Stuff{
Copy-Item -Path "C:\HotFolders" -Recurse -Destination "C:\New\Path\Here"
Foreach($file in (Get-ChildItem -Path "C:\New\Path\Here")){ Rename-Item $file.FullName -NewName $file.Name.Replace("_DG","_GWL")}
Start-Sleep 360
&Copee-Stuff}
Please note: you can also filter by csv, and or any other file extensions provided the -Filter
Parameter.
How to rename a folder in c# which is currently opened by windows explorer
So I'm answering my own question after some further reasearch..
The folder can be renamed using SHFileOperation()
as shown here:
https://learn.microsoft.com/en-us/windows/win32/shell/manage
(whether this uses the 'magic' mentioned by Wouter or not. ;-)
But if there is a windows/.Net API such as System.IO.Directory.Move
, WTF do I need to use the Shell? Not talking about performance ...
Anyway, using SHFileOperation()
is a pain in the a.. using C# since you need to declare all this p-invoke stuff. And in this particular case you need to use different structs for 32 and 64-bit windows, since the packing is different (see https://www.pinvoke.net/default.aspx/shell32.shfileoperation). This is very cumbersome, as usually I'd specify AnyCPU as target. At this point you either need to branch at runtime (very bad indeed), depending whether you are a 64 or 32 bit process, or you build differently for two different targets, which is quite a big impact just to work around the silly explorer.
Regards
Related Topics
What Strategies and Tools Are Useful for Finding Memory Leaks in .Net
Calling JavaScript Function from Codebehind
How to Populate/Instantiate a C# Array with a Single Value
Does It Make Sense to Use "As" Instead of a Cast Even If There Is No Null Check
How to Read and Write from the Serial Port
Write File from Assembly Resource Stream to Disk
Concat All Strings Inside a List<String> Using Linq
How to Implement Ienumerable<T>
How to Cast from Parent Class to Child Class
Difference Between Registerstartupscript and Registerclientscriptblock
Pass Complex Parameters to [Theory]
Change C# Dllimport Target Code Depending on X64/X86
Validateantiforgerytoken Purpose, Explanation and Example
Is Using a Mutex to Prevent Multiple Instances of the Same Program from Running Safe
Has Foreach's Use of Variables Been Changed in C# 5
.Net Hashtable VS Dictionary - Can the Dictionary Be as Fast