Copy even or odd files

weklica
weklica used Ask the Experts™
on
I need to copy even numbered or odd numbered files from a folder or CD into a new directory.  The name of the files are literally 1 2 3 4 5 6 7

So, how do I copy only even or only odd files?

I would like this to work in windows and MAC.  So, I am looking for a batch file version and maybe an applescript that may work.  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT / Software Engineering Consultant
Top Expert 2016
Commented:
On the Windows Batch side this is pretty simple, something like this should work:

@echo off
setlocal EnableDelayedExpansion
for %%A in ("c:\temp\*.*") do (
  set /A mod=%%~nA % 2
  if !mod! EQU 0 (
    copy "%%A" "c:\temp\even"
  ) else (
    copy "%%A" "c:\temp\odd"
  )
)

I can't speak to an Apple script.

~bp
Todd GerbertIT Consultant
Top Expert 2010
Commented:
You can use the for command.  This example is intended for use inside a batch file (to use it directly on the command line use a single percent sign before the i).
This will copy odd numbered files named 1.txt, 3.txt, etc, up to 9.txt from the folder c:\test1 into the folder named c:\test2
To copy more files, change the 9 to a higher odd-number (you can make it higher than the actual number of files)
To copy even files change the 1 to a 2 and the 9 to 10 (or some higher even-number).

for /l %%i in (1,2,9) do copy c:\test1\%%i.txt c:\test2

Open in new window

Author

Commented:
BILLPREW:  YOUR VERSION COPIES ALL OF THE NUMBERED FILES INTO ODD AND THE NON NUMERIC FILES INTO EVEN.  I AM SURE IT IS CLOSE, SOME SMALL ERROR?

TGERBERT: YOUR SOLUTION WORKED WELL.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Yes, my solution was an example, since your original post didn't indicate which you wanted to copy (even or odd) I showed how you could separate based on filename, and then act accordingly.

~bp

Author

Commented:
ah, i did edit the directories, but not the 'even  or odd'  rather, made those as directories thinking the files would dump into those folders.  my bad. will try again.  Thanks
If you like this to work in windows(cmd.exe) and MAC, then i recommend a cross platform  programming language, eg, Python or Perl. Here's a Perl snippets (similarly can be done with Python). The advantage to this method is that you maintain only 1 script and of course, you must have these programming language installed before you can use.



#!/usr/bin/perl

use strict;
use warnings;
use File::Copy;
my $destination="/destination" ; #put destination here
while(my $file=<[0-9]*>){
   if ( $file % 2 eq 1 ){
      print $file ."\n";
      copy($file,$destination) or die "Copy failed: $!";
   }
}

Open in new window

Author

Commented:
Sorry for delay, was waiting for MAC comment.  Windows worked fine.  Thanks much!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial