HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Program that organizes files based on folder name and file name

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fileorganizesprogramfilesthatfolderbasednameand

Problem

I am rather new to coding, and I would like to see what improvements should I make to the code I wrote to ensure that I am using good practices, and that it will function as it's supposed to. The following code detects the operating system (I have it set for either Linux or Windows), detects the folders around it, detects the files around it, then moves the files into folders based on the similarity between the file and folder names.

```
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Organizer {
public static void main(String[] args) throws Exception{ //This part organizes the files

String p1 = null;
String p2 = null;
if ((confstring()).equalsIgnoreCase("linux")){
p1 = "bash";
p2 = "-c";

} else if ((confstring()).equalsIgnoreCase("windo")){
p1 = "CMD";
p2 = "/C";
}
String newname = null ;
String[] confcmd = configure() ;//Retrieves the commands from the configure() function
String[] folder = execute(confcmd[0], p1,p2);//Executes the first command and gets a list of folders
String[] interact = new String[folder.length] ;

for (int n=0 ; n < folder.length; n++){
interact[n] = folder[n].substring(0,3);//Makes the keyword to compare the filenames to
//System.out.println(interact[n]);
}
String[] stdoutput = execute(confcmd[1],p1,p2);

if ((confstring()).equalsIgnoreCase("linux")){//both of the following if statements get the filenames, compares them to the keyword, then moves and renames the files
for ( int n=0 ; n < interact.length; n++){
for(int i=0 ; i<stdoutput.length; i++){
if((stdoutput[i]).contains(interact[n])){
//System.out.println(stdoutput[i]);
newname = confcmd[2] + " " + stdoutput[i] + " " + folder[n] +confcmd[3] + stdoutput[i] + ")" + "-" +stdoutput[i]; //The ")" results from a shell scripting quirk I had to address
//System.out.println(n

Solution

Some suggestions:

  • Don't wrap shell commands in Java unless you absolutely have to. Java supports listing files, moving them, getting dates etc. natively. As an added bonus you'll instantly support basically any platform without writing any platform specific code, and you won't have to do a bunch of string parsing to get the result in a usable format.



  • Format your code with a linter or IDE. It will make it much easier to read for others.



  • Short variable names (p1, confcmd, etc.) make your code harder to read. Programming is a brain problem, not a typing problem, so you should optimise for understanding rather than brevity.



  • Anything in your main function is basically not reusable in other Java code. If you can think of any use case of importing your code into another file, you should create a separate method for that functionality avoiding dependencies on the surrounding shell (such as arguments).



  • Inline comments are a code smell. Everywhere you feel the need for an inline comment you should ask yourself if you could refactor the code in such a way that the comment is unnecessary.

Context

StackExchange Code Review Q#132871, answer score: 4

Revisions (0)

No revisions yet.