W10 CMD forfiles

124 views Asked by At

Since I had a task where I have to copy recursively files of a nested directory for work, i discovered the forfiles-function in windows cmd.

It worked properly and now I wonder how does the function distinguish between a file and a directory?

If every file had a file extension like .jpg .png .xls or something like that, I could understand it, but some of my files came without extensions, but it still did its job.

As I'm used to linux, I tried to google the sourcecode, but windows applications aren't opensource, so if anybody can explain me, how does it work, it would be very interesting to know.

PS: why does this got downvoted? its a general question

2

There are 2 answers

0
MSalters On BEST ANSWER

The command will eventually call the Windows FindFirstFile/FindNextFile functions. Those return a WIN32_FIND_DATA structure which may contain a FILE_ATTRIBUTE_DIRECTORY flag. If that flag is not set, it's a file.

0
Dominique On

Internally there is quite a difference between a file and a directory, and it's no surprise that typical file/directory handling commands know about this. The fact that a file doesn't have an extension (or that a directory is called "directory.jpeg") does not cause any confusion within those commands.

If you check forfiles' "man page" (forfiles /?), you might see that the /C switch gives you access to the @isdir variable, which can tell you the difference: are you dealing with a directory (value:TRUE) or a file (value:FALSE)?