Need help renaming a file to the same name as folder.
I am looking to loop through a directory full of folders each containing one file. I am wanting to change each file name in each folder to the same name as the folder.The code compiles fine, it just never actually does anything. Here is my code.
import javax.swing.JOptionPane;
import java.io.File;
public class filing
{
public static void main (String [ ] args)
{
File dir = new File("C:\\testing");
File file;
File cf;
String dirs[ ] = dir.list();
String folderName = "";
if (dirs == null)
{
JOptionPane.showMessageDialog(null, "Directory does not exist", "I can't find it", JOptionPane.WARNING_MESSAGE);
}
else
{
for (int i=0; i<dirs.length; i++)
{
folderName = dirs[ i ];
file = new File("C:\\testing\\"+folderName);
String changeFile[ ] = file.list();
cf=new File(changeFile[0]);
cf.renameTo(new File("C:\\testing\\"+folderName+".txt"));
}
}
System.exit(0);
}
}>

