What's the proper way to overwrite system/hidden files with Windows API? - winapi

When you copy files using CopyFileExA function (see Windows API Reference: winbase.h) it has the ability to "fail" or not, if the destination file already exist, using (or clearing) the flag COPY_FILE_FAIL_IF_EXISTS. However, I can not find the way to overwrite files with attributes Read Only, Hidden or System.
I am working on a backup program with specific requirements which I can't find anywhere else.
I was thinking that before copying a file with one or more of those attributes set, perhaps I could clear those attributes on an existing file and then proceed to copy the file, knowing that my program will overwrite existing file with clear attributes.
Is that the best approach?
Another option would be just delete the old file before copying a new one. But I'm afraid that if I delete the old (backup) file and only then will try to copy the new one AND if the copy for some reason fails, then I will ended up without a backup file.
Or maybe there's a way of overwriting System/Hidden files that I'm not aware of.
What would be the best method of accomplishing this - copy and overwriting files with System/Hidden/ReadOnly attributes set?

Related

How can I mirror deleted duplicates from a source into a destination?

Here's the scenario: We have a computer running Windows 10 which has a directory that's backed up nightly. The backups are done with a batch file utilizing Robocopy and scheduled via Windows. The parameters are as such that the backup will always add any new files or existing file edits into the destination, but it will never delete files from the destination that have been deleted in the source. It essentially archives all files which are in the source directory at the end of each day.
Here's the tricky part. The source directory is very large, and occasionally someone finds a duplicate file (or several duplicates of a file) in it. When that happens, we need to delete all but one copy of the file, and then we need to access the backup directory manually, locate the file there, and do the same. This is tedious and time-consuming as it's not rare for someone to notice an entire subdirectory full of files that exist 5+ times each.
What we're looking for is a way to scan the source directory and all subdirectories inside for duplicate files and remove all but one copy of them, and then a way to reflect that into the destination. I've assumed that we will not be able to use Robocopy to reflect the changes in the destination due to the nature of the backup script it's running, but we do have the ability to run any third-party software on the destination directory as well, essentially running an action in both directories to clean each of them of duplicate files.
On that note, I'm not against using third-party tools to make this cleaner or more efficient, I'm just not aware of any.
There is one way to solve this problem I was also suffering from this problem. but I found that how to use "BATCH" file
There are mainly 2 command
X_COPY
ROBO_COPY
According to your need here, (1)x_copy will be helpfull
xcopywill backup your specific file or folder even if you changed some megabytes data, it will copy the new data and will not be replaced on previous data it will make new copy.
HOW TO DO
Open NotePad and type
xcopy "source file" "destination" /y/e/d/c/f/h/i/z/j
And then save your notepad as ".bat" file
for more requirement use below url
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy

Move directory and inherit permissions from target folder (as Copy would do)

That's what I want to achieve for my VCL application:
I extract a ZIP file to a directory in %TEMP%. If everything was okay, I move the directory to the target dir.
For the move operation I use JclFileUtils.FileMove (from JCL which resolves to MoveFileEx), because I need a real move operation and I want to overwrite files in the target dir.
This works so far, but the moved files have a per user file permission (inherited from Temp). I want the moved files to inherit permissions from the target folder.
Remarks:
Copy and delete is a workaround, I am aware of. But I want to avoid this (because of file size).
System.IOUtils.TFile.Move does not work for me, because it is implemented by copy and delete (in XE4).
Here a similar problem is described for .NET, but I do not know if an equivalent to GetAccessControl/SetAccessControl exists in Delphi.

Change TXT File - keep meta data

One of my programs stores some information in a .CONFIG file.
I want to change those information for my purposes, but after changing it with an editor, the software does not use this file. Therefore i think i need to keep the meta data of this file in order to be successful.
The evidence, that the software does not use this modified file:
After changing this file (located in program folder) and uninstalling the software, the modified .CONFIG file is not removed.
Maybe i have to say: The .config file is protected. If i want to change it, windows blocks with "access denied". So i have to save it somewhere else, remove the original and copy it to the folder (without success as i already said).
So how to keep those meta information? is there a software which allows to modify it but keeps those information?
Thanks in advance

Cocoa document incrementally save big package

For the last 2 days I've been trying to make my single-file document into a package, but I can't get it to work. In the documentation it states the preferred way is to use NSFileWrapper. I've tried it but it's just such a unintuitive way of handling files.
I guess to update a file I need to delete the file wrapper from it's directory, create a completely new one and add it again to the directory. I haven't found anything that explicitly states it, but I guess I should update the file only when fileWrapperOfType:error: is called.
As NSFileWrapper keeps everything (at least once loaded) in memory, this means that I'll have the old version and the new version at the same time until the user (or autosave) saves the file.
It seems like NSFileWrapper shouldn't be used for big files, but I think it's better if all the files that are needed by the document are inside the package(can be copied to another Mac/iPhone/iPad without errors) and I don't want to limit the user on how many/how big the files can be.
When using a manual URL-based saving mechanism, I end up getting corrupt packages, as the destination directory is always a temporary one, and I couldn't find any information on how to merge them. Every time I manually save the document without any changes, an error occurs, as I don't write anything to the temporary directory. But I don't see a reason in writing/linking everything to the temporary directory, only for it to be copied/'un'-linked back to its destination.
As I can't seem to find the right answer, what is the best-practice for saving and restoring big packages with many/big files in them?

Creating a file bundle on OSX

For an application I would like to store a collection of files together, and have them appear in the filesystem as a single file so its easy to manage. I am currently storing everything in a folder.
I would like to keep things accessible so you can manually edit the inside contents if neccesary.
One way to do this would be to create a zip archive and give it a custom extension other then .zip. Then it appears as a filetype and if needed you can unpack and access the content, but for normal use keep it hidden.
I can't seem to find a convenient way to do this. Boost and zlib can do the compression but don't work with archives. I found libzip but I have a hard time understanding how to use it and to me it seems that it only reads/writes zip archives without doing the actual compression.
Is there a more convenient way to tackle this?
Can you call system functions for creating an archive on OSX from c++ / Carbon?
Is there another way to make a folder appear as a single file?
In OSX, you can create Document Packages (similar to application bundles) which are treated as a single file in the Finder, but are really just directories with some internal structure.
Apple does not zip these packages, but they do provide the functionality you describe and they can be created and accessed through CoreFoundation by using CFBundleRef .
From the documentation:
... The important thing to remember about creating a document package is that it is just a directory. As long as the type of the document package is registered (as described in “Registering Your Document Type”), all you have to do is create a directory with the appropriate filename extension. (The Finder uses the filename extension as its cue to treat the directory as a package.) You can create the directory (and create any files you want to put inside that directory) using the standard BSD file system routines ...
As 1st step, simple rename the folder and add the extension .bundle, e.g. Myappdir.bundle
That's will show the whole folder as one file with a lego-like bundle icon.
The next step is you must create one Info.plist file inside.

Resources