Delete a file: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎[[Java]]: Use Java header instead)
m (Changed over to headers.)
Line 4: Line 4:
In this task, the job is to delete a file called "input.txt" and delete a directory called "docs". This should be done twice: once "here", i.e. in the current working directory and once in the filesystem root.
In this task, the job is to delete a file called "input.txt" and delete a directory called "docs". This should be done twice: once "here", i.e. in the current working directory and once in the filesystem root.


==[[Forth]]==
=={{header|Forth}}==
[[Category:Forth]]
There is no means to delete directories in ANS Forth.
There is no means to delete directories in ANS Forth.
s" input.txt" delete-file throw
s" input.txt" delete-file throw
Line 31: Line 30:
}
}


==[[MAXScript]]==
=={{header|MAXScript}}==
[[Category:MAXScript]]
There's no way to delete folders in MAXScript
There's no way to delete folders in MAXScript
-- Here
-- Here
Line 39: Line 37:
deleteFile "\input.txt"
deleteFile "\input.txt"


==[[Perl]]==
=={{header|Perl}}==
[[Category:Perl]]


use File::Spec::Functions qw(catfile rootdir);
use File::Spec::Functions qw(catfile rootdir);
Line 60: Line 57:
perl -e 'rmdir "/docs"'
perl -e 'rmdir "/docs"'


==[[Python]]==
=={{header|Python}}==


import os
import os
Line 70: Line 67:
os.rmdir("/docs")
os.rmdir("/docs")


==[[Raven]]==
=={{header|Raven}}==
[[Category:Raven]]


'input.txt' delete
'input.txt' delete
Line 78: Line 74:
'/docs' rmdir
'/docs' rmdir


==[[Ruby]]==
=={{header|Ruby}}==
[[Category:Ruby]]


#!/usr/bin/env ruby
#!/usr/bin/env ruby
Line 86: Line 81:
Dir.delete("/docs")
Dir.delete("/docs")


==[[Toka]]==
=={{header|Toka}}==
[[Category:Toka]]


needs shell
needs shell
Line 93: Line 87:
" input.txt" remove
" input.txt" remove


==[[Visual Basic .NET]]==
=={{header|Visual Basic .NET}}==
[[Category:Visual Basic .NET]]


'''Platform:''' [[.NET]]
'''Platform:''' [[.NET]]

Revision as of 20:15, 12 November 2007

Task
Delete a file
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the job is to delete a file called "input.txt" and delete a directory called "docs". This should be done twice: once "here", i.e. in the current working directory and once in the filesystem root.

Forth

There is no means to delete directories in ANS Forth.

 s" input.txt" delete-file throw
s" /input.txt" delete-file throw

Java

import java.util.File;
public class FileDeleteTest {
   public static boolean deleteFile(String filename) {
       boolean exists = new File(filename).delete();
       return exists;
   }
   public static void test(String type, String filename) {
       System.out.println("The following " + type + " called " + filename + 
           (deleteFile(filename) ? " was deleted." : " could not be deleted.")
       );
   }
   public static void main(String args[]) {
        test("file", "input.txt");
        test("file", File.seperator + "input.txt");
        test("directory", "docs");
        test("directory", File.seperator + "docs" + File.seperator);
   }
}

MAXScript

There's no way to delete folders in MAXScript

-- Here
deleteFile "input.txt"
-- Root
deleteFile "\input.txt"

Perl

use File::Spec::Functions qw(catfile rootdir);
# here
unlink 'input.txt';
rmdir 'docs';
# root dir
unlink catfile rootdir, 'input.txt';
rmdir catfile rootdir, 'docs';

Without Perl Modules

Current directory

perl -e 'unlink input.txt'
perl -e 'rmdir docs'

Root Directory

perl -e 'unlink "/input.txt"'
perl -e 'rmdir "/docs"'

Python

import os
# current directory
os.remove("output.txt")
os.rmdir("docs")
# root directory
os.remove("/output.txt")
os.rmdir("/docs")

Raven

'input.txt'  delete
'/input.txt' delete
'docs'  rmdir
'/docs' rmdir

Ruby

 #!/usr/bin/env ruby
 File.delete("output.txt", "/output.txt")
 Dir.delete("docs")
 Dir.delete("/docs")

Toka

 needs shell
 " docs" remove
 " input.txt" remove

Visual Basic .NET

Platform: .NET

Language Version: 9.0+

'Current Directory
IO.Directory.Delete("docs")
IO.Directory.Delete("docs", True) 'also delete files and sub-directories
IO.File.Delete("output.txt")

'Root
IO.Directory.Delete("\docs")
IO.File.Delete("\output.txt")

'Root, platform independent
IO.Directory.Delete(IO.Path.DirectorySeparatorChar & "docs")
IO.File.Delete(IO.Path.DirectorySeparatorChar & "output.txt")