OpenFile function (winbase.h)
Creates, opens, reopens, or deletes a file.
Syntax
Parameters
The name of the file.
The string must consist of characters from the 8-bit Windows character set. The OpenFile function does not support Unicode file names or opening named pipes.
A pointer to the OFSTRUCT structure that receives information about a file when it is first opened.
The structure can be used in subsequent calls to the OpenFile function to see an open file.
The OFSTRUCT structure contains a path string member with a length that is limited to OFS_MAXPATHNAME characters, which is 128 characters. Because of this, you cannot use the OpenFile function to open a file with a path length that exceeds 128 characters. The CreateFile function does not have this path length limitation.
The action to be taken.
This parameter can be one or more of the following values.
Value | Meaning |
---|---|
OF_CANCEL 0x00000800 | Ignored. To produce a dialog box containing a Cancel button, use OF_PROMPT. |
OF_CREATE 0x00001000 | Creates a new file. If the file exists, it is truncated to zero (0) length. |
OF_DELETE 0x00000200 | Deletes a file. |
OF_EXIST 0x00004000 | Opens a file and then closes it. Use this to test for the existence of a file. |
OF_PARSE 0x00000100 | Fills the OFSTRUCT structure, but does not do anything else. |
OF_PROMPT 0x00002000 | Displays a dialog box if a requested file does not exist. A dialog box informs a user that the system cannot find a file, and it contains Retry and Cancel buttons. The Cancel button directs OpenFile to return a file-not-found error message. |
OF_READ 0x00000000 | Opens a file for reading only. |
OF_READWRITE 0x00000002 | Opens a file with read/write permissions. |
OF_REOPEN 0x00008000 | Opens a file by using information in the reopen buffer. |
OF_SHARE_COMPAT 0x00000000 | For MS-DOS–based file systems, opens a file with compatibility mode, allows any process on a specified computer to open the file any number of times. Other efforts to open a file with other sharing modes fail. This flag is mapped to the FILE_SHARE_READ|FILE_SHARE_WRITE flags of the CreateFile function. |
OF_SHARE_DENY_NONE 0x00000040 | Opens a file without denying read or write access to other processes. On MS-DOS-based file systems, if the file has been opened in compatibility mode by any other process, the function fails. This flag is mapped to the FILE_SHARE_READ|FILE_SHARE_WRITE flags of the CreateFile function. |
OF_SHARE_DENY_READ 0x00000030 | Opens a file and denies read access to other processes. On MS-DOS-based file systems, if the file has been opened in compatibility mode, or for read access by any other process, the function fails. This flag is mapped to the FILE_SHARE_WRITE flag of the CreateFile function. |
OF_SHARE_DENY_WRITE 0x00000020 | Opens a file and denies write access to other processes. On MS-DOS-based file systems, if a file has been opened in compatibility mode, or for write access by any other process, the function fails. This flag is mapped to the FILE_SHARE_READ flag of the CreateFile function. |
OF_SHARE_EXCLUSIVE 0x00000010 | Opens a file with exclusive mode, and denies both read/write access to other processes. If a file has been opened in any other mode for read/write access, even by the current process, the function fails. |
OF_VERIFY | Verifies that the date and time of a file are the same as when it was opened previously. This is useful as an extra check for read-only files. |
OF_WRITE 0x00000001 | Opens a file for write access only. |
Return value
If the function succeeds, the return value specifies a file handle to use when performing file I/O. To close the file, call the CloseHandle function using this handle.
If the function fails, the return value is HFILE_ERROR. To get extended error information, call GetLastError.
Remarks
If the lpFileName parameter specifies a file name and extension only, this function searches for a matching file in the following directories and the order shown:
- The directory where an application is loaded.
- The current directory.
- The Windows system directory.
Use the GetSystemDirectory function to get the path of this directory.
The 16-bit Windows system directory.
There is not a function that retrieves the path of this directory, but it is searched.
The Windows directory.
Use the GetWindowsDirectory function to get the path of this directory.
The lpFileName parameter cannot contain wildcard characters.
The OpenFile function does not support the OF_SEARCH flag that the 16-bit Windows OpenFile function supports. The OF_SEARCH flag directs the system to search for a matching file even when a file name includes a full path. Use the SearchPath function to search for a file.
A sharing violation occurs if an attempt is made to open a file or directory for deletion on a remote machine when the value of the uStyle parameter is the OF_DELETE access flag OR’ed with any other access flag, and the remote file or directory has not been opened with FILE_SHARE_DELETE share access. To avoid the sharing violation in this scenario, open the remote file or directory with OF_DELETE access only, or call DeleteFile without first opening the file or directory for deletion.
In WindowsВ 8 and Windows ServerВ 2012, this function is supported by the following technologies.
Technology | Supported |
---|---|
Server Message Block (SMB) 3.0 protocol | Yes |
SMB 3.0 Transparent Failover (TFO) | Yes |
SMB 3.0 with Scale-out File Shares (SO) | Yes |
Cluster Shared Volume File System (CsvFS) | Yes |
Resilient File System (ReFS) | Yes |
В
CsvFs will do redirected IO for compressed files.
Find and Open Files Using Command Prompt in Windows 10
Do you ever drive yourself crazy looking for all the files you need for a project? Have you saved them in unknown places on your computer? Can you only remember part of the name you gave the file? If any of these apply to you, using the command prompt instead of Windows Explorer may help you quickly find the files you need and allow you to open them directly from that location.
We have to mention, though, that it’s a waste of time using the Command prompt to open simple, trivial files. However, this method is something you should definitely look at when dealing with multiple files and assorted file types in random folders.
Opening Command Prompt
To search for the files you want, first launch Command Prompt. There are several ways to do this.
- Type cmd in the Windows search box, then click on the icon to open the window. It’s preferred to run the command prompt as an administrator.
- Use the Run window. A fast way to launch this window is to press the Win + R keys on your keyboard. Then, type cmd or cmd.exe and press Enter or click OK.
- Use Cortana if it works for you properly: as soon as you see the Cortana is “listening” text, say “open command prompt,” and it will open up.
Search for your files
In the command prompt, type dir «\search term*» /s , but replace the words “search term” with text you want to find by using the file name. In the following screen, we are trying to search for a folder/file named “movies.”
The correct file path can be easily identified by the size of the folder. Once you have typed in the command, press Enter, and the search begins. It may look like nothing is happening for a second or two, though. But soon all the file paths mentioning the correct file or folder will be identified and named.
What do all the terms in the command mean?
- dir is a command used to show files in the current directory but which can also locate data anywhere in the system.
- The backslash tells dir to search from the root directory of the current drive.
- /s tells dir to search all subdirectories.
- Finally, * is used by command-line apps as a wildcard. It indicates that the search will locate all file names that contain your search term.
- Putting an asterisk at the end of the name, like business* , will find all the files with your search term at the beginning of the name.
- If you put it at the beginning, then your search will include only results with your text at the end. For example, *.jpg .
- If you put an asterisk at each end of the search term, you will cover all the bases. No matter where your text is in the actual name of the file, that file will be listed in the results.
Change Command Prompt Directories for Precise File Location
If your target file is located in D: drive, you won’t find it anywhere in the Windows folder from the C: drive. Therefore, you should learn to change the drive path as shown below.
First, type cd.. , which will enable you to easily move one folder up. To go to D drive, just type d: , and the command prompt will now listen to the new drive.
Within the destination drive, you can search for the correct file once again using the dir «\search term*» /s command discussed in the previous section. Do take care to use the * wildcard, both at the beginning and end of the search term for a comprehensive search – especially if that folder has hundreds of files. It’s pretty common to make mistakes in the wildcards and backslash, which returns a “no label” error as shown below. If you type correctly, you will easily identify the folder path and the correct file name.
If the folder path is very long, you may want to copy-paste the entire thing. We have a detailed tutorial on how to cut-copy-paste in the Windows 10 command prompt. But the method is simple enough: use Ctrl + A to select the entire command prompt screen, then just release the mouse or trackpad click. This would make any part of the command screen selectable for a copy-paste action as shown below.
Open your Files in Windows 10 Command Prompt
Once you find the file you want to open, you can open it from this screen using its default program, without locating it in your file explorer.
First, change the directory to the immediate folder where the file is located. This can be done using cd followed by the entire folder path to the correct file. It’s common to make mistakes, so it’s better to copy-paste the entire folder path as discussed in the previous section. Once the correct directory menu has been opened, you can open any files there. Here it’s a folder, where I saved my personal collection of rented movies.
Enter the file name inside the quotation marks «..» as shown here. Press Enter and the file will open using its default application. Here it’s Movies and TV programs, which is set as my default application for .mp4 files.
The file, a Mel Gibson-led movie called “Get the Gringo,” opens from the command prompt.
You can open multiple files from the command prompt once you’ve opened the correct folder. Here we open another file called “Erin Brockovich.mp4” in the same folder.
The file, a Julia Roberts-led movie called “Erin Brockovich,” opens from the command prompt using the default app.
Once you’re done, you may want to move back to another folder in the command prompt. For this, use cd.. followed by a new drive name as shown below. Navigating your computer’s files using the command prompt is very similar to just doing it in a regular way from your File explorer.
Wrapping Up
If you are looking for all your files concerning one topic or just trying to find the one that is hiding among all the folders in your computer, try using the command prompt. It may make it more efficient for you.
Amazingly, the command prompt is way faster and more responsive than opening the files from the File Explorer. This is true for video files, Windows Photos, Word documents, and heavy-duty games. The reason for this is that the GUI always consumes more memory and slows down the processes.
Related:
Sayak Boral is a technology writer with over ten years of experience working in different industries including semiconductors, IoT, enterprise IT, telecommunications OSS/BSS, and network security. He has been writing for MakeTechEasier on a wide range of technical topics including Windows, Android, Internet, Hardware Guides, Browsers, Software Tools, and Product Reviews.