WeixiangYan/CodeTransOcean huggingface dataset:
Task: Check that file exists




Go: package main

import (
    "fmt"
    "os"
)

func printStat(p string) {
    switch i, err := os.Stat(p); {
    case err != nil:
        fmt.Println(err)
    case i.IsDir():
        fmt.Println(p, "is a directory")
    default:
        fmt.Println(p, "is a file")
    }
}

func main() {
    printStat("input.txt")
    printStat("/input.txt")
    printStat("docs")
    printStat("/docs")
}
Task: Check that file exists



C#: using System.IO;

Console.WriteLine(File.Exists("input.txt"));
Console.WriteLine(File.Exists("/input.txt"));
Console.WriteLine(Directory.Exists("docs"));
Console.WriteLine(Directory.Exists("/docs"));








Fortran: LOGICAL :: file_exists
INQUIRE(FILE="input.txt", EXIST=file_exists)   
                                               
INQUIRE(FILE="/input.txt", EXIST=file_exists)
Task: Check that file exists


C++: #include "boost/filesystem.hpp"
#include <string>
#include <iostream>

void testfile(std::string name)
{
  boost::filesystem::path file(name);
  if (exists(file))
  {
    if (is_directory(file))
      std::cout << name << " is a directory.\n";
    else
      std::cout << name << " is a non-directory file.\n";
  }
  else
    std::cout << name << " does not exist.\n";
}

int main()
{
  testfile("input.txt");
  testfile("docs");
  testfile("/input.txt");
  testfile("/docs");
}









Fortran: LOGICAL :: file_exists
INQUIRE(FILE="input.txt", EXIST=file_exists)   
                                               
INQUIRE(FILE="/input.txt", EXIST=file_exists)
Task: Check that file exists

C: #include <sys/types.h> 
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>


int check_reg(const char *path) {
	struct stat sb;
	return stat(path, &sb) == 0 && S_ISREG(sb.st_mode);
}


int check_dir(const char *path) {
	struct stat sb;
	return stat(path, &sb) == 0 && S_ISDIR(sb.st_mode);
}

int main() {
	printf("input.txt is a regular file? %s\n",
	    check_reg("input.txt") ? "yes" : "no");
	printf("docs is a directory? %s\n",
	    check_dir("docs") ? "yes" : "no");
	printf("/input.txt is a regular file? %s\n",
	    check_reg("/input.txt") ? "yes" : "no");
	printf("/docs is a directory? %s\n",
	    check_dir("/docs") ? "yes" : "no");
	return 0;
}










Fortran: LOGICAL :: file_exists
INQUIRE(FILE="input.txt", EXIST=file_exists)   
                                               
INQUIRE(FILE="/input.txt", EXIST=file_exists)
Task: Check that file exists





Java: import java.io.File;
public class FileExistsTest {
   public static boolean isFileExists(String filename) {
       boolean exists = new File(filename).exists();
       return exists;
   }
   public static void test(String type, String filename) {
       System.out.println("The following " + type + " called " + filename + 
           (isFileExists(filename) ? " exists." : " not exists.")
       );
   }
   public static void main(String args[]) {
        test("file", "input.txt");
        test("file", File.separator + "input.txt");
        test("directory", "docs");
        test("directory", File.separator + "docs" + File.separator);
   }
}






Fortran: LOGICAL :: file_exists
INQUIRE(FILE="input.txt", EXIST=file_exists)   
                                               
INQUIRE(FILE="/input.txt", EXIST=file_exists)
Task: Check that file exists







Python: import os

os.path.isfile("input.txt")
os.path.isfile("/input.txt")
os.path.isdir("docs")
os.path.isdir("/docs")




Fortran: LOGICAL :: file_exists
INQUIRE(FILE="input.txt", EXIST=file_exists)   
                                               
INQUIRE(FILE="/input.txt", EXIST=file_exists)
Task: Check that file exists








VB: Option Explicit

Sub Main_File_Exists()
Dim myFile As String, myDirectory As String
    
    myFile = "Abdu
    myDirectory = "C:\"
    Debug.Print File_Exists(myFile, myDirectory)
End Sub

