<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: LISP : Comparing strings from 2 text files in a auto-updater for acad.pgp in Visual LISP, AutoLISP and General Customization Forum</title>
    <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13278220#M4144</link>
    <description>&lt;LI-CODE lang="lisp"&gt;;; Script AutoLISP pour gérer les raccourcis acad.pgp et ajouter des lignes selon la langue
;; Variables pour les fichiers de langues
(setq file-fra "C:\\temp\\PGP-FRA.txt")  ;; Fichier pour le français
(setq file-enu "C:\\temp\\PGP-ENU.txt")  ;; Fichier pour l'anglais

(defun detect-language-and-setup-files ()
  (setq pgppath (findfile "acad.pgp"))  ;; Using pgppath consistently
  (if pgppath
      (progn
        (prompt (strcat "Fichier acad.pgp trouvé à : " pgppath "\n"))
        (prompt (strcat "Chemin converti en majuscules : " (strcase pgppath) "\n"))
        (cond ((vl-string-search "FRA" (strcase pgppath))
               (setq language-file file-fra)  ;; Fichier pour le français
               (prompt "Langue détectée : Français\n"))
              ((vl-string-search "ENU" (strcase pgppath))
               (setq language-file file-enu)  ;; Fichier pour l'anglais
               (prompt "Langue détectée : Anglais\n"))
              (t
               (setq language-file nil)
               (prompt "Langue inconnue : aucune action prise\n"))))
      (prompt "Fichier acad.pgp introuvable\n")))

(defun read-last-n-lines (filename n)
  (setq lines '())  ;; Initialize an empty list for lines
  (setq result '())  ;; Initialize an empty list for result
  (if (and filename (findfile filename))
      (progn
        (prompt (strcat "Ouverture du fichier : " filename "\n"))
        (setq file (open filename "r"))  ;; Open the file for reading
        (if file
            (progn
              (prompt "Lecture du fichier...\n")
              (while (setq line (read-line file))  ;; Ensure read-line is used correctly
                (setq lines (cons line lines)))  ;; Collect lines in reverse order
              (close file)  ;; Close the file after reading
              ;; Get the last n lines manually
              (setq result (if (&amp;lt;= n (length lines))
                               (mapcar (function (lambda (x) (nth x lines))) (iota n))  ;; Get last n lines
                               lines))  ;; If fewer than n lines, return all lines
              (prompt (strcat "Dernières lignes lues : " (apply 'strcat (mapcar 'strcat result)) "\n")))  ;; Debugging output
            (prompt "Erreur lors de l'ouverture du fichier.\n")))
      (prompt (strcat "Fichier introuvable : " filename "\n")))
  result)

;; Helper function to generate a list of numbers from 0 to n-1
(defun iota (n)
  (if (&amp;lt; n 1)
      nil
      (cons (- n 1) (iota (- n 1)))))

(defun clean-string (str)
  ;; Remove leading and trailing spaces, tabs, and newlines from the string
  (vl-string-trim "\n" (vl-string-trim "\t" (vl-string-trim " " str))))

(defun compare-last-n-lines (file1 file2 n)
  (let* ((lines1 (mapcar 'clean-string (read-last-n-lines file1 n)))
         (lines2 (mapcar 'clean-string (read-last-n-lines file2 n))))
    (prompt (strcat "Lignes de " file1 " : " (apply 'strcat (mapcar 'strcat lines1)) "\n"))
    (prompt (strcat "Lignes de " file2 " : " (apply 'strcat (mapcar 'strcat lines2)) "\n"))
    (equal lines1 lines2)))

(defun backup-and-append (source target)
  (if (and (findfile source) (findfile target))
      (progn
        (prompt (strcat "Fichier source trouvé : " source "\n"))
        (prompt (strcat "Fichier cible trouvé : " target "\n"))
        (setq timestamp (vl-string-right-trim "\n" (rtos (getvar "cdate") 2 0)))
        (setq backup-name (strcat (vl-filename-directory target) "\\" 
                                  (vl-filename-base target) "-" timestamp 
                                  (vl-filename-extension target)))
        ;; Créer une sauvegarde
        (vl-file-copy target backup-name)
        (prompt (strcat "Sauvegarde créée : " backup-name "\n"))
        ;; Ajouter le contenu
        (setq in (open source "r"))  ;; Open the source file for reading
        (setq out (open target "a"))  ;; Open the target file for appending
        (if (and in out)
            (progn
              (prompt "Ajout des lignes dans le fichier cible...\n")
              (while (not (eq (setq line (read-line in)) nil))
                (if (and line (not (null line)))
                    (write-line (strcat line) out)))  ;; Write each line from the source file to the target file
              (close in)
              (close out)
              (prompt "Contenu ajouté avec succès.\n"))
            (prompt "Erreur lors de l'ouverture des fichiers.\n")))
      (prompt "Un des fichiers est introuvable\n")))

(defun check-and-append ()
  (setq pgppath (findfile "acad.pgp"))  ;; Using pgppath consistently
  (if pgppath
      (progn
        (prompt (strcat "Fichier acad.pgp trouvé à : " pgppath "\n"))
        (if language-file
            (progn
              (setq pgp-lines (mapcar 'clean-string (read-last-n-lines pgppath 3)))
              (prompt (strcat "Dernières lignes de acad.pgp lues : " (apply 'strcat (mapcar 'strcat pgp-lines)) "\n"))
              (setq lang-lines (mapcar 'clean-string (read-last-n-lines language-file 3)))
              (prompt (strcat "Dernières lignes du fichier de langue lues : " (apply 'strcat (mapcar 'strcat lang-lines)) "\n"))
              (if (equal pgp-lines lang-lines)
                  (prompt "Les 3 dernières lignes sont identiques. Aucune modification nécessaire.\n")
                  (progn
                    (prompt "Les lignes diffèrent, ajout nécessaire.\n")
                    (backup-and-append language-file pgppath))))
            (prompt "Fichier de langue introuvable.\n")))
      (prompt "Fichier acad.pgp introuvable.\n")))

;; Exemple d'utilisation
(detect-language-and-setup-files)
(check-and-append)

(setvar "RE-INIT" 16)
(prompt "Mise a jour PGP effectue\n")&lt;/LI-CODE&gt;&lt;P&gt;The corrected code :&lt;/P&gt;</description>
    <pubDate>Thu, 23 Jan 2025 14:20:35 GMT</pubDate>
    <dc:creator>cyberflow</dc:creator>
    <dc:date>2025-01-23T14:20:35Z</dc:date>
    <item>
      <title>LISP : Comparing strings from 2 text files in a auto-updater for acad.pgp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13272345#M4137</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I've done a little lisp that compares my custom shortcuts for PGP (PGP-ENU.txt and PGP-FRA.txt) and appends it to my acad.pgp.&lt;BR /&gt;&lt;BR /&gt;Now it doesn't seem to compare correctly both depending on the language used (pulls out "enu" or "fra" in the path of acad.pgp) and appends to acad.pgp the correct PGP-*.txt file.&lt;BR /&gt;&lt;BR /&gt;Now my code keeps always appending the PGP-*.txt files in acad.pgp even if both endings are identical&lt;BR /&gt;&lt;BR /&gt;Is there anything specific thing that needs to be done to compare several text lines in a text file ?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Here's the source :&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="lisp"&gt;;; Script AutoLISP pour gérer les raccourcis acad.pgp et ajouter des lignes selon la langue
;; Variables pour les fichiers de langues
(setq file-fra "C:\\temp\\PGP-FRA.txt")  ;; Fichier pour le français
(setq file-enu "C:\\temp\\PGP-ENU.txt")  ;; Fichier pour l'anglais

(defun detect-language-and-setup-files ()
  (setq pgppath (findfile "acad.pgp"))  ;; Using pgppath consistently
  (if pgppath
      (progn
        (prompt (strcat "Fichier acad.pgp trouvé à : " pgppath "\n"))
        (prompt (strcat "Chemin converti en majuscules : " (strcase pgppath) "\n"))
        (cond ((vl-string-search "FRA" (strcase pgppath))
               (setq language-file file-fra)  ;; Fichier pour le français
               (prompt "Langue détectée : Français\n"))
              ((vl-string-search "ENU" (strcase pgppath))
               (setq language-file file-enu)  ;; Fichier pour l'anglais
               (prompt "Langue détectée : Anglais\n"))
              (t
               (setq language-file nil)
               (prompt "Langue inconnue : aucune action prise\n"))))
      (prompt "Fichier acad.pgp introuvable\n")))

(defun read-last-n-lines (filename n)
  (setq lines '())  ;; Initialize an empty list for lines
  (setq result '())  ;; Initialize an empty list for result
  (if (and filename (findfile filename))
      (progn
        (prompt (strcat "Ouverture du fichier : " filename "\n"))
        (setq file (open filename "r"))  ;; Open the file for reading
        (if file
            (progn
              (prompt "Lecture du fichier...\n")
              (while (setq line (read-line file))  ;; Ensure read-line is used correctly
                (setq lines (cons line lines)))  ;; Collect lines in reverse order
              (close file)  ;; Close the file after reading
              ;; Get the last n lines manually
              (setq lines (reverse lines))  ;; Reverse the list to get the correct order
              (setq result (if (&amp;lt;= n (length lines))
                               (mapcar (function (lambda (x) (nth x lines))) (iota n))  ;; Get last n lines
                               lines))  ;; If fewer than n lines, return all lines
              (prompt (strcat "Dernières lignes lues : " (apply 'strcat (mapcar 'strcat result)) "\n")))  ;; Debugging output
            (prompt "Erreur lors de l'ouverture du fichier.\n")))
      (prompt (strcat "Fichier introuvable : " filename "\n")))
  result)

;; Helper function to generate a list of numbers from 0 to n-1
(defun iota (n)
  (if (&amp;lt; n 1)
      nil
      (cons (- n 1) (iota (- n 1)))))

(defun clean-string (str)
  ;; Remove leading and trailing spaces, tabs, and newlines from the string
  (vl-string-trim "\n" (vl-string-trim "\t" (vl-string-trim " " str))))

(defun compare-last-n-lines (file1 file2 n)
  (let* ((lines1 (mapcar 'clean-string (read-last-n-lines file1 n)))
         (lines2 (mapcar 'clean-string (read-last-n-lines file2 n))))
    (prompt (strcat "Lignes de " file1 " : " (apply 'strcat (mapcar 'strcat lines1)) "\n"))
    (prompt (strcat "Lignes de " file2 " : " (apply 'strcat (mapcar 'strcat lines2)) "\n"))
    (equal lines1 lines2)))

(defun backup-and-append (source target)
  (if (and (findfile source) (findfile target))
      (progn
        (prompt (strcat "Fichier source trouvé : " source "\n"))
        (prompt (strcat "Fichier cible trouvé : " target "\n"))
        (setq timestamp (vl-string-right-trim "\n" (rtos (getvar "cdate") 2 0)))
        (setq backup-name (strcat (vl-filename-directory target) "\\" 
                                  (vl-filename-base target) "-" timestamp 
                                  (vl-filename-extension target)))
        ;; Créer une sauvegarde
        (vl-file-copy target backup-name)
        (prompt (strcat "Sauvegarde créée : " backup-name "\n"))
        ;; Ajouter le contenu
        (setq in (open source "r"))  ;; Open the source file for reading
        (setq out (open target "a"))  ;; Open the target file for appending
        (if (and in out)
            (progn
              (prompt "Ajout des lignes dans le fichier cible...\n")
              (while (not (eq (setq line (read-line in)) nil))
                (if (and line (not (null line)))
                    (write-line (strcat line) out)))  ;; Write each line from the source file to the target file
              (close in)
              (close out)
              (prompt "Contenu ajouté avec succès.\n"))
            (prompt "Erreur lors de l'ouverture des fichiers.\n")))
      (prompt "Un des fichiers est introuvable\n")))

(defun check-and-append ()
  (setq pgppath (findfile "acad.pgp"))  ;; Using pgppath consistently
  (if pgppath
      (progn
        (prompt (strcat "Fichier acad.pgp trouvé à : " pgppath "\n"))
        (if language-file
            (progn
              (setq pgp-lines (mapcar 'clean-string (read-last-n-lines pgppath 3)))
              (prompt (strcat "Dernières lignes de acad.pgp lues : " (apply 'strcat (mapcar 'strcat pgp-lines)) "\n"))
              (setq lang-lines (mapcar 'clean-string (read-last-n-lines language-file 3)))
              (prompt (strcat "Dernières lignes du fichier de langue lues : " (apply 'strcat (mapcar 'strcat lang-lines)) "\n"))
              (if (equal pgp-lines lang-lines)
                  (prompt "Les 3 dernières lignes sont identiques. Aucune modification nécessaire.\n")
                  (progn
                    (prompt "Les lignes diffèrent, ajout nécessaire.\n")
                    (backup-and-append language-file pgppath))))
            (prompt "Fichier de langue introuvable.\n")))
      (prompt "Fichier acad.pgp introuvable.\n")))

;; Exemple d'utilisation
(detect-language-and-setup-files)
(check-and-append)

(setvar "RE-INIT" 16)
(prompt "Mise a jour PGP effectue\n")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;We can see the last lines in both files are the same ...&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="cyberflow_0-1737411776897.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1457065i862B51237B1F4693/image-size/medium?v=v2&amp;amp;px=400" role="button" title="cyberflow_0-1737411776897.png" alt="cyberflow_0-1737411776897.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;For the curious ones, i'm doing this cause i'm tired of going to do copy paste and updating this each time i reinstal Autocad.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jan 2025 22:24:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13272345#M4137</guid>
      <dc:creator>cyberflow</dc:creator>
      <dc:date>2025-01-20T22:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: LISP : Comparing strings from 2 text files in a auto-updater for acad.pgp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13272623#M4138</link>
      <description>&lt;P&gt;There is an oldfashioned dos command "copy file1+file2 file3" this could be ran as a shell command from CAD, rename the acadpgp after making file3, then rename file3 to acadpgp. The pgp is in a known location.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Like you I have a setup lisp that does lots of stuff after an upgrade. Is it easier the way I would approach to have a custom startup lisp with all the shorctuts defined in it and gets loaded via Appload, Startup suite. Mine has 37 defuns some programs, some 1 liner shortcuts. I try and leave anything to do with out the box alone.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2025 03:01:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13272623#M4138</guid>
      <dc:creator>Sea-Haven</dc:creator>
      <dc:date>2025-01-21T03:01:18Z</dc:date>
    </item>
    <item>
      <title>Re: LISP : Comparing strings from 2 text files in a auto-updater for acad.pgp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13273723#M4139</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/6254908"&gt;@Sea-Haven&lt;/a&gt;&amp;nbsp;Hi there, really a nice approach to this but i'd have the same problem.&lt;BR /&gt;&lt;BR /&gt;Is your script running at each time autocad is launched ?&lt;BR /&gt;If it's launched at each launch, how do you manage to prevent it to copy the file if it's already done ?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2025 14:46:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13273723#M4139</guid>
      <dc:creator>cyberflow</dc:creator>
      <dc:date>2025-01-21T14:46:13Z</dc:date>
    </item>
    <item>
      <title>Re: LISP : Comparing strings from 2 text files in a auto-updater for acad.pgp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13274167#M4140</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1657496"&gt;@cyberflow&lt;/a&gt;&amp;nbsp;hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i do not use (equal) to compare strings, i think this is where the compare fails. replace it with '=' or (eq)&lt;/P&gt;&lt;P&gt;AutoLISP has not (let* ) function (on line #57)&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to get more help, post your txt + files&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Moshe&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2025 18:09:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13274167#M4140</guid>
      <dc:creator>Moshe-A</dc:creator>
      <dc:date>2025-01-21T18:09:36Z</dc:date>
    </item>
    <item>
      <title>Re: LISP : Comparing strings from 2 text files in a auto-updater for acad.pgp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13274270#M4141</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/52747"&gt;@Moshe-A&lt;/a&gt;&amp;nbsp;!&lt;BR /&gt;&lt;BR /&gt;I did found the error with some help from &lt;A href="https://www.theswamp.org/index.php?topic=59971.new#new" target="_blank" rel="noopener"&gt;VovKa on TheSwamp forum&lt;/A&gt; !&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;Basicly i needed to remove this line : "(setq lines (reverse lines))"&lt;BR /&gt;&lt;BR /&gt;It was always comparing two different list one was reversed and the other one not.&lt;BR /&gt;&lt;BR /&gt;The code works correctly now.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2025 18:53:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13274270#M4141</guid>
      <dc:creator>cyberflow</dc:creator>
      <dc:date>2025-01-21T18:53:58Z</dc:date>
    </item>
    <item>
      <title>Re: LISP : Comparing strings from 2 text files in a auto-updater for acad.pgp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13277194#M4142</link>
      <description>&lt;P&gt;When you use Appload you save the lsp to the "Startup Suite" so it gets loaded every time you open a dwg. You would only have your custom shortcuts in the custom lsp file.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Examples&lt;/P&gt;&lt;LI-CODE lang="general"&gt;(defun c:od () ;;opens new  dwg in current dwg  directory
(startapp "explorer" (getvar "dwgprefix"))
(princ)
)

(defun C:FD () (setvar "filedia" 1))

(defun C:j ()(command "Pedit" "J"))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jan 2025 02:17:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13277194#M4142</guid>
      <dc:creator>Sea-Haven</dc:creator>
      <dc:date>2025-01-23T02:17:18Z</dc:date>
    </item>
    <item>
      <title>Re: LISP : Comparing strings from 2 text files in a auto-updater for acad.pgp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13278215#M4143</link>
      <description>&lt;P&gt;Thx for the tip&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/6254908"&gt;@Sea-Haven&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jan 2025 14:17:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13278215#M4143</guid>
      <dc:creator>cyberflow</dc:creator>
      <dc:date>2025-01-23T14:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: LISP : Comparing strings from 2 text files in a auto-updater for acad.pgp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13278220#M4144</link>
      <description>&lt;LI-CODE lang="lisp"&gt;;; Script AutoLISP pour gérer les raccourcis acad.pgp et ajouter des lignes selon la langue
;; Variables pour les fichiers de langues
(setq file-fra "C:\\temp\\PGP-FRA.txt")  ;; Fichier pour le français
(setq file-enu "C:\\temp\\PGP-ENU.txt")  ;; Fichier pour l'anglais

(defun detect-language-and-setup-files ()
  (setq pgppath (findfile "acad.pgp"))  ;; Using pgppath consistently
  (if pgppath
      (progn
        (prompt (strcat "Fichier acad.pgp trouvé à : " pgppath "\n"))
        (prompt (strcat "Chemin converti en majuscules : " (strcase pgppath) "\n"))
        (cond ((vl-string-search "FRA" (strcase pgppath))
               (setq language-file file-fra)  ;; Fichier pour le français
               (prompt "Langue détectée : Français\n"))
              ((vl-string-search "ENU" (strcase pgppath))
               (setq language-file file-enu)  ;; Fichier pour l'anglais
               (prompt "Langue détectée : Anglais\n"))
              (t
               (setq language-file nil)
               (prompt "Langue inconnue : aucune action prise\n"))))
      (prompt "Fichier acad.pgp introuvable\n")))

(defun read-last-n-lines (filename n)
  (setq lines '())  ;; Initialize an empty list for lines
  (setq result '())  ;; Initialize an empty list for result
  (if (and filename (findfile filename))
      (progn
        (prompt (strcat "Ouverture du fichier : " filename "\n"))
        (setq file (open filename "r"))  ;; Open the file for reading
        (if file
            (progn
              (prompt "Lecture du fichier...\n")
              (while (setq line (read-line file))  ;; Ensure read-line is used correctly
                (setq lines (cons line lines)))  ;; Collect lines in reverse order
              (close file)  ;; Close the file after reading
              ;; Get the last n lines manually
              (setq result (if (&amp;lt;= n (length lines))
                               (mapcar (function (lambda (x) (nth x lines))) (iota n))  ;; Get last n lines
                               lines))  ;; If fewer than n lines, return all lines
              (prompt (strcat "Dernières lignes lues : " (apply 'strcat (mapcar 'strcat result)) "\n")))  ;; Debugging output
            (prompt "Erreur lors de l'ouverture du fichier.\n")))
      (prompt (strcat "Fichier introuvable : " filename "\n")))
  result)

;; Helper function to generate a list of numbers from 0 to n-1
(defun iota (n)
  (if (&amp;lt; n 1)
      nil
      (cons (- n 1) (iota (- n 1)))))

(defun clean-string (str)
  ;; Remove leading and trailing spaces, tabs, and newlines from the string
  (vl-string-trim "\n" (vl-string-trim "\t" (vl-string-trim " " str))))

(defun compare-last-n-lines (file1 file2 n)
  (let* ((lines1 (mapcar 'clean-string (read-last-n-lines file1 n)))
         (lines2 (mapcar 'clean-string (read-last-n-lines file2 n))))
    (prompt (strcat "Lignes de " file1 " : " (apply 'strcat (mapcar 'strcat lines1)) "\n"))
    (prompt (strcat "Lignes de " file2 " : " (apply 'strcat (mapcar 'strcat lines2)) "\n"))
    (equal lines1 lines2)))

(defun backup-and-append (source target)
  (if (and (findfile source) (findfile target))
      (progn
        (prompt (strcat "Fichier source trouvé : " source "\n"))
        (prompt (strcat "Fichier cible trouvé : " target "\n"))
        (setq timestamp (vl-string-right-trim "\n" (rtos (getvar "cdate") 2 0)))
        (setq backup-name (strcat (vl-filename-directory target) "\\" 
                                  (vl-filename-base target) "-" timestamp 
                                  (vl-filename-extension target)))
        ;; Créer une sauvegarde
        (vl-file-copy target backup-name)
        (prompt (strcat "Sauvegarde créée : " backup-name "\n"))
        ;; Ajouter le contenu
        (setq in (open source "r"))  ;; Open the source file for reading
        (setq out (open target "a"))  ;; Open the target file for appending
        (if (and in out)
            (progn
              (prompt "Ajout des lignes dans le fichier cible...\n")
              (while (not (eq (setq line (read-line in)) nil))
                (if (and line (not (null line)))
                    (write-line (strcat line) out)))  ;; Write each line from the source file to the target file
              (close in)
              (close out)
              (prompt "Contenu ajouté avec succès.\n"))
            (prompt "Erreur lors de l'ouverture des fichiers.\n")))
      (prompt "Un des fichiers est introuvable\n")))

(defun check-and-append ()
  (setq pgppath (findfile "acad.pgp"))  ;; Using pgppath consistently
  (if pgppath
      (progn
        (prompt (strcat "Fichier acad.pgp trouvé à : " pgppath "\n"))
        (if language-file
            (progn
              (setq pgp-lines (mapcar 'clean-string (read-last-n-lines pgppath 3)))
              (prompt (strcat "Dernières lignes de acad.pgp lues : " (apply 'strcat (mapcar 'strcat pgp-lines)) "\n"))
              (setq lang-lines (mapcar 'clean-string (read-last-n-lines language-file 3)))
              (prompt (strcat "Dernières lignes du fichier de langue lues : " (apply 'strcat (mapcar 'strcat lang-lines)) "\n"))
              (if (equal pgp-lines lang-lines)
                  (prompt "Les 3 dernières lignes sont identiques. Aucune modification nécessaire.\n")
                  (progn
                    (prompt "Les lignes diffèrent, ajout nécessaire.\n")
                    (backup-and-append language-file pgppath))))
            (prompt "Fichier de langue introuvable.\n")))
      (prompt "Fichier acad.pgp introuvable.\n")))

;; Exemple d'utilisation
(detect-language-and-setup-files)
(check-and-append)

(setvar "RE-INIT" 16)
(prompt "Mise a jour PGP effectue\n")&lt;/LI-CODE&gt;&lt;P&gt;The corrected code :&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jan 2025 14:20:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-comparing-strings-from-2-text-files-in-a-auto-updater-for/m-p/13278220#M4144</guid>
      <dc:creator>cyberflow</dc:creator>
      <dc:date>2025-01-23T14:20:35Z</dc:date>
    </item>
  </channel>
</rss>

