sreevno has asked for the wisdom of the Perl Monks concerning the following question:

I am trying a script where I am in directory C:/perl64/bin/extract. My perl script is in this directory and .caz(form of zip) files are also there in this directory. My script creates another directory in the above directory using the names of .caz files. and copied them into respective folders. I am trying to extract this .caz files in the newly created directory i.e C:/perl64/bin/extract/$newdir(name as .caz file). When I am excuting the scirpt the files are extracting at C:/perl64/bin/extract , but I want to change to new directory and extract eh fiel inside that. Please help me. code I am using is below. I tried servral options to "cd" in windows but not able to execute the file inside the new directory.

open(DAT, $data_file); my @raw_data = <DAT>; foreach my $fileLine1 (@raw_data) { chomp($fileLine1); my @dir = split('\.caz',$fileLine1); foreach my $newdir (@dir){ chomp($newdir); mkdir $newdir; my $newdir1 = "$dir\\$newdir"; opendir(DIR, $dir) or die "can't opendir $dir: $! \n"; @files=readdir(DIR); closedir DIR; my $oldfile="$dir\\$newdir.caz"; copy($oldfile, $newdir1) or die "File cannot be copied"; #my $final_dest= "C:\\Perl64\\bin\\extract\\$newdir"; #my $cmd1 = system("cmd /K cd /d $newdir1"); #my $cmd1= "cd C:\\Perl64\\bin\\extract\\$newdir"; my $cmd1= 'cd C:\Perl64\bin\extract\$newdir'; open (CMD, "|$cmd1") or die "fail1"; #chdir("C:\\Perl64\\bin\\extract\\$newdir\\"); #print $cmd1; my $cmd = "cazipxp -u $newdir\.caz"; my %newfiles = open (CMD, "|$cmd") or die "fail"; }}
  • Comment on change directory in windows and extract the files from a .zip file inside the folder
  • Download Code

Replies are listed 'Best First'.
Re: change directory in windows and extract the files from a .zip file inside the folder
by 2teez (Vicar) on Sep 12, 2012 at 08:46 UTC
    Hi,

    ..My perl script is in this directory and .caz(form of zip) files are also there in this directory..
    One way of achieving your aim is as follow:

    • Open the directory that has both perl script and the files you want to extract,
    • get the names of the files ( I used zip in the code below ) using match and Regex, then make a folder with the name,
    • copy the files to the newly created folder and use the module Archive::Extract to the content in the folder

    Please, modify the script below to suit your purpose:
    #!/usr/bin/perl use warnings; use strict; use File::Copy qw(copy); use Archive::Extract; use Cwd qw(abs_path); my $current_directory = $ARGV[0] // '.'; $current_directory = abs_path $current_directory; chdir $current_directory or die "directory doesn't exist: $!"; opendir my $dh, $current_directory or die "can't open directory: $!"; while ( my $file = readdir $dh ) { chomp $file; next if $file eq '.' or $file eq '..'; if ( $file =~ m{(?<filename>(?<new_folder>.+?)\.zip)$} ) { mkdir $+{new_folder}; ## make the folder copy $+{filename}, $+{new_folder}; ## copy file to be extracted into it' +s folder # files extracted in it's folder my $extract_file = Archive::Extract->new( archive => $+{filena +me} ); $extract_file->extract( to => $+{new_folder} ) or die $extract_file->error; } } closedir $dh or die "can't close directory: $!";

    You might have to check Archive::Extract
    Hope this helps.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      Thanks, But my file is not zip or tar file. It is .caz file whihc uses command called cazipxp -u <filename> for extracting. when I am running the above command thru script it is extracting in the folder where I am executing the script but not to the new directory whihc I have created thru script. My .caz file is there in both parent and new directory. But I want to extract it in new directory. My .pl file is in parent directory. pls help

        sreevno:

        But my file is not zip or tar file. It is .caz file

        2teez knows that. That's why the reply said:

        Please, modify the script below to suit your purpose

        Some effort is required on your part. Since 2teez provided you a skeleton program that changes directories and extracts files from an archive, it's assumed that you can find the code to extract from a .caz file and alter the code appropriately.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: change directory in windows and extract the files from a .zip file inside the folder
by cheekuperl (Monk) on Sep 12, 2012 at 06:20 UTC
    What error do you get? What happens when you run the program?
Re: change directory in windows and extract the files from a .zip file inside the folder
by Anonymous Monk on Sep 12, 2012 at 06:29 UTC

    I am in directory C:/perl64/bin/extract

    Get out of there, find a better directory :)

Re: change directory in windows and extract the files from a .zip file inside the folder
by sreevno (Initiate) on Sep 12, 2012 at 05:57 UTC

    I am mew to perl and this is my first post here. please help

      children process cannot affect %ENV of parent process -- if you want to change directory use chdir