Function File_Exists(F As String, D As String) As Boolean
    If F = "" Then
        File_Exists = False
    Else
        D = IIf(Right(D, 1) = "\", D, D & "\")
        File_Exists = (Dir(D & F) <> "")
    End If
End Function



Fortran: LOGICAL :: file_exists
INQUIRE(FILE="input.txt", EXIST=file_exists)   
                                               
INQUIRE(FILE="/input.txt", EXIST=file_exists)
Task: Check that file exists






PHP: if (file_exists('input.txt')) echo 'input.txt is here right by my side';
if (file_exists('docs'     )) echo 'docs is here with me';
if (file_exists('/input.txt')) echo 'input.txt is over there in the root dir';
if (file_exists('/docs'    )) echo 'docs is over there in the root dir';





Fortran: LOGICAL :: file_exists
INQUIRE(FILE="input.txt", EXIST=file_exists)   
                                               
INQUIRE(FILE="/input.txt", EXIST=file_exists)
Task: Check that file exists

C: #include <sys/types.h> 
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>


int check_reg(const char *path) {
	struct stat sb;
	return stat(path, &sb) == 0 && S_ISREG(sb.st_mode);
}


int check_dir(const char *path) {
	struct stat sb;
	return stat(path, &sb) == 0 && S_ISDIR(sb.st_mode);
}

int main() {
	printf("input.txt is a regular file? %s\n",
	    check_reg("input.txt") ? "yes" : "no");
	printf("docs is a directory? %s\n",
	    check_dir("docs") ? "yes" : "no");
	printf("/input.txt is a regular file? %s\n",
	    check_reg("/input.txt") ? "yes" : "no");
	printf("/docs is a directory? %s\n",
	    check_dir("/docs") ? "yes" : "no");
	return 0;
}









COBOL:        identification division.
       program-id. check-file-exist.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 skip                 pic 9 value 2.
       01 file-name.
          05 value "/output.txt".
       01 dir-name.
          05 value "/docs/".
       01 unusual-name.
          05 value "Abdu'l-Bahá.txt".

       01 test-name            pic x(256).

       01 file-handle          usage binary-long.
       01 file-info.
          05 file-size         pic x(8) comp-x.
          05 file-date.
             10 file-day       pic x comp-x.
             10 file-month     pic x comp-x.
             10 file-year      pic xx comp-x.
          05 file-time.
             10 file-hours     pic x comp-x.
             10 file-minutes   pic x comp-x.
             10 file-seconds   pic x comp-x.
             10 file-hundredths  pic x comp-x. 

       procedure division.
       files-main.

      
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       move unusual-name to test-name
       perform check-file

      
       move 1 to skip
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       goback.

       check-file.
       call "CBL_CHECK_FILE_EXIST" using test-name file-info
       if return-code equal zero then
           display test-name(1:32) ": size " file-size ", "
                   file-year "-" file-month "-" file-day space
                   file-hours ":" file-minutes ":" file-seconds "."
                   file-hundredths
       else
           display "error: CBL_CHECK_FILE_EXIST " return-code space
                   trim(test-name)
       end-if
       .

       end program check-file-exist.
Task: Check that file exists



C#: using System.IO;

Console.WriteLine(File.Exists("input.txt"));
Console.WriteLine(File.Exists("/input.txt"));
Console.WriteLine(Directory.Exists("docs"));
Console.WriteLine(Directory.Exists("/docs"));







COBOL:        identification division.
       program-id. check-file-exist.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 skip                 pic 9 value 2.
       01 file-name.
          05 value "/output.txt".
       01 dir-name.
          05 value "/docs/".
       01 unusual-name.
          05 value "Abdu'l-Bahá.txt".

       01 test-name            pic x(256).

       01 file-handle          usage binary-long.
       01 file-info.
          05 file-size         pic x(8) comp-x.
          05 file-date.
             10 file-day       pic x comp-x.
             10 file-month     pic x comp-x.
             10 file-year      pic xx comp-x.
          05 file-time.
             10 file-hours     pic x comp-x.
             10 file-minutes   pic x comp-x.
             10 file-seconds   pic x comp-x.
             10 file-hundredths  pic x comp-x. 

       procedure division.
       files-main.

      
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       move unusual-name to test-name
       perform check-file

      
       move 1 to skip
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       goback.

       check-file.
       call "CBL_CHECK_FILE_EXIST" using test-name file-info
       if return-code equal zero then
           display test-name(1:32) ": size " file-size ", "
                   file-year "-" file-month "-" file-day space
                   file-hours ":" file-minutes ":" file-seconds "."
                   file-hundredths
       else
           display "error: CBL_CHECK_FILE_EXIST " return-code space
                   trim(test-name)
       end-if
       .

       end program check-file-exist.
Task: Check that file exists


C++: #include "boost/filesystem.hpp"
#include <string>
#include <iostream>

void testfile(std::string name)
{
  boost::filesystem::path file(name);
  if (exists(file))
  {
    if (is_directory(file))
      std::cout << name << " is a directory.\n";
    else
      std::cout << name << " is a non-directory file.\n";
  }
  else
    std::cout << name << " does not exist.\n";
}

int main()
{
  testfile("input.txt");
  testfile("docs");
  testfile("/input.txt");
  testfile("/docs");
}








COBOL:        identification division.
       program-id. check-file-exist.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 skip                 pic 9 value 2.
       01 file-name.
          05 value "/output.txt".
       01 dir-name.
          05 value "/docs/".
       01 unusual-name.
          05 value "Abdu'l-Bahá.txt".

       01 test-name            pic x(256).

       01 file-handle          usage binary-long.
       01 file-info.
          05 file-size         pic x(8) comp-x.
          05 file-date.
             10 file-day       pic x comp-x.
             10 file-month     pic x comp-x.
             10 file-year      pic xx comp-x.
          05 file-time.
             10 file-hours     pic x comp-x.
             10 file-minutes   pic x comp-x.
             10 file-seconds   pic x comp-x.
             10 file-hundredths  pic x comp-x. 

       procedure division.
       files-main.

      
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       move unusual-name to test-name
       perform check-file

      
       move 1 to skip
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       goback.

       check-file.
       call "CBL_CHECK_FILE_EXIST" using test-name file-info
       if return-code equal zero then
           display test-name(1:32) ": size " file-size ", "
                   file-year "-" file-month "-" file-day space
                   file-hours ":" file-minutes ":" file-seconds "."
                   file-hundredths
       else
           display "error: CBL_CHECK_FILE_EXIST " return-code space
                   trim(test-name)
       end-if
       .

       end program check-file-exist.
Task: Check that file exists





Java: import java.io.File;
public class FileExistsTest {
   public static boolean isFileExists(String filename) {
       boolean exists = new File(filename).exists();
       return exists;
   }
   public static void test(String type, String filename) {
       System.out.println("The following " + type + " called " + filename + 
           (isFileExists(filename) ? " exists." : " not exists.")
       );
   }
   public static void main(String args[]) {
        test("file", "input.txt");
        test("file", File.separator + "input.txt");
        test("directory", "docs");
        test("directory", File.separator + "docs" + File.separator);
   }
}





COBOL:        identification division.
       program-id. check-file-exist.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 skip                 pic 9 value 2.
       01 file-name.
          05 value "/output.txt".
       01 dir-name.
          05 value "/docs/".
       01 unusual-name.
          05 value "Abdu'l-Bahá.txt".

       01 test-name            pic x(256).

       01 file-handle          usage binary-long.
       01 file-info.
          05 file-size         pic x(8) comp-x.
          05 file-date.
             10 file-day       pic x comp-x.
             10 file-month     pic x comp-x.
             10 file-year      pic xx comp-x.
          05 file-time.
             10 file-hours     pic x comp-x.
             10 file-minutes   pic x comp-x.
             10 file-seconds   pic x comp-x.
             10 file-hundredths  pic x comp-x. 

       procedure division.
       files-main.

      
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       move unusual-name to test-name
       perform check-file

      
       move 1 to skip
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       goback.

       check-file.
       call "CBL_CHECK_FILE_EXIST" using test-name file-info
       if return-code equal zero then
           display test-name(1:32) ": size " file-size ", "
                   file-year "-" file-month "-" file-day space
                   file-hours ":" file-minutes ":" file-seconds "."
                   file-hundredths
       else
           display "error: CBL_CHECK_FILE_EXIST " return-code space
                   trim(test-name)
       end-if
       .

       end program check-file-exist.
Task: Check that file exists







Python: import os

os.path.isfile("input.txt")
os.path.isfile("/input.txt")
os.path.isdir("docs")
os.path.isdir("/docs")



COBOL:        identification division.
       program-id. check-file-exist.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 skip                 pic 9 value 2.
       01 file-name.
          05 value "/output.txt".
       01 dir-name.
          05 value "/docs/".
       01 unusual-name.
          05 value "Abdu'l-Bahá.txt".

       01 test-name            pic x(256).

       01 file-handle          usage binary-long.
       01 file-info.
          05 file-size         pic x(8) comp-x.
          05 file-date.
             10 file-day       pic x comp-x.
             10 file-month     pic x comp-x.
             10 file-year      pic xx comp-x.
          05 file-time.
             10 file-hours     pic x comp-x.
             10 file-minutes   pic x comp-x.
             10 file-seconds   pic x comp-x.
             10 file-hundredths  pic x comp-x. 

       procedure division.
       files-main.

      
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       move unusual-name to test-name
       perform check-file

      
       move 1 to skip
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       goback.

       check-file.
       call "CBL_CHECK_FILE_EXIST" using test-name file-info
       if return-code equal zero then
           display test-name(1:32) ": size " file-size ", "
                   file-year "-" file-month "-" file-day space
                   file-hours ":" file-minutes ":" file-seconds "."
                   file-hundredths
       else
           display "error: CBL_CHECK_FILE_EXIST " return-code space
                   trim(test-name)
       end-if
       .

       end program check-file-exist.
Task: Check that file exists








VB: Option Explicit

Sub Main_File_Exists()
Dim myFile As String, myDirectory As String
    
    myFile = "Abdu
    myDirectory = "C:\"
    Debug.Print File_Exists(myFile, myDirectory)
End Sub

Function File_Exists(F As String, D As String) As Boolean
    If F = "" Then
        File_Exists = False
    Else
        D = IIf(Right(D, 1) = "\", D, D & "\")
        File_Exists = (Dir(D & F) <> "")
    End If
End Function


COBOL:        identification division.
       program-id. check-file-exist.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 skip                 pic 9 value 2.
       01 file-name.
          05 value "/output.txt".
       01 dir-name.
          05 value "/docs/".
       01 unusual-name.
          05 value "Abdu'l-Bahá.txt".

       01 test-name            pic x(256).

       01 file-handle          usage binary-long.
       01 file-info.
          05 file-size         pic x(8) comp-x.
          05 file-date.
             10 file-day       pic x comp-x.
             10 file-month     pic x comp-x.
             10 file-year      pic xx comp-x.
          05 file-time.
             10 file-hours     pic x comp-x.
             10 file-minutes   pic x comp-x.
             10 file-seconds   pic x comp-x.
             10 file-hundredths  pic x comp-x. 

       procedure division.
       files-main.

      
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       move unusual-name to test-name
       perform check-file

      
       move 1 to skip
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       goback.

       check-file.
       call "CBL_CHECK_FILE_EXIST" using test-name file-info
       if return-code equal zero then
           display test-name(1:32) ": size " file-size ", "
                   file-year "-" file-month "-" file-day space
                   file-hours ":" file-minutes ":" file-seconds "."
                   file-hundredths
       else
           display "error: CBL_CHECK_FILE_EXIST " return-code space
                   trim(test-name)
       end-if
       .

       end program check-file-exist.
Task: Check that file exists




Go: package main

import (
    "fmt"
    "os"
)

func printStat(p string) {
    switch i, err := os.Stat(p); {
    case err != nil:
        fmt.Println(err)
    case i.IsDir():
        fmt.Println(p, "is a directory")
    default:
        fmt.Println(p, "is a file")
    }
}

func main() {
    printStat("input.txt")
    printStat("/input.txt")
    printStat("docs")
    printStat("/docs")
}






COBOL:        identification division.
       program-id. check-file-exist.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 skip                 pic 9 value 2.
       01 file-name.
          05 value "/output.txt".
       01 dir-name.
          05 value "/docs/".
       01 unusual-name.
          05 value "Abdu'l-Bahá.txt".

       01 test-name            pic x(256).

       01 file-handle          usage binary-long.
       01 file-info.
          05 file-size         pic x(8) comp-x.
          05 file-date.
             10 file-day       pic x comp-x.
             10 file-month     pic x comp-x.
             10 file-year      pic xx comp-x.
          05 file-time.
             10 file-hours     pic x comp-x.
             10 file-minutes   pic x comp-x.
             10 file-seconds   pic x comp-x.
             10 file-hundredths  pic x comp-x. 

       procedure division.
       files-main.

      
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       move unusual-name to test-name
       perform check-file

      
       move 1 to skip
       move file-name(skip:) to test-name
       perform check-file

       move dir-name(skip:) to test-name
       perform check-file

       goback.

       check-file.
       call "CBL_CHECK_FILE_EXIST" using test-name file-info
       if return-code equal zero then
           display test-name(1:32) ": size " file-size ", "
                   file-year "-" file-month "-" file-day space
                   file-hours ":" file-minutes ":" file-seconds "."
                   file-hundredths
       else
           display "error: CBL_CHECK_FILE_EXIST " return-code space
                   trim(test-name)
       end-if
       .

       end program check-file-exist.
Task: Check that file exists






PHP: if (file_exists('input.txt')) echo 'input.txt is here right by my side';
if (file_exists('docs'     )) echo 'docs is here with me';
if (file_exists('/input.txt')) echo 'input.txt is over there in the root dir';
if (file_exists('/docs'    )) echo 'docs is over there in the root dir';
Task: Check that file exists






PHP: if (file_exists('input.txt')) echo 'input.txt is here right by my side';
if (file_exists('docs'     )) echo 'docs is here with me';
if (file_exists('/input.txt')) echo 'input.txt is over there in the root dir';
if (file_exists('/docs'    )) echo 'docs is over there in the root dir';






Ada: with Ada.Text_IO; use Ada.Text_IO;

procedure File_Exists is
   function Does_File_Exist (Name : String) return Boolean is
      The_File : Ada.Text_IO.File_Type;
   begin
      Open (The_File, In_File, Name);
      Close (The_File);
      return True;
   exception
      when Name_Error =>
         return False;
   end Does_File_Exist;
begin
   Put_Line (Boolean'Image (Does_File_Exist ("input.txt" )));
   Put_Line (Boolean'Image (Does_File_Exist ("\input.txt")));
end File_Exists;