Copy a folder programmatically without resolving hardlinks in Windows (Win32 API) - windows

I want to copy an entire folder without resolving the hardlinks
example:
Folder1
|
+---File1
File2
HardLink3 -> File3
(HardLink3 created using fsutil hardlink create or mklink)
I want to copy this folder to
Folder2
|
+---File1
File2
HardLink3 -> File3
keeping Folder2\HardLink3 as a hardlink pointing to File3
Is there an Win32 API call to copy a entire folder with
this semantic, or, if I need to do CopyFile / CreateHardLink
file by file, what's the API call to check if a given file is
a hardlink or not?

If you're absolutely sure that this is what you want to do, the easiest way to determine whether a file has multiple links (i.e., "is a hard link") is probably GetFileInformationByHandle.
The nNumberOfLinks value returned will be 1 for a normal file and more than 1 if the file is (or has) a hard link.
If I've understood your scenario correctly, it might be more sensible to check whether a file is hard linked to one of a specific set of files (the files in the "shared folder") rather than whether it is hard linked to any file anywhere. To do this, look at the File ID (nFileIndexHigh and nFileIndexLow) which for a hard link is the same as for the original file.
In the latter case, as an optimization you could use GetFileInformationByHandleEx with the FileIdBothDirectoryInfo option to read the names and file IDs for all the files in a given directory in a single operation.

I do not think there is a Win32 API call to do what you want all in one go, so you probably need to do this by hand.
Checking if a file is a hard-link or not is probably not what you want to do. If a file is not a symbolic link, directory (or reparse point or some other obscure thing) it is actually a hard link, i.e. the name of the file points to a stored file on disk. So if two files are pointing to the same data they are both hard links to that file.
Anyway, the Win32 methods to enumerate all hard links to a file are FindFirstFileNameW and FindNextFileNameW.

Related

How to recursively all sub directories with only one file inside

How can I recursively delete from a directory all its sub directories with only one file inside, and the file's name contains key word "DEL" and the file was created before 2017?
Thanks.
This is likely not possible, but you can come at least close to it:
The idea is to use find to travel through all the directories. Have a look at the -type flag, so that find picks up directories only.
With the -exec option of find, you pass the name of a program which checks, whether this is a directory to be deleted. I'm not sure whether it is safe to delete it right now (I think it is), or whether it would upset find (in which case you would just print the path of the directory to stdout, so that it can be deleted by the script which is invoking find), so this is something you have to try out.
Thr tricky part is to find whether this is a directory to be deleted. Finding the number of files in a directory can be done in different ways, and it depends on which programming language you use for this "checking program".
Matching the filename is trivial.
What won't work is finding out the creation date in case you are on Linux or Unix, because it is not stored anywhere. The closest you can get is the inode change time. If you are on Windows, you can find out the creation date.

Command Prompt: Move a file to an Unknownly named folder

So, is there a possible way to move Test.txt to C:\ProgramData\CsD2\Tools\("Unknown Folder Name")\data\per Using command prompt?
using foxidrives solution for your previous question for detecting the correct directory, then just
move test.txt "%folder%\"
Short answer: yes. Not quite sure what the situation is that has left only the middle part of your path unknown, and the need to use the comnand line, but I have encountered similar cases on Linux and expect the algoirthm can be adapted to Windows commands. It's possible to do this by hand rather than writing a shell script, but it's up to you and your skills.
Permissions matter. Make sure you elevate yours enough to read and write in Tools before continuing.
First, change directory to C:\ProgramData\CsD2\Tools\
Presumably there are many items here. Some may be "hidden," so list the contents of this directory and be sure to include an option to show hidden files and folders. If you can, restrict the search to directories only.
It's tempting to display contents recursively in the above step. It's up to you, but I find it makes the output cluttered without a script to do the rest of the work.
Now it's time to search for the subfolder set that theoretically only exists in your target folder. Suppose Tools contains the directories fldr1, fldr2, and fldr3. Use your command to list a directory's contents with the path "fldr1\data\per", then use "fldr2\data\per", and so on until it doesn't return an error. Per may be empty, but that should look different from the path not found error.
Now you've found the name of your mystery folder. Write it down for future reference.
At thus point, you know the path to Test.txt, and the full path to the destination directory. Do a move command to relocate Test.txt, and you're done. I like to relist the contents of the target directory after to be comfortable that it arrived.

