Jump to content
Sign in to follow this  
dutchM

CreateFolder Module - using variable in pathToCreate?

Recommended Posts

I want the folder to be created to be names as today's date in a Month-Day-Year format. So 8-9-2012. I added calendar code to it:

//Really simple version:

//return new File(path).mkdirs();

//Version with decent error reporting:

File dir = new File(pathToCreate);

Calendar cal = new GregorianCalendar();

// Get the components of the date

int year = cal.get(Calendar.YEAR);

int month = cal.get(Calendar.MONTH);

int day = cal.get(Calendar.DAY_OF_MONTH);

if( dir.exists() ) return false;

if( dir.mkdirs() ) { //Success!

return true;

} else { //Figure out why it failed

File realParent = dir;

while( realParent != null && ! realParent.exists() ) {

realParent = realParent.getParentFile();

}

if( realParent != null && !realParent.canWrite() ) {

throw new FileNotFoundException("Directory " + dir.getAbsolutePath() + " could not be created because the parent directory " + realParent.getAbsolutePath() + " is not writeable" );

} else {

throw new FileNotFoundException("Directory + " + dir.getAbsolutePath() + " could not be created because of an unknown error." );

}

}

but how do I get variables to work in the pathToCreate? I tried /Users/erickornmeyer/Desktop/DBPrice Updates/Pricing/Vendor Cost Updates/$month-$day-$year but it didn't do anything. Any help would be appreciated!

Share this post


Link to post
Share on other sites

You are making the file object before the calendar stuff and then not doing anything with it

This might get you nearer




Calendar cal = new GregorianCalendar()



// Get the components of the date

int year = cal.get(Calendar.YEAR)

int month = cal.get(Calendar.MONTH)

int day = cal.get(Calendar.DAY_OF_MONTH



SEP = System.getProperty('file.separator')

pathToCreate = pathToCreate + SEP + month + '-' + day + '-' + year

// will give e.g. path/child/01-31-2012



File dir = new File(pathToCreate)



if( dir.exists() ) return false

if( dir.mkdirs() ) { //Success!

return true

} else { //Figure out why it failed

File realParent = dir

while( realParent != null && ! realParent.exists() ) {

realParent = realParent.getParentFile()

} //end while

if( realParent != null && !realParent.canWrite() ) {

throw new FileNotFoundException('Directory ' + dir.getAbsolutePath() + ' could not be created because the parent directory ' + realParent.getAbsolutePath() + ' is not writeable' )

} else {

throw new FileNotFoundException('Directory + ' + dir.getAbsolutePath() + ' could not be created because of an unknown error.' )

} //end if

} //end if





although you could do

end = new Date().format ('mm-dd-yyyy')

pathToCreate = pathToCreate + SEP + end

Share this post


Link to post
Share on other sites

end = new Date().format ('mm-dd-yyyy')

pathToCreate = pathToCreate + SEP + end

Worked great except the output was 31-14-2012 for today. Not sure where it is getting that month value

Changed it to MM and it works now, thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using this site, you agree to our Terms of Use.