4
1
From wikipedia, it is told that robocopy will skip copying files in open status.
However, when it is under test for robocopy behaviour, the robocopy still copy the opened file by the most simple text editor in windows. Why?
4
1
From wikipedia, it is told that robocopy will skip copying files in open status.
However, when it is under test for robocopy behaviour, the robocopy still copy the opened file by the most simple text editor in windows. Why?
5
First, as @fejyesynb correctly noted, Notepad doesn't keep an active file handle – it opens the file, quickly reads (or writes) the data, and closes the file again. The data is on screen, but the file is actually closed the whole time.
Second, Windows has inherited from MS-DOS the concept of "share modes" as a simple form of file locking. When opening a file you can choose whether to share it for read/write, for only reading, or not at all.
For example, if your program (robocopy) wants to open the file for reading (FileAccess.Read), it will only succeed if all existing file handles allow the 'read' share mode (or if there aren't any open file handles at all). But if the file was opened with "share none", then you'll get "File in use" if you try to open it for any purpose.
You can perform this in PowerShell, by calling the low-level .NET System.IO.File.Open() function:
$fh = [System.IO.File]::Open($path,
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::Read,
[System.IO.FileShare]::None)
The 4th parameter can be any System.IO.FileShare enum value, for example:
[System.IO.FileShare]::None
– share nothing[System.IO.FileShare]::Read
– share read (block write/delete)[System.IO.FileShare]::ReadWrite
– share read/write (block delete)When you're done:
$fh.Close()
3
Because you are thinking of a different meaning of "open".
Notepad (and all other text editors I know) opens a file for reading, then you see it on your screen. Then it closes the file while you can still see the contents (now in RAM).
If, for example, you have fd = open("file.txt", FLAGS);
and it isn't (yet) closed by close(fd)
, then it wouldn't be copied with robocopy.
Thus, how could I simulate the case to keep opening the file in a simple manner? I would like to show the robocopy properties that it will avoid copying open file properties. But can't find a simple way to keep opening it – cuda – 2018-09-24T03:27:02.697
The simplest way I can think of (on Windows) is to build a simple C program that opens a file, sleeps for say 60 seconds and closes afterwards. You will then have 60 seconds to test robocopy. – fejyesynb – 2018-09-24T03:27:56.950
@cuda For a more concrete answer, open a new question (1 question per post on SE) – fejyesynb – 2018-09-24T03:29:04.983
Apart from making a program and place in that remote session computer, any more simple way by windows scripts? dont have ide of C program/JAVA/C# – cuda – 2018-09-24T03:29:35.053
I don't think scripts are low level enough for system open
and close
calls – fejyesynb – 2018-09-24T03:30:39.887
the stackexchange enforce the rules to create another question in 40 mins later. Is there any other way to keep file open without writing the most simple C program, do not have that tools of IDE or privilege to install – cuda – 2018-09-24T03:34:51.930
guess that something like this https://social.technet.microsoft.com/Forums/en-US/a6231646-57df-43ca-86db-dc04d93c32de/opening-text-file-with-vbscript?forum=ITCG
– cuda – 2018-09-24T03:38:07.183opened another question here https://superuser.com/questions/1360787/how-to-simply-keep-a-file-in-open-status-without-help-of-program-in-c-language-e but seems not working with that scripts... the file still get copied...robocoy does not ignore copying it
– cuda – 2018-09-24T05:48:56.293@fejyesynb: Practically everything above Cmd's level can handle it. PowerShell has full .NET runtime access, for example. – user1686 – 2018-09-24T06:14:33.313
@grawity it will be much appreciated if you could provide some insights how to work to open and hold the file handle without close file, then I run the robocopy to see if it really works with avoid copying the opened file. – cuda – 2018-09-24T06:30:18.510
1
Depends on how the file is opened
Many applications load the whole file to memory and close it, thus in fact the file is not in used. This of course can't be used with huge files, therefore editors for large text files have various techniques to work efficiently, like loading a part of the file to memory at once
Many others do keep the file handle opened, but they don't lock the file
Notepad OTOH maps the file to memory, resulting in a file that doesn't look like being opened. That means what fejyesynb said is not correct, because it doesn't load the file to RAM
For more information you can read
1
Possible duplicate of Why is Windows not warning about file in use for certain programs?
– phuclv – 2018-09-25T02:00:24.387