#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2016
    Posts
    1
    Rep Power
    0

    how do i convert ".txt" file to ".dat"


    Here i have convert all ".txt" file to ".dat" from respectived directory. But i am getting output ".txt.dat" instead if ".dat"
    Here is program
    Code:
    #!/usr/bin/perl
    
    $runDir = $ENV{"REGR_RUN_DIR"};
    @files = (<*.txt>);
    
    foreach $FILE (@files)
    {
    open (IN, $FILE) || die "Couldn't open $FILE for reading";
    open (OUT, ">$runDir/$FILE.dat") || die "Couldn't open $FILE for reading";
    
    while(<IN>) {
        print OUT $_;
    }
    }
    close(IN);
    close(OUT);
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,229
    Rep Power
    1231
    You need to make a copy of the $FILE var then strip off the .txt from its value before making the new file. There are several ways to strip off the .txt. You could use a substitution regex or the File::Basename module.

    Why are you opening 2 filehandles and doing a read/write operation? That is the most inefficient approach.

    Do you need to rename the files or create copies using new filenames? Instead of doing what you currently have, use either the copy() function or move() function from the File::Copy module.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2016
    Location
    Jakarta
    Posts
    1
    Rep Power
    0
    Try this:

    Code:
    Const NumCount = 3375
    Private Sub Command1_Click()
    Dim MyNum As String, MyDatNum As Integer
    ' Build the text file with numbers
    Open "MyNumbers.Txt" For Output As #1
    For I = 1 To NumCount
        Print #1, Str$(I)
    Next
    Close
    ' Read the text file and write it to a random access file
    Open "MyNumbers.Txt" For Input As #1
    Open "MyNumbers.Dat" For Random As #2 Len = 2
    For I = 1 To NumCount
        Line Input #1, MyNum
        Put #2, , CInt(MyNum)
    Next
    Close
    'Prove that all the numerical data exists
    Open "MyNumbers.Dat" For Random As #1 Len = 2
    For I = 1 To NumCount
        Get #1, , MyDatNum
        List1.AddItem MyDatNum
    Next
    Close
    ' Display the bytes saved
    Label1.Caption = "Bytes saved = " & FileLen("MyNumbers.Txt") - FileLen("MyNumbers.Dat")
    End Sub

IMN logo majestic logo threadwatch logo seochat tools logo