Author Topic: Operating on a text file with a lisp  (Read 2878 times)

0 Members and 1 Guest are viewing this topic.

xiaxiang

  • Guest
Operating on a text file with a lisp
« on: February 23, 2011, 02:14:20 AM »
The next two issues to ask for advice.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

     Firstly, I want to use one lisp routine to remove specific text file in the specific path, for example:[ C:\WINDOWS\ d.ini ]. Perform this procedure, check whether the file in the path  . If there are then delete it. Program is mainly used to kill a particular "as virus" file.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

     Secondly, it is more complex. I Want to get the system time with one lisp routine . But I should make it  not so clearly, I just want to make a cover-up. Specific requirements are:

Perform this procedure, first check whether there is specified text file in a specific path , if there are not then creat it, for example:[ C:\WINDOWS\ d.ini ].  if there are Then  skip this step. Get the System time and then write to the text file. The format of the time needs for requirements of 8 lines, 4 lines for Year numbers ,2 lines for Month numbers and  2 lines for  Day numbers . The figures for each line is expressed as Decimal,use "49-58" to represent "0-9".
Format Example:

      51 ----> 2

      49 ----> 0

      50 ----> 1

      50 ----> 1

      49 ----> 0

      51 ----> 2

      50 ----> 1

      54 ----> 5

So very clear, the system time is  February 15, 2011.

Waiting  for your help! Greetings!
« Last Edit: February 23, 2011, 02:32:17 AM by xiaxiang »

gile

  • Water Moccasin
  • Posts: 2283
  • Marseille, France
Re: Operating on a text file with a lisp
« Reply #1 on: February 23, 2011, 03:32:29 AM »
Hi,

For your first task, look at findfile and vl-file-delete functions.

For your second task, look at findfile, open, write-line and close functions.
To convert the date to ascii codes try:
(vl-string-to-list (menucmd "M=$(edtime, $(getvar,date),YYYY MO DD)"))
Speaking English as a French Frog

Stefan

  • Bull Frog
  • Posts: 255
Re: Operating on a text file with a lisp
« Reply #2 on: February 23, 2011, 04:05:31 AM »
First, all you have to do is (vl-file-delete "C:\\Windows\\d.ini"). If the file is there, it will be deleted. If not, nothing happend.

For second part, use
(setq open_file (open "C:\\Windows\\d.ini" "w") )

This will open the file if is there; otherwise the file will be created.

To write current date in opened file

(setq a (substr (rtos (getvar "CDATE") 2 0) 1 8 ) )
(foreach n (vl-string->list a)
  (princ n open_file)
  (princ "\n" open_file)
  )
(close open_file)

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Operating on a text file with a lisp
« Reply #3 on: February 23, 2011, 07:08:35 AM »
This may help as a reference for the DIESEL expression gile suggests.

xiaxiang

  • Guest
Re: Operating on a text file with a lisp
« Reply #4 on: February 23, 2011, 09:34:40 PM »
Hi,

For your first task, look at findfile and vl-file-delete functions.

For your second task, look at findfile, open, write-line and close functions.
To convert the date to ascii codes try:
((menucmd "M=$(edtime, $(getvar,date),YYYY MO DD)"))
Hi,gile
Thanks for your help.I couldn't find "vl-string-to-list".Maybe "vl-string->list"?

xiaxiang

  • Guest
Re: Operating on a text file with a lisp
« Reply #5 on: February 23, 2011, 09:57:52 PM »
First, all you have to do is (vl-file-delete "C:\\Windows\\d.ini"). If the file is there, it will be deleted. If not, nothing happend.

For second part, use
(setq open_file (open "C:\\Windows\\d.ini" "w") )

This will open the file if is there; otherwise the file will be created.

To write current date in opened file

(setq a (substr (rtos (getvar "CDATE") 2 0) 1 8 ) )
(foreach n (vl-string->list a)
  (princ n open_file)
  (princ "\n" open_file)
  )
(close open_file)

  Hi,phanaem
   Thanks for helping me with your first post in the swamp.
   I don't know why but the result which using your code is "50 48 49 49 48 50 50 52" for today.It's the Ascii Character codes.
   It need to be offseted.So I add the Algorithm "+1" to your code. Then I get the result "51 49 50 50 49 51 51 53" just for today 20110224.That's what I need.
   Then I post my code here.
Code: [Select]
(defun c:cs(/ X n open_file )
(setq open_file (open "C:\\Windows\\d.ini" "w") )
(setq a (substr (rtos (getvar "CDATE") 2 0) 1 8 ) )
;(foreach n (mapcar '(lambda (x) (+ x 1)) (vl-string->list a))
(foreach n (mapcar '1+ (vl-string->list a))
  (princ n open_file)
  (princ "\n" open_file)
  )
(close open_file)
)
Thanks 8-)
« Last Edit: February 23, 2011, 10:06:25 PM by xiaxiang »

xiaxiang

  • Guest
Re: Operating on a text file with a lisp
« Reply #6 on: February 23, 2011, 10:21:22 PM »
This may help as a reference for the DIESEL expression gile suggests.
Thanks,Lee.The more we learn,the better we use.

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Operating on a text file with a lisp
« Reply #7 on: February 24, 2011, 07:29:23 AM »
Perhaps:

Code: [Select]
(mapcar '1+ (vl-string->list (menucmd "m=$(edtime,$(getvar,DATE),YYYYMODD)")))

xiaxiang

  • Guest
Re: Operating on a text file with a lisp
« Reply #8 on: February 24, 2011, 10:10:16 AM »
Perhaps:

Code: [Select]
(mapcar '1+ (vl-string->list (menucmd "m=$(edtime,$(getvar,DATE),YYYYMODD)")))
Yes.It works well also.Thanks, Lee :-)