Solved

Reading/writing and manipulations of files from subdirectories

Posted on 1998-10-01
10
276 Views
Last Modified: 2010-04-15
Can anyone help w/ tips to write a function to read and/or write files from/to a certain directory in your hard disk.
Also some basics file manipulation such as renaming etc...
All that under NT if possible.
For example I like to check if a file exists under a certain directory, and then from there i 'd like to copy it to a different directory under a different name.

Thanx for your help.
0
Comment
Question by:mvu
10 Comments
 
LVL 7

Expert Comment

by:faster
ID: 1253167
Use MoveFileEx(), CopyFile() API to rename or copy fle.  Use CreateFile(), ReadFile(), WriteFile() etc to read or write file.

BOOL MoveFileEx( LPCTSTR lpExistingFileName,
 // address of name of the existing file
 
LPCTSTR lpNewFileName,
 // address of new name for the file
 
DWORD dwFlags
 // flag to determine how to move file
 
);
 
BOOL CopyFile( LPCTSTR lpExistingFileName,
 // pointer to name of an existing file
 
LPCTSTR lpNewFileName,
 // pointer to filename to copy to
 
BOOL bFailIfExists
 // flag for operation if file exists
 
);
 

0
 

Author Comment

by:mvu
ID: 1253168
Thanks, but how do I move a file to a new directory?
for example, c:\dir1\filename need to go to c:\dir2\filename


0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1253169
you can always use system("copy c:\dir1\file1 c:\dir2\file2");
0
 

Author Comment

by:mvu
ID: 1253170
Thanks, but how do I move a file to a new directory?
for example, c:\dir1\filename need to go to c:\dir2\filename


0
 
LVL 10

Expert Comment

by:rbr
ID: 1253171
use
system("move c:\dir1\file1 c:\dir2\file2");

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 10

Expert Comment

by:rbr
ID: 1253172
Sorry mistake use

system("move c:\\dir1\\file1 c:\\dir2\\file2");
same for copying.
system("copy c:\\dir1\\file1 c:\\dir2\\file2");
0
 

Author Comment

by:mvu
ID: 1253173
I tried, it did not work?  My system just hung...
Maybe if someone can help w/ the FindFileInTree() function that i tried to find a specific file given a root dir.  From there i could use CopyFile to copy it over wherever.
But my compiler says the FindFileInTree function cannot be recognized.  Any tips on implementing my own and/or how to get the existing one to work?

Thanks.
0
 

Accepted Solution

by:
Bayo earned 100 total points
ID: 1253174
You can Create a New Directoy using
BOOL CreateDirectory(

    LPCTSTR lpPathName,      // pointer to a directory path string
    LPSECURITY_ATTRIBUTES lpSecurityAttributes       // pointer to a security descriptor
   );      
 

Parameters

lpPathName

Points to a null-terminated string that specifies the path of the directory to be created.

There is a default string size limit for paths of MAX_PATH characters. This limit is related to how the CreateDirectory function parses paths.
Windows NT: An application can transcend this limit and send in paths longer than MAX_PATH characters by calling the wide (W) version of CreateDirectory and prepending "\\?\" to the path. The "\\?\" tells the function to turn off path parsing; it lets paths longer than MAX_PATH be used with CreateDirectoryW. This also works with UNC names. The "\\?\" is ignored as part of the path. For example, "
\\?\C:\myworld\private" is seen as "C:\myworld\private", and "
\\?\UNC\bill_g_1\hotstuff\coolapps" is seen as "\\
bill_g_1\hotstuff\coolapps".

lpSecurityAttributes

Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited.

Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new directory. If lpSecurityAttributes is NULL, the directory gets a default security descriptor. The target file system must support security on files and directories for this parameter to have an effect.
Windows 95: The lpSecurityDescriptor member of the structure is ignored.

 
And then use the MoveFile function that renames an existing file or a directory (including all its children).

BOOL MoveFile(

    LPCTSTR lpExistingFileName,      // address of name of the existing file  
    LPCTSTR lpNewFileName       // address of new name for the file
   );      
 

Parameters

lpExistingFileName

Points to a null-terminated string that names an existing file or directory.

lpNewFileName

Points to a null-terminated string that specifies the new name of a file or directory. The new name must not already exist. A new file may be on a different file system or drive. A new directory must be on the same drive.

Return Values
If the function succeeds, the return value is nonzero.

0
 

Expert Comment

by:sridharp
ID: 1253175
using findfirst and findnext function  U can find the file at which location it is stored. If the directory is present then use the command mkdir by including dir.h header file and use the above functions to find the file. And then use the system command 'copy ' to copy the file from one location to the other.
0
 

Author Comment

by:mvu
ID: 1253176
Thank you all for your input.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now