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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

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