Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch script to check files  (Read 5000 times)

0 Members and 1 Guest are viewing this topic.

satya85

    Topic Starter


    Greenhorn

    Batch script to check files
    « on: April 19, 2009, 09:38:42 AM »
    Experts

    I am new to batch programming and need some help here. I am looking for a batch script for following
    task.

    a) Say in folder A, i have 10 files which are updated daily. i need to check these files and if the files are
    updated with latest timestamp then copy to destination folder. The no of files in folder A may vary.
    b) if among 10 files, any file is not updated then display error, saying this file is not updated.
    b) On destination, before copying i need to archive existing files.



    Thanks in advance.

    Dias de verano

    • Guest
    Re: Batch script to check files
    « Reply #1 on: April 20, 2009, 11:46:16 AM »
    what is "latest timestamp"? Is it today's date? What is your local date format?

    How will you "archive"?



    satya85

      Topic Starter


      Greenhorn

      Re: Batch script to check files
      « Reply #2 on: April 20, 2009, 06:58:40 PM »
      Dias de verano,

      Yes it's today's date and date format is mm/dd/yyyy format.

      gh0std0g74



        Apprentice

        Thanked: 37
        Re: Batch script to check files
        « Reply #3 on: April 20, 2009, 08:20:37 PM »
        IF you know Perl and can use it, here's an alternative solution.
        Code: [Select]
        use strict;
        use warnings;
        use File::Find;
        use File::Copy;
        use Archive::Tar;
        use Cwd;

        my $dir="c:/test";
        my $destination="c:/destination";
        my $archive_destination="c:/archive_destination";
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
        $year+=1900; $mon+=1;
        my $archive="$year-$mon-$mday-$hour-$min-$sec.tar";  #set archive filename


        sub archive {
            my $cwd = getcwd();
            chdir($destination);
            my @filelist;
            while(<*>){  -f && push(@filelist,$_); } #get files to archive
            my $tar = Archive::Tar->new;
            $tar->add_files(@filelist);
            $tar->write($archive);
            move($archive,$archive_destination); #move archived file to destination
            foreach(@filelist){
                     unlink $_ ;  # remove files after archive.
            }
            chdir($cwd);
        }

        sub wanted {
            if ( lstat($_) && -f _ && (int(-M _) == 0) ) {
             my $filename = $File::Find::name;
             print "moving $filename to $destination\n";
             move("$filename","$destination") or warn "Cannot move $filename to $destination; $!\n";
            } elsif ( -f _ ) {
             print "File not updated: $filename\n";
            }
        }

        archive;
        find({wanted => \&wanted}, "$dir");

        Dias de verano

        • Guest
        Re: Batch script to check files
        « Reply #4 on: April 21, 2009, 12:17:01 AM »
        If I may say so, ghostdog, an excellent solution!

        satya85

          Topic Starter


          Greenhorn

          Re: Batch script to check files
          « Reply #5 on: April 21, 2009, 07:52:59 AM »
          Thanks Ghostdog, but i need script which needs to be scheduled and hence looking for some batch script

          gh0std0g74



            Apprentice

            Thanked: 37
            Re: Batch script to check files
            « Reply #6 on: April 21, 2009, 08:03:37 AM »
            Thanks Ghostdog, but i need script which needs to be scheduled and hence looking for some batch script

            open your editor, type in

            perl myscript.pl

            save as mybatch.bat. Use the task scheduler to run it periodically.

            Otherwise, wait for other solutions to come by.

            satya85

              Topic Starter


              Greenhorn

              Re: Batch script to check files
              « Reply #7 on: April 21, 2009, 08:35:47 AM »
              i tried to save the perl script to mybatch.bat and ran that script, didnt work.

              BC_Programmer


                Mastermind
              • Typing is no substitute for thinking.
              • Thanked: 1140
                • Yes
                • Yes
                • BC-Programming.com
              • Certifications: List
              • Computer: Specs
              • Experience: Beginner
              • OS: Windows 11
              Re: Batch script to check files
              « Reply #8 on: April 21, 2009, 08:55:49 AM »
              No- he means you save the perl file as a perl file (.pl) and then create a small batch file that invokes the perl interpreter with t hat perl code. you can then set up an automated task using the batch just as you would have given a pure batch solution.
              I was trying to dereference Null Pointers before it was cool.

              satya85

                Topic Starter


                Greenhorn

                Re: Batch script to check files
                « Reply #9 on: April 21, 2009, 10:08:20 AM »
                ok got it , i installed active perl to run this. Anyways i need a batch script that does the same. Thanks ghostdog for the perl script.

                can i have text file that has list of file names and then pass each file name as parameter to check for timestamp and move to destination folder.

                gh0std0g74



                  Apprentice

                  Thanked: 37
                  Re: Batch script to check files
                  « Reply #10 on: April 21, 2009, 10:39:17 AM »
                  ok got it , i installed active perl to run this. Anyways i need a batch script that does the same. Thanks ghostdog for the perl script.

                  can i have text file that has list of file names and then pass each file name as parameter to check for timestamp and move to destination folder.
                  sure
                  Code: [Select]
                  use File::stat;
                  use Time::localtime;
                  use File::Copy;
                  my $destination = "some path";
                  open(FH,"<", "file") or die "Cannot open file:$!";
                  while (<FH>){
                   chomp; #get rid of newline
                   my $st = stat($_) or die "No $_: $!";
                   my $timestamp = $st->mtime; #get file modified time in secs since epoch
                   # depending on how you want to check time stamp ..code here
                   move($_,$destination);
                   
                  }
                  close(FH);

                  please read up the documentation to get started. perldoc perl.