Directory navigation in command prompts

I'm trying to run a batch file that exists in one folder, from a batch file in another folder:
Parent folder Big containes 2 folders BatchFolder and Medium.
BatchFolder contains a batch file called Batch1.
Medium contains another folder called Small.
Small contains a batch file called Batch2 that needs to run Batch1.
The command prompt is run from the location of Batch2
Therefor, how do I navigate up the folders To Big, and then navigate into the BatchFolder?
I've been trying alsorts to achieve this with no success, such as Bacth2 containing the following "call ../BatchFolder/Batch1.bat"
I'm not sure whether you really need to navigate to the required folder (i.e. set it as the current one) or you simply need a way to call the batch script in that folder using a relative path notation. Navigating, from how I understand the term, means the former, but your last sentence seem to show that you need the latter.
First, the answer:
call %~dp0%..\..\BatchFolder\Batch1.bat
Next, what it means. %~dp0% is a variation of %0: the latter is the full path to this batch file (i.e. Batch2.bat) including the file name, and the former is the full path to this batch's folder (including the trailing \).
.. points to the immediate parent folder. It is repeated twice because we need to access the 'grand-parent' of the Batch2.bat's folder, and the grand-parent is Big. Once we are pointing to Big, we can address the files/folders in it, in this case it's BatchFolder that we need, and eventually we can put the name of Batch1.bat.
This works regardless of what the current folder is. That is, in case I wasn't clear on that head, by simply calling a batch file you are not yet changing the current folder. You would have to use the CD command for that. That would be navigating (but I'm still open to being corrected as to my understanding of this term).

Prolog - Finding the current directory, relative directory for 'tell' predicate

I'm having trouble trying to figure out how to get prolog to spit out a text file where I want it to. I'm currently doing a bunch of operations and then using
tell('output.txt')
to record the output. Now the problem is that when I do this, it creates this file in the SWI \bin\ folder. I was wondering if there's a way to make it create this file in the directory containing the actual .pl file. So even if the file was moved (and it will be), the text file gets created right where the source file is.
Long story short, is there a way to get the location of the source file once the source file has been consulted?
Many Thanks!
You can get the names of all the loaded files using source_file/1.
From the SWI-Prolog manual:
source_file(?File)
True if File is a loaded Prolog source file. File is the absolute and
canonical path to the source-file.

Arbitrary sort key in filesystem

I have a pet project where I build a text-to-HTML translator. I keep the content and the converted output in a directory tree, mirroring the structure via the filesystem hierachy. Chapters go into directories and subchapters go into subdirectories. I get the chapter headings from the directory and file names. I want to keep all data in files, no database or so.
Kind of a keep-it-simple approach, no need to deal with meta-data.
All works well, except for the sort order of the directories and files to be included. I need sort of an arbitrary key for sorting directories and files in my application. That would determine the order the content goes into the output.
I have two solutions, both not really good:
1) Prepend directories and files with a sort key (e.g. "01_") and strip that in the output files in order not to pollute the output file names. That works badly for directories since they must keep the key data in order not to break the directory structure. That ends with an ugly "01_Introduction"...
2) put an config file into each directory with information on how to sort the directory content, to be used from my applications. That is error-prone and breaks the keep-it-simple no meta-data approach.
Do you have an idea? What would you do?
If your goal is to effectively avoid metadata, then I'd go with some variation of option 1.
I really do not find 01_Introduction to be ugly., at all.

Resources