Recent

Author Topic: I can't delete a folder using %userprofile%  (Read 2361 times)

AnthonyStuart

  • Newbie
  • Posts: 2
I can't delete a folder using %userprofile%
« on: January 05, 2021, 05:57:21 pm »
Hi, I'm Anthony.
And I have a project which is to create a cleaning software, like Ccleaner, I have used the DeleteDirectory function.

The problem is that I want to delete for example the history of microsoft edge, the data is in, AppData / Local / Microsoft / Edge / Default folder, what I want to do is that when I make it delete, look for the user's folder , not mine, that's why I use %userprofile%, but when using it, it doesn't find the file.

Another thing, how can I delete an individual file?
I am new, and Free Pascal, I found it very easy to understand and use.

Greetings to all!...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: I can't delete a folder using %userprofile%
« Reply #1 on: January 05, 2021, 06:05:15 pm »
Most FPC functions are programmer interface functions, not windows explorer functions. These are faster, but don't do things like environment variable expansion.

IOW you need to expand these yourself, using getenvironmentstring() or getwindowsspecialdir and string operations.
« Last Edit: January 05, 2021, 06:15:41 pm by marcov »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: I can't delete a folder using %userprofile%
« Reply #2 on: January 05, 2021, 06:13:10 pm »
@AnthonyStuart,

As I do not use Windows, not much I can say about it. Have you tried to browse to the location and delete those files or folder using your File Manager or Windows Explorer? If cannot find or deletem them, maybe they have file attribute set to hidden or maybe permission issue. As far as I know, Win10 does not allow user to access certain data of other users.

You may need to set the file attribut to include hidden and/or system file:
https://www.freepascal.org/docs-html/rtl/sysutils/findfirst.html

... how can I delete an individual file?

You can use DeleteFile:
https://www.freepascal.org/docs-html/rtl/sysutils/deletefile.html

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: I can't delete a folder using %userprofile%
« Reply #3 on: January 05, 2021, 09:59:40 pm »
IOW you need to expand these yourself, using getenvironmentstring() or getwindowsspecialdir and string operations.

GetEnvironmentString() won't work in this case, as there is no environment variable defined for the local AppData folder, only for the roaming AppData folder.

As for GetWindowsSpecialDir(), there are no CSIDL (or even a KNOWNFOLDERID) defined for any of Microsoft Edge's folders.  But, you can use GetWindowsSpecialDir(CSIDL_LOCAL_APPDATA, False) and then append '\Microsoft\Edge\Default' to the result of that.
« Last Edit: January 05, 2021, 10:02:13 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: I can't delete a folder using %userprofile%
« Reply #4 on: January 05, 2021, 10:59:17 pm »
Has anyone looked at "ExpandEnvironmentStrs" ?
The only true wisdom is knowing you know nothing

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: I can't delete a folder using %userprofile%
« Reply #5 on: January 05, 2021, 11:37:26 pm »
Has anyone looked at "ExpandEnvironmentStrs" ?

That is just a wrapper for the Win32 ExpandEnvironmentStrings() function in kernel32.dll.  That has the same problem that GetEnvironmentString() has - there are no environment variables defined for the file path that Anthony is looking for.
« Last Edit: January 05, 2021, 11:38:59 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

AnthonyStuart

  • Newbie
  • Posts: 2
Re: I can't delete a folder using %userprofile%
« Reply #6 on: January 06, 2021, 12:35:08 am »
at least I can make it run a .bat with the command but not even that xD

that is, it does not work, at least I put the full path, but if I want to share it it will not work

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: I can't delete a folder using %userprofile%
« Reply #7 on: January 06, 2021, 05:46:22 am »
that is, it does not work, at least I put the full path, but if I want to share it it will not work

You've to tell us what doesn't work and show us what code you tried. Otherwise it's all crystal-balling, which doesn't usually work very well :-\

But OK, normally you would use GetUserDir() to get the "base" directory and add the relative path to it, which for your needs would be something like:

Code: Pascal  [Select][+][-]
  1. function GetEdgeDataDir: String;
  2. const
  3.   RelPath = 'AppData\Local\Microsoft\Edge\Default';
  4. begin
  5.   Result := GetUserDir + RelPath;
  6. end;

If that fails, make sure the function's result is shown somewhere (like in a ShowMessage() or a StatusBar) and then check to see whether:
  • the directory exist and
  • the user has enough permissions to do whatever the program is trying.[/tt]).
(both of which you should be doing anyway before anything else ...)

And that's basically all there is to it  ;)
« Last Edit: January 06, 2021, 05:50:07 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: I can't delete a folder using %userprofile%
« Reply #8 on: January 06, 2021, 07:47:46 pm »
But OK, normally you would use GetUserDir() to get the "base" directory and add the relative path to it

That would be the wrong solution in this case, since the user can configure Windows to relocate the AppData folder to wherever they want.  So it is not quaranteed to be underneath their user profile.  That is why the Win32 API provides a way to get the exact location of the AppData folder, as described earlier, eg:

Code: Pascal  [Select][+][-]
  1. function GetEdgeDataDir: String;
  2. begin
  3.   Result := GetWindowsSpecialDir(CSIDL_LOCAL_APPDATA, False);
  4.   if Result <> '' then
  5.     Result := IncludeTrailingPathDelimiter(Result) + 'Microsoft\Edge\Default';
  6. end;
« Last Edit: January 06, 2021, 07:49:40 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: I can't delete a folder using %userprofile%
« Reply #9 on: January 06, 2021, 08:52:08 pm »
That would be the wrong solution in this case, since the user can configure Windows to relocate the AppData folder to wherever they want.

Which is why I wrote "normally" (i.e. for users who don't mess too much with the innards of Windows) and insisted on the necessity of checking tht it went well.

But you're right: yours is a better solution :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018