Reputation: 5502
I want to create a filename with characters like ":","-". i tried the following code to append the date and time to my filename.
Str.Format(_T("%d-%d-%d-%d:%d:%d.log"),systemTime.wDay ,systemTime.wMonth ,systemTime.wYear,systemTime.wHour,systemTime.wMinute,systemTime.wSecond);
std::wstring NewName=filename.c_str() + Str;
MoveFileEx(oldFilename.c_str(), NewName.c_str(), 2 )
MoveFileEx fails with windows ErrorCode 123(ERROR_INVALID_NAME).So i think the issue is with my new Filename which contain ":" and "-"
Thanks,
Upvotes: 2
Views: 221
Reputation: 2115
Windows does not allow few special character for creating as a file name.
But, for creating file name using current date and time you can use this formatting.
CTime CurrentTime( CTime::GetCurrentTime() );
SampleFileName = CurrentTime.Format( _T( " %m_%d_%y %I_%M_%S" ) ) + fileExtension;
For more time formating, Please refer this
Upvotes: 2
Reputation: 153977
"I want to create..." No you don't. Different systems impose different constraints on what is legal in a filename. Most modern systems do allows fairly long names (say more than a 100 characters), and don't impose a format on them (although Windows does still handle anything after the last .
, if there is one, specially, so you want to be careful there). If you're not concerned about portability, you can simply follow the rules of the system you're on: under Unix, no '/'
or '\0'
(but I'd also avoid anything a Unix shell would consider a meta-character: anything in ()[]{}<>!$|?*" \
and the backtick, at least), and I'd avoid starting a filename with a '-'
. Windows formally forbids anything in <>:"/\|?*
; here to, I'd avoid anything other programs might consider special (including using two %
, which could be interpreted as a shell variable), and I'd also be careful that if there was a .
, the final .something
was meaningful to the system. (If the filename already ends with something like .log
, there's no problem with additional dots before that.)
In most cases, it's probably best to be conservative; you never know what system you'll be using in the future. In my own work (having been burned by creating a filename witha a colon under Linux, and not being able to even delete it later under Windows), I've pretty much adopted the rule of only allowing '-', '_' and the alphanumeric characters (and forbidding filenames which differ only in case—more than a few people I know will only use lower case for letters). That's far more restrictive than just Unix and Windows, but who knows what the future holds. (It's also too liberal for some of the systems I've worked on in the past. These are hopefully gone for good, however.)
Upvotes: 2
Reputation: 38616
Indeed, you cannot use the :
character in windows file names. Replace it with something else. If a program depends on the name then modify it to interpret the alternative delimiter.
Upvotes: 3