1.

Solve : Make numbered copies of the same short file.?

Answer»

I have a small file called 'tone.mp3' which is just a short tone.
I want to make numbered copies of the file, from 00 to 99, like this.
tone_00.mp3
tone_01.mp3
tone_02.mp2
until
tone_99.mp3

I know it woudl take me THREE hours to do it...
- but you guys can do it in 3 min or less.
2 min 48 secs lol.

Code: [SELECT]@echo off
cls
setlocal enabledelayedexpansion

for /l %%1 in (0,1,99) do (
set seq=%%1
if !seq! lss 10 set seq=0!seq!
copy tone.mp3 %temp%\tone_!seq!.mp3>nul
)

Code: [Select]import java.io.*;
import java.nio.channels.*;
import java.nio.*;

public class SequentialCopy {
public static void main(String[] args){
if (args.length != 2){
System.out.printf("USAGE: java SequentialCopy [file name] [how many]\n");
System.exit(1);
}
File file = new File(args[0]);
int numLength=args[1].length();
int MAX = Integer.parseInt(args[1]);
if ( file.exists() && file.isFile() ){
String f = file.toString();
String extension = "";
int d=f.length();
if ( f.indexOf(".") != -1 ) {
d = f.lastIndexOf(".") ; //get the last dot
extension = f.substring(d); //get the extension
}
String fileName = f.substring(0,d);
for (int i = 0; i<=MAX ;i++ ){
String newName = fileName + "_" + String.format("%0"+numLength+"d",i) + extension;
if( copyFile(f , newName) == 0 ){
System.out.println( "File: " + f + " copied to: " + newName );
}
}
}
}
public static int copyFile( String src, String dest ){
File inputFile = new File(src);
File outputFile = new File(dest);
try {
FileChannel in = new FileInputStream( inputFile).getChannel() ;
FileChannel out = new FileOutputStream (outputFile ).getChannel() ;
ByteBuffer buffer = ByteBuffer.allocate( 204800 );
while (true) {
buffer.clear();
int r = in.read( buffer );
if (r==-1) { break; }
buffer.flip();
out.write( buffer );
}
in.close();
out.close();
return 0;
}catch (IOEXCEPTION ex ){
ex.printStackTrace();
return 1;
}
}
}

for making 1001 files
Code: [Select]java SequentialCopy "tone.mp3" 1000

[recovering disk space - old attachment deleted by ADMIN]Code: [Select]@echo off
For /L %%p in (1,1,99) DO copy tone.mp3 tone%%p.mp3
For /L %%p in (1,1,9) DO ren tone%%p.mp3 tone0%%p.mp3
You guys are awesome!
Thanks.



Discussion

No Comment Found