Author Topic: Create random file only create 64 files  (Read 1537 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Create random file only create 64 files
« on: March 26, 2008, 02:03:37 PM »
I did this in response to Marks challenge here.  The problem is that it will only create a max of 64 files, and I'm at a loss as to why.  I put in a counter at one point, so I know it is running the full amount, but it stops creating the files after 64.  Here is the code.

Thanks.

Code: [Select]
(defun MakeFiles (iLet iDig DirPath FileExt Amount / cntLet cntDig cnt cntEnd LetList DigList CombindList
    FileName DigRange *error*)
   
    (defun *error* (msg)
        (vl-bt)
        (print msg)
    )
    ;----------------------------------------------------------------------
    (defun RandomGenerator (Num)
        (*
            (/
                (* Num 16378.11)
                11
            )
            33.99
        )
    )
    ;----------------------------------------------------------------------
    (setq cnt (ascii "a"))
    (setq cntEnd (ascii "z"))
    (repeat (1+ (- cntEnd cnt))
        (setq LetList (cons (chr cnt) LetList))
        (setq cnt (1+ cnt))
    )
   
    (setq cnt (ascii "A"))
    (setq cntEnd (ascii "Z"))
    (repeat (1+ (- cntEnd cnt))
        (setq LetList (cons (chr cnt) LetList))
        (setq cnt (1+ cnt))
    )
   
    (setq cnt (ascii "0"))
    (setq cntEnd (ascii "9"))
    (repeat (1+ (setq DigRange (- cntEnd cnt)))
        (setq DigList (cons (chr cnt) DigList))
        (setq cnt (1+ cnt))
    )
   
    (setq CombindList (append DigList LetList))
    (setq cntName (+ iLet iDig))
    (setq RandNum (RandomGenerator (getvar 'MilliSecs)))
    (repeat Amount
        (setq RandNum (sqrt RandNum))
        (setq FileName "")
        (setq cntLet 0)
        (setq cntDig 0)
        (while (not (equal cntName (strlen FileName)))
            (setq FileName
                (strcat
                    FileName
                    (cond
                        (
                            (and
                                (< cntLet iLet)
                                (< cntDig iDig)
                            )
                            (setq RandNum (RandomGenerator (+ RandNum 623546.33)))
                            (setq Num (fix (rem RandNum (length CombindList))))
                            (if (< 0 Num DigRange)
                                (setq cntDig (1+ cntDig))
                                (setq cntLet (1+ cntLet))
                            )
                            (nth Num CombindList)
                        )
                        ((< cntLet iLet)
                            (setq RandNum (RandomGenerator (+ RandNum 556325447.66)))
                            (setq Num (fix (rem RandNum (length LetList))))
                            (setq cntLet (1+ cntLet))
                            (nth Num LetList)
                        )
                        ((< cntDig iDig)
                            (setq RandNum (RandomGenerator (+ RandNum 75625145.99)))
                            (setq Num (fix (rem RandNum (length DigList))))
                            (setq cntDig (1+ cntDig))
                            (nth Num DigList)
                        )
                    )
                )
            )
        )
        (close
            (open (strcat DirPath FileName FileExt) "w")
        )
    )
    (princ)
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Jeff_M

  • King Gator
  • Posts: 4098
  • C3D user & customizer
Re: Create random file only create 64 files
« Reply #1 on: March 26, 2008, 02:29:20 PM »
Tim, replace the (close (open ..)) with this and see what happens.....
Code: [Select]
(if (not (findfile (strcat DirPath FileName FileExt)))
        (close
            (open (strcat DirPath FileName FileExt) "w")
        )
(princ (strcat "\nFile \"" FileName FileExt "\" already exists."))
)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Create random file only create 64 files
« Reply #2 on: March 26, 2008, 02:34:00 PM »
I was thinking that might be a problem, but then I thought it would error, but it doesn't error.  I just added this, and it didn't work.

Code: [Select]
        (if (not (vl-position FileName FileNameList))
            (progn
                (close
                    (open (strcat DirPath FileName FileExt) "w")
                )
                (setq FileNameList (cons FileName FileNameList))
            )
        )
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Create random file only create 64 files
« Reply #3 on: March 26, 2008, 02:38:15 PM »
Good call Jeff.  Seems I need to work on the random number generator.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Create random file only create 64 files
« Reply #4 on: March 26, 2008, 02:45:49 PM »
Here is the new version.  It created 10,000 text files without one error.  Error being trying to have the same name more than once.

Code: [Select]
(defun MakeFiles (iLet iDig DirPath FileExt Amount / cntLet cntDig cnt cntEnd LetList DigList CombindList
    FileName DigRange *error* FileNameList cntMain cntError)
   
    (defun *error* (msg)
        (vl-bt)
        (print msg)
    )
    ;----------------------------------------------------------------------
    (defun RandomGenerator (Num)
        (*
            (/
                (* Num (sqrt (getvar 'MilliSecs)))
                11
            )
            33.99
        )
    )
    ;----------------------------------------------------------------------
    (setq cnt (ascii "a"))
    (setq cntEnd (ascii "z"))
    (repeat (1+ (- cntEnd cnt))
        (setq LetList (cons (chr cnt) LetList))
        (setq cnt (1+ cnt))
    )
   
    (setq cnt (ascii "A"))
    (setq cntEnd (ascii "Z"))
    (repeat (1+ (- cntEnd cnt))
        (setq LetList (cons (chr cnt) LetList))
        (setq cnt (1+ cnt))
    )
   
    (setq cnt (ascii "0"))
    (setq cntEnd (ascii "9"))
    (repeat (1+ (setq DigRange (- cntEnd cnt)))
        (setq DigList (cons (chr cnt) DigList))
        (setq cnt (1+ cnt))
    )
   
    (setq CombindList (append DigList LetList))
    (setq cntName (+ iLet iDig))
    (setq RandNum (RandomGenerator (getvar 'MilliSecs)))
    (setq cntMain 0)
    (setq cntError 0)
    (while (< cntMain Amount)
        (setq RandNum (sqrt RandNum))
        (setq FileName "")
        (setq cntLet 0)
        (setq cntDig 0)
        (while (not (equal cntName (strlen FileName)))
            (setq FileName
                (strcat
                    FileName
                    (cond
                        (
                            (and
                                (< cntLet iLet)
                                (< cntDig iDig)
                            )
                            (setq RandNum (RandomGenerator (+ RandNum 623546.33)))
                            (setq Num (fix (rem RandNum (length CombindList))))
                            (if (< 0 Num DigRange)
                                (setq cntDig (1+ cntDig))
                                (setq cntLet (1+ cntLet))
                            )
                            (nth Num CombindList)
                        )
                        ((< cntLet iLet)
                            (setq RandNum (RandomGenerator (+ RandNum 556325447.66)))
                            (setq Num (fix (rem RandNum (length LetList))))
                            (setq cntLet (1+ cntLet))
                            (nth Num LetList)
                        )
                        ((< cntDig iDig)
                            (setq RandNum (RandomGenerator (+ RandNum 75625145.99)))
                            (setq Num (fix (rem RandNum (length DigList))))
                            (setq cntDig (1+ cntDig))
                            (nth Num DigList)
                        )
                    )
                )
            )
        )
        (if (not (vl-position FileName FileNameList))
            (progn
                (close
                    (open (strcat DirPath FileName FileExt) "w")
                )
                (setq FileNameList (cons FileName FileNameList))
                (setq cntMain (1+ cntMain))
            )
            (setq cntError (1+ cntError))
        )
        (if (> cntError 100)
            (setq cntMain (1+ Amount))
        )
    )
    (if (> cntError 0)
        (prompt (strcat "\n Had to generate new file name " (itoa cntError) " times."))
    )
    (princ)
)
Called like
Code: [Select]
(MakeFiles 10 12 "c:\\temp\\" ".txt" 10000)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.