This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Creating a folder in C drive root

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to create a folder in the C:\ root location. I have a program which does this.



On my computer, this creates a folder called Test under C:\MyPrograms\Java\Test

How would I change the code above to create a folder under C:\Test, without specifying the C:\ prefix? If I ran my program under F:\MyPrograms\Java\Test, how could I achieve get the folder under F:\Test?
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok below shown
is the program for creating the folder in any Drive or path given by you.



public class Fold {


public static void main(String[] args) {
FileOutputStream fout;
FileInputStream fin;



String newFolderPath = "C:\\";
String newFolderName = "Sonu";
String spoonFeeding = newFolderPath + newFolderName;
File f = new File(spoonFeeding);
f.mkdir();

}
}


I hope this will help you.
 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dhwani mathur:
ok below shown
is the program for creating the folder in any Drive or path given by you.



public class Fold {


public static void main(String[] args) {
FileOutputStream fout;
FileInputStream fin;



String newFolderPath = "C:\\";
String newFolderName = "Sonu";
String spoonFeeding = newFolderPath + newFolderName;
File f = new File(spoonFeeding);
f.mkdir();

}
}


I hope this will help you.



Not quite what I need. is currently hardcoded to C:\, which means the drive letter is specified.

What I want is a way to create a folder at the root level, e.g C:\Test or D:\Test, without specifying the drive letter.
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Pass the drive where you want to create as a command line argument and then pass it to the File constructor.



Regards
Nik
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aaron,

call the static method as:
String path = System.getProperty("user.dir");

in path is the directory from where the application has started (where the class is).
Example:


The list in the output should contain the new folder. Perhaps "root" is not appropriate, because I don't think that it is really a root.

Yours,
Bu.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can try this to get the current drive:

File tempFile = new File(".");
String driveName = tempFile .getAbsolutePath().substring(0,2);
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
File f = new File("/Test");
f.mkdir();

should work, shouldn't it?
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok ilja !!
i tried this it works the folder gets created in E drive.
 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
File f = new File("/Test");
f.mkdir();

should work, shouldn't it?



Thanks Ilja, your suggestion was what I was after
 
Quick! Before anybody notices! Cover it up with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic