Recent

Author Topic: SOLVED: About VBScript & WScript code  (Read 3102 times)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: About VBScript code
« Reply #15 on: February 15, 2020, 05:40:29 pm »
Thanks marcov, your code works like a charm... ;)
Something else... Can you convert this simple code to Lazarus? please.
(Yes, I already know we can do this through Lazarus directly. I just want to see the convertion from .vbs.)
 
This code is to see if a file is read-only and, if it is not, marks it as read-only.
Best regards.

Code: Text  [Select][+][-]
  1. Set objFSO = CreateObject("Scripting.FileSystemObject")
  2. Set objFile = objFSO.GetFile("C:\MyFolder\TextFile.txt")
  3.  
  4. If objFile.Attributes = objFile.Attributes AND 1 Then
  5.     objFile.Attributes = objFile.Attributes XOR 1
  6. End If

You do realize that I figured most of this out while writing the first reply? It is just simple substitutions (paying attention to case, since this one of the few cases where Pascal is case sensitive), and looking up properties in MSDN.

I'm by no means a COM expert. Note that errorhandling will probably also be more complex

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2.  
  3. uses windows,variants,sysutils,activex,comobj;
  4.  
  5. var
  6.   objFSO,
  7.   objFile  :OLeVariant;
  8.   attr : integer;
  9. begin
  10.  objFSO := CreateOleObject('Scripting.FileSystemObject');
  11.  objFile := objFSO.GetFile('C:\repo\ul.txt');
  12.  attr:=objFile.Attributes;
  13.  writeln('Attributes: ',attr);
  14.  if (attr and 1)=0 then
  15.    objfile.Attributes:=attr xor 1;
  16. end.
  17.  


Inbetween I also wrote an iterator (something I hinted on in comment in the last example)

A for loop now looks like

Code: Pascal  [Select][+][-]
  1. var  
  2.     objlist,
  3.     obj     : Olevariant;
  4.     listiter :  oEnumIterator;
  5.  
  6.   for obj in listiter.Enumerate(objlist) do  // or e.g. objlist.Properties_  if you want to list properties.
  7.     writeln(Obj.Version);
  8.  

I attach the last example rewritten in that way. I also tried to do something with the date object in the last list based on some code on MSDN
« Last Edit: February 15, 2020, 05:48:19 pm by marcov »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: About VBScript & WScript code
« Reply #16 on: February 15, 2020, 05:41:14 pm »
Oh, the code in the zip probably requires FPC 3.2+

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: About VBScript code
« Reply #17 on: February 15, 2020, 05:54:38 pm »
This code is to see if a file is read-only and, if it is not, marks it as read-only.

That is easy to do with standard FPC functions:

Code: Pascal  [Select][+][-]
  1. program MkReadOnly;
  2.  
  3. uses
  4.   SysUtils;
  5.  
  6. var
  7.   Attr: LongInt;
  8.   AFile: String;
  9.  
  10. begin
  11.   if ParamCount <> 1 then
  12.     WriteLn('Sintax: MkReadOnly Filename')
  13.   else begin
  14.     AFile := Paramstr(1);
  15.     if not FileExists(AFile) then
  16.       WriteLn('Can''t find ', AFile)
  17.     else
  18.       if not FileIsReadOnly(AFile) then begin
  19.         Attr := FileGetAttr(AFile) or faReadOnly;
  20.         FileSetAttr(AFile, Attr);
  21.       end;
  22.   end;
  23. end.
« Last Edit: February 15, 2020, 05:58:21 pm 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.

SaraT

  • Full Member
  • ***
  • Posts: 124
  • A little student
Re: About VBScript & WScript code
« Reply #18 on: February 15, 2020, 09:11:49 pm »
Many thanks guys for your code. They work like a charm. ;)
That's all for now. I will be basing on these samples.

Hugs.

 

TinyPortal © 2005-2018