current dwg file name

current dwg file name

waseefur.rahman
Advocate Advocate
1,987 Views
8 Replies
Message 1 of 9

current dwg file name

waseefur.rahman
Advocate
Advocate

Hi,

Can any one guide me how to get the current dwg file name in C++,

By using this below code I am getting the full path with file name,

 

const ACHAR *dwgfilename = curDoc()->fileName();


but I need only the file name.

0 Likes
Accepted solutions (3)
1,988 Views
8 Replies
Replies (8)
Message 2 of 9

tbrammer
Advisor
Advisor
Accepted solution

How about this:

TCHAR drive[_MAX_DRIVE+1], dir[_MAX_DIR+1], fname[_MAX_FNAME+_MAX_EXT+1], ext[_MAX_EXT+1];
errno_t err = _tsplitpath_s(curDoc()->fileName(), drive, dir, fname, ext);
_tcscat_s(fname, _MAX_FNAME+_MAX_EXT, ext);

 


Thomas Brammer ● Software Developer ● imos AG ● LinkedIn ●
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

0 Likes
Message 3 of 9

waseefur.rahman
Advocate
Advocate
That's great,

One more help I need,

After getting the file name,
I need to create a new path with that file name.

Then copy file to that new path,
I have tried with the code below, but it's not working

//source path

const ACHAR * dwgsourcepath = curDoc ()->filename ();

//File name

TCHAR drive[_MAX_DRIVE+1], dir[_MAX_DIR+1], fname[_MAX_FNAME+_MAX_EXT+1], ext[_MAX_EXT+1];
errno_t err = _tsplitpath_s(curDoc()->fileName(), drive, dir, fname, ext);
_tcscat_s(fname, _MAX_FNAME+_MAX_EXT, ext);

//Destination path

const chat *dwgdestinationpath = D:\\New folder + fname [_MAX_FNAME + _MAX_EXT + 1];

//copy file from source to destination

std::ifstream in(dwgsourcepath);
std::ifstream in(dwgdestinationpath);
out << in.rdbuf();

0 Likes
Message 4 of 9

tbrammer
Advisor
Advisor
Accepted solution

I'm not an expert for std-streams. Don't know whether they work for binary data.

This should work:

 

TCHAR dwgdestinationpath[_MAX_PATH+1] = _T("D:\\New folder\\");
_tcscat_s(dwgdestinationpath, _MAX_PATH, fname);
BOOL bFailIfExists=FALSE; // set TRUE if you don't want to overwrite existing file
BOOL bCopySuccess = CopyFile(dwgsourcepath, dwgdestinationpath, bFailIfExists);

Thomas Brammer ● Software Developer ● imos AG ● LinkedIn ●
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

0 Likes
Message 5 of 9

waseefur.rahman
Advocate
Advocate

Great,
Many Thanks ,


one more help Please,


Since "dwgdestinationpath" we are creating from below,

 

TCHAR dwgdestinationpath[_MAX_PATH + 1] = _T("D:\\New folder\\");

_tcscat_s(dwgdestinationpath, _MAX_PATH, fname);

 

But I am getting this Destination path from below

 

kt::CStr sOutput

Also attached the screen shot,

From the coding you provided it copies the dwg file to destination without extension (i.e) .dwg

 

I would be highly thankful for your help..

Thanks & Regards

 

 

0 Likes
Message 6 of 9

tbrammer
Advisor
Advisor

I don't understand what I see in your PNG Smiley Sad

 


TCHAR dwgdestinationpath[_MAX_PATH + 1] = _T("D:\\New folder\\");

_tcscat_s(dwgdestinationpath, _MAX_PATH, fname);

...

From the coding you provided it copies the dwg file to destination without extension (i.e) .dwg


I made the assumption that you use fname after the function call

    _tcscat_s(fname, _MAX_FNAME+_MAX_EXT, ext);

which appended the extension to fname.


Thomas Brammer ● Software Developer ● imos AG ● LinkedIn ●
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

0 Likes
Message 7 of 9

waseefur.rahman
Advocate
Advocate
Actually I can't directly enter the path as ("D:\\New folder"), Since it's variable..
I am getting this path from const char* path,

What I Need is I need to add file name to this path.

Then use this path in copy command as below,

CopyFile(dwgsourcepath, path, bFailIfExists)

Hope you got it..
0 Likes
Message 8 of 9

tbrammer
Advisor
Advisor
Accepted solution

From your PNG it looks as you want to take the directory from sOutput.str(). I can't see what type sOutput is. You wrote that you get the path as const char*. So you have to convert it to TCHAR*. The easiest way to do this is to use CString, because it's construtor works with both: char* or wchar*:

TCHAR dwgdestinationpath[_MAX_PATH + 1];
CString csPath(sOutput.str()), csBackslash(_T("\\"));
if (csPath.Right(1) != csBackslash) // append '\' if required
    csPath += csBackslash;
_tcscpy_s(dwgdestinationpath, _MAX_PATH, (LPCTSTR)csPath);  
_tcscat_s(dwgdestinationpath, _MAX_PATH, fname); 

Or just use CString everywhere to make it more readable:

 

CString dwgdestinationpath(sOutput.str()), csBackslash(_T("\\")); 
if (dwgdestinationpath.Right(1) != csBackslash) // append '\' if required
	dwgdestinationpath += csBackslash;
dwgdestinationpath += fname; 

 


Thomas Brammer ● Software Developer ● imos AG ● LinkedIn ●
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

0 Likes
Message 9 of 9

waseefur.rahman
Advocate
Advocate

That's Great,
got the solution,
Highly Thankful for your Help..

0 Likes