title: File.exists() in a Jenkins groovy file does not work
body: <p>I want to create a groovy function in my Jenkins job that looks into a folder and deletes all files who are older than X days.</p>
  <p>So I start looking in the internet and found different kind of solutions.</p>
  <p>At first I create a <code>.groovy</code> file with Visual Studio Code on my local PC to understand how it works. That is the reason why my code looks not similar to the codes in the internet because I changed it so that I understand how the code works.</p>
  <pre><code>def deleteFilesOlderThanDays(int daysBack, String path) {
    def DAY_IN_MILLIS = 24 * 60 * 60 * 1000
    File directory = new File(path)
  
    if(directory.exists()){
      File[] listFiles = directory.listFiles()
      
      for(File listFile : listFiles) {
        def days_from_now = ( (System.currentTimeMillis() - listFile.lastModified()) /(DAY_IN_MILLIS))
        if(days_from_now &gt; daysBack) {
          println('------------')
          println('file is older')
          println(listFile)
          }
          else{
            println('------------')
            println('File is not older')
            println(listFile)
          }
        }//End: for(File listFile : listFiles) {
      }//End: if(directory.exists()){
  }
  </code></pre>
  <p>(I know, the code do not delete something. It is only for my understanding)</p>
  <p>The second step was to include this new created function into my Jenkins groovy file. But since then I'm desperate.</p>
  <p>I have the problem that I do not get a positive result at the beginning from the code if the folder really exist.</p>
  <p>The line:</p>
  <pre><code>if(directory.exists()){
  </code></pre>
  <p>makes me a lot of problems and it is not clear for me why.</p>
  <p>I have tried so many kind of versions but I haven’t found a solution for me.
  I have also used the “Pipeline Syntax” example [Sample Step fileExists] but it doesn’t help for me.</p>
  <p>I have included:</p>
  <pre><code>import java.io.File
  </code></pre>
  <p>At the beginning of my file.</p>
  <p>I have a basic file which I include in the Jenkins job. This file includes my library files. One of this library files is the <code>file.groovy</code>. In the basic Jenkins file I execute the function <code>file.deleteFilesOlderThanDays()</code> (for testing I do not use any parameters).</p>
  <p>The code from my function for testing is:</p>
  <pre><code>def deleteFilesOlderThanDays() {
  
    dir = '.\\ABC'
    echo &quot;1. ----------------------------------------&quot;
    File directory1 = new File('.\\ABC\\')
    exist = directory1.exists()
    echo 'Directory1 name is = '+directory1
    echo 'exist value is = '+exist
    echo &quot;2. ----------------------------------------&quot;
    File directory2 = new File('.\\ABC')
    exist = directory2.exists()
    echo 'Directory2 name is = '+directory2
    echo 'exist value is = '+exist
    echo &quot;3. ----------------------------------------&quot;  
    File directory3 = new File(dir)
    exist = directory3.exists()
    echo 'Directory3 name is = '+directory3
    echo 'exist value is = '+exist
    echo &quot;4. Pipeline Syntax ------------------------&quot;  
    exist = fileExists '.\\ABC'
    echo 'exist value is = '+exist
    echo &quot;5. ----------------------------------------&quot;
    File directory5 = new File(dir)
    echo 'Directory5 name is = '+directory5
    // execute an error
    // exist = fileExists(directory5)
    exist = fileExists &quot;directory5&quot;
    echo 'exist value is = '+exist
    echo &quot;6. ----------------------------------------&quot;
    exist = fileExists(dir)
    echo 'exist value is = '+exist
  
    File[] listFiles = directory5.listFiles()
    echo 'List file = '+listFiles
  }
  </code></pre>
  <p>And the Output in the Jenkins Console Output is: (I cleaned it a little bit up….)</p>
  <pre><code>1. ----------------------------------------
  Directory1 name is = .\ABC\
  exist value is = false
  2. ----------------------------------------
  Directory2 name is = .\ABC
  exist value is = false
  3. ----------------------------------------
  Directory3 name is = .\ABC
  exist value is = false
  4. Pipeline Syntax ------------------------
  exist value is = true
  5. ----------------------------------------
  Directory5 name is = .\ABC
  exist value is = false
  6. ----------------------------------------
  exist value is = true
  
  List file = null
  </code></pre>
  <p>I only get a true value in step 4 and 6. So I can be sure that the folder really exist.</p>
  <p>So it seems to be for me that the command:</p>
  <pre><code>File directory = new File(dir)
  </code></pre>
  <p>Not work correct in my case.</p>
  <p>I can’t create a <code>listFile</code> variable because the directory would not be initialized correct.</p>
  <p>For me is also not clear which kind of commands I should use. The groovy examples use always functions like:</p>
  <pre><code> .exists()
  </code></pre>
  <p>But in the Jenkins examples I always find code like this:</p>
  <pre><code>  fileExists()
  </code></pre>
  <p>Why there are some differences between groovy and Jenkins groovy style? It should be the same ore not?</p>
  <p>Does anyone have an idea for me or can told me what I’m doing wrong?</p>