Delete a file: Difference between revisions

From Rosetta Code
Content added Content deleted
(added c)
Line 3: Line 3:


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.

=={{header|C}}==
<c>#include <stdio.h>

int main() {
remove("input.txt");
remove("/input.txt");
remove("docs");
remove("/docs");
return 0;
}</c>


=={{header|Forth}}==
=={{header|Forth}}==
Line 22: Line 33:
=={{header|Java}}==
=={{header|Java}}==


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


=={{header|MAXScript}}==
=={{header|MAXScript}}==
Line 50: Line 61:
=={{header|Perl}}==
=={{header|Perl}}==


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


'''Without Perl Modules'''
'''Without Perl Modules'''
Line 78: Line 89:
=={{header|Python}}==
=={{header|Python}}==


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


=={{header|Raven}}==
=={{header|Raven}}==

Revision as of 23:38, 26 April 2008

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.

C

<c>#include <stdio.h>

int main() {

 remove("input.txt");
 remove("/input.txt");
 remove("docs");
 remove("/docs");
 return 0;

}</c>

Forth

There is no means to delete directories in ANS Forth.

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

Haskell

import System.IO
import System.Directory

main = do
  removeFile "output.txt"
  removeDirectory "docs"
  removeFile "/output.txt"
  removeDirectory "/docs"

Java

<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);
  }

}</java>

MAXScript

There's no way to delete folders in MAXScript

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

Perl

<perl>use File::Spec::Functions qw(catfile rootdir);

  1. here

unlink 'input.txt'; rmdir 'docs';

  1. root dir

unlink catfile rootdir, 'input.txt'; rmdir catfile rootdir, 'docs';</perl>

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"'

PowerShell

Delete-Item input.txt
# Can also use the del alias for the Delete-Item cmdlet
del input.txt


Python

<python>import os

  1. current directory

os.remove("output.txt") os.rmdir("docs")

  1. root directory

os.remove("/output.txt") os.rmdir("/docs")</python>

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

UNIX Shell

rm -rf docs
rm input.txt
rm -rf /docs
rm /input.txt

Visual Basic .NET

Platform: .NET

Works with: Visual Basic .NET 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")