im not sure that that was your question but fileExists() works for directories as well
Answer from balaganAtomi on Stack Overflowim not sure that that was your question but fileExists() works for directories as well
fileExists(target_dir) in pipeline checks the given file in workspace, it is not permitted to access the outside.
to do this, the sh or bat in pipeline can help verify if the target directory exists by calling shell command.
res = sh(script: "test -d ${target_dir} && echo '1' || echo '0' ", returnStdout: true).trim()
if(res=='1'){
echo 'yes'
} else {
echo 'no'
}
You need to use brackets when using the fileExists step in an if condition or assign the returned value to a variable
Using variable:
def exists = fileExists 'file'
if (exists) {
echo 'Yes'
} else {
echo 'No'
}
Using brackets:
if (fileExists('file')) {
echo 'Yes'
} else {
echo 'No'
}
The keyword "return" must be used
stage('some stage') {
when { expression { return fileExists ('myfile') } }
steps {
echo "file exists"
}
}
You may benefit from this answer from a similar question:
"
java.io.File methods will refer to files on the master where Jenkins is running, so not in the current workspace on the slave machine.
To refer to files on the slave machine, you should use the readFile method "
def dir = readFile("${WORKSPACE}/ABC");
Link to original answer
Thanks for all that feedback.
OK, for me is now clear that Jenkins Groovy != Groovy is. I have read a lot about it that there are different command if you are executing file search on a Jenkins Master or on a Jenkins Slave.
The suggestion from Youg to start after confirmation helps me. I had problems with deleting the file so at the end I used a primitive batch command to get my function run.
The finally functions looks like now:
def deleteFilesOlderThanXDays(daysBack, path) {
def DAY_IN_MILLIS = 24 * 60 * 60 * 1000
if(fileExists(path)){
// change into path
dir(path) {
// find all kind of files
files = findFiles(glob: '*.*')
for (int i = 0; i < files.length; i++) {
def days_from_now = ( (System.currentTimeMillis() - files[i].lastModified) /(DAY_IN_MILLIS))
if(days_from_now > daysBack) {
echo('file : >>'+files[i].name+'<< is older than '+daysBack+' days')
bat('del /F /Q "'+files[i].name+'"')
}
else{
echo('file : >>'+files[i].name+'<< is not only than '+daysBack+' days')
}
}// End: for (int i = 0; i < files.length; i++) {
}// End: dir(path) {
}// End: if(fileExists(path)){
}
Thanks for helping and best regards,
Use the below:-
def exists = fileExists '/root/elp/test.php'
if (exists) {
sh "LINUX SHELL COMMAND"
} else {
println "File doesn't exist"
}
And you can follow check-if-a-file-exists-in-jenkins-pipeline
And you can also use below:-
def exitCode = sh script: 'find -name "*.zip" | egrep .', returnStatus: true
boolean exists = exitCode == 0
The following example is when using Jenkins declarative pipeline:
pipeline{
agent any
environment{
MY_FILE = fileExists '/tmp/myfile'
}
stages{
stage('conditional if exists'){
when { expression { MY_FILE == 'true' } }
steps {
echo "file exists"
}
}
stage('conditional if not exists'){
when { expression { MY_FILE == 'false' } }
steps {
echo "file does not exist"
}
}
}
}
Alternatively for scripted pipeline you may find this bash syntax to check if file exists useful.
As far as I remember, Java's File object either won't work in sandboxed mode, or will run only on master node. If you want to check for folder on a slave node, you need to invoke Pipeline methods, or pure shell.
The below code works for me. You had echo instead of println, I'm not au fait with Groovy in Jenkins but in pure Groovy there is no echo that I am aware of. Also, i moved the statement def file = new File(folderName) to before the if decision.
def folderExists(folderName) {
def file = new File(folderName)
if(file.exists())
{
println "file/folder exists: "+folderName
println "Is directry "+ file.isDirectory().toString()
return file.isDirectory()
}
else{
println "Not found: "+folderName
return false
}
}
println folderExists('C:\\temp')