#1 2022-03-15 15:56

bdpq
Member
Registered: 2015-11-05
Posts: 9

Set file attribute in Pascal Script?

I was wondering is there a way to set the file attributes with Pascal Script?  I am using the program to process a large set of files and it would be nice to be able to set the file attributes programmatically (for example: hidden) when running the script.

Thanks you!

Offline

#2 2022-03-16 13:00

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Set file attribute in Pascal Script?

There are all available functions you can use in a PascalScript for ReNamer:
https://www.den4b.com/wiki/ReNamer:Pasc … :Functions

Since ReNamer is a tool to rename file names, such file modifications are not in scope of this tool, me think.

Why not using the scripting tools of your OS, like DOS batch , VBScript or Powershell?
Open CMD and type in: attrib /?

Maybe you can "abuse" the PascalScript ExecConsoleApp() function for your task
https://www.den4b.com/wiki/ReNamer:Pasc … _Execution

I would use an scripting tools of my OS or a dedicated file manager.


my opinion
 


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#3 2022-03-16 20:20

bdpq
Member
Registered: 2015-11-05
Posts: 9

Re: Set file attribute in Pascal Script?

That is what I am doing right now, I am using ExecConsoleApp() and call a .bat script to set the file attribute.  It works but is rather cumbersome.  The point of it is that it would be nice to set the attributes as some files are renamed, so that I do not need to search for the files after they are renamed and set the attributes in other software.

Offline

#4 2022-03-16 20:27

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Set file attribute in Pascal Script?

At least we can export the NewName column
https://www.den4b.com/wiki/ReNamer:Export_menu

I can provide a VBScript or Powershell command to parse that exported list
line-by-line and then there can be done something to each file name.

Stil two steps, but would work...




 


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#5 2022-03-17 11:44

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,440

Re: Set file attribute in Pascal Script?

You can call Windows API functions directly in Pascal Script.

The functions you are interested in are GetFileAttributesW and SetFileAttributesW.

Below is a script that will read common file attributes and will in insert them as text into the filename, for a demonstration:

function GetFileAttributesW(lpFileName: WideString): Cardinal;
  external 'GetFileAttributesW@kernel32.dll stdcall';

function SetFileAttributesW(lpFileName: WideString; dwFileAttributes: Cardinal): Cardinal;
  external 'SetFileAttributesW@kernel32.dll stdcall';

const
  FILE_ATTRIBUTE_READONLY             = $0000001;
  FILE_ATTRIBUTE_HIDDEN               = $0000002;
  FILE_ATTRIBUTE_SYSTEM               = $0000004;
  FILE_ATTRIBUTE_ARCHIVE              = $0000020;
  FILE_ATTRIBUTE_NORMAL               = $0000080;
  FILE_ATTRIBUTE_TEMPORARY            = $0000100;
  FILE_ATTRIBUTE_OFFLINE              = $0001000;

var
  Attributes: Cardinal;
  AttrText: String;

begin
  // Get file attributes
  AttrText := '';
  Attributes := GetFileAttributesW(FilePath);
  if (Attributes and FILE_ATTRIBUTE_READONLY <> 0) then
    AttrText := AttrText + 'R';
  if (Attributes and FILE_ATTRIBUTE_HIDDEN <> 0) then
    AttrText := AttrText + 'H';
  if (Attributes and FILE_ATTRIBUTE_SYSTEM <> 0) then
    AttrText := AttrText + 'S';
  if (Attributes and FILE_ATTRIBUTE_ARCHIVE <> 0) then
    AttrText := AttrText + 'A';
  if (Attributes and FILE_ATTRIBUTE_TEMPORARY <> 0) then
    AttrText := AttrText + 'T';
  if (Attributes and FILE_ATTRIBUTE_OFFLINE <> 0) then
    AttrText := AttrText + 'O';
  FileName := '[' + AttrText + '] ' + FileName;
end.

If you wish to change file attributes, replace the code body (between "begin" and "end") with something like this:

  // Set READONLY attribute
  Attributes := GetFileAttributesW(FilePath);
  Attributes := Attributes or FILE_ATTRIBUTE_READONLY;
  SetFileAttributesW(FilePath, Attributes);
  // Unset READONLY attribute
  Attributes := GetFileAttributesW(FilePath);
  Attributes := Attributes and (not FILE_ATTRIBUTE_READONLY);
  SetFileAttributesW(FilePath, Attributes);

Remember that the pascal script rule is executed every time a new name is generated!

Offline

Board footer

Powered by FluxBB