The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,437 other followers

Using Windows API function MoveFile to check if a filename is valid…  – via Stack Overflow

Posted by jpluimers on 2018/10/17

This has a really neat Windows API trick [WayBackdelphi – How can I sanitize a string for use as a filename? – Stack Overflow by [WayBack] Alex.

Via: [WayBack] Interesting use of MoveFile, with NIL as the first parameter. https://stackoverflow.com/a/961500/49925 – Thomas Mueller (dummzeuch) – Google+

  function IsValidFilePath(const FileName: String): Boolean;
  var
    S: String;
    I: Integer;
  begin
    Result := False;
    S := FileName;
    repeat
      I := LastDelimiter('\/', S);
      MoveFile(nil, PChar(S));
      if (GetLastError = ERROR_ALREADY_EXISTS) or
         (
           (GetFileAttributes(PChar(Copy(S, I + 1, MaxInt))) = INVALID_FILE_ATTRIBUTES)
           and
           (GetLastError=ERROR_INVALID_NAME)
         ) then
        Exit;
      if I>0 then
        S := Copy(S,1,I-1);
    until I = 0;
    Result := True;
  end;

–jeroen

3 Responses to “Using Windows API function MoveFile to check if a filename is valid…  – via Stack Overflow”

  1. Remy said

    Using nil as the first parameter of MoveFile() is undocumented behavior.

  2. Mike said

    this is not thread save

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: