1.

Solve : Extracting chosen characters from command's results?

Answer»

Hello,

I want to make simple batch file that copies log file and then adds original file's modification date and run program.
I remember some time ago i was doing something like printing only chosen characters from command's result with +X -Y (from X to Y, or something very similar) but i totally forgotten how exactly and where should i use it.

In that case i would like to use 1-10, 13-14 and 16-17 characters (which should be date and time) of "dir ./debug.html" result Code: [Select]import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.Format;

public class CopyFileWithModification {
    public static void main(String[] args){
        if (args.length != 1){
            System.out.printf("Usage: java CopyFileWithModification <file name>\n");
            System.exit(1);
        }
        File fileName = new File(args[0]);
        if ( fileName.exists() && fileName.isFile() ){
            long t = fileName.lastModified() ;
            Format dateFormatter = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
            String newFileName = fileName + "_" + dateFormatter.format( new Date(t) ) ;
            copyFile( args[0] , newFileName) ;
        }
    }
    public static int copyFile( String src, String dest ){
          File inputFile = new File(src);
          File outputFile = new File(dest);
          try {
            FILEREADER in = new FileReader(inputFile);
            FileWriter out = new FileWriter(outputFile);
            int ch;
            while ( (ch = in.read() ) != -1) out.write(ch);
            out.close();
            in.close();
            return 0;
          }catch (IOException ex ){
            ex.printStackTrace();
            return 1;
          }
    }
}


[recovering disk space - old attachment deleted by admin]Wow, copying a file a single byte at a time. That's EFFICIENT. I MEAN it would work here, but on the other hand, java!=batch so it doesn't really meet their needs.





Code: [Select]echo off
REM copylog <source> <destination>
copy %1 %2
echo %date% %time% >> %2

Quote from: BC_Programmer on December 24, 2010, 05:36:31 PM

Wow, copying a file a single byte at a time. That's efficient. I mean it would work here, but on the other hand, java!=batch so it doesn't really meet their needs.
true , java is not batch. However, put the java statement inside a text file, name it with extension .bat, and its called a batch. !

Quote
Code: [Select]echo off
REM copylog <source> <destination>
copy %1 %2
echo %date% %time% >> %2

are you serious? you are appending the current date time to the destination file contents. don't think its what OP wants.
Quote from: ghostdog74 on December 24, 2010, 06:50:47 PM
true , java is not batch. However, put the java statement inside a text file, name it with extension .bat, and its called a batch. !
java statements don't work in text files. and you cannot pass a .java file with the java or javaw executables. now if you use the compiled version, that would work, so one would need to either compile the provided java code using javac from the JDK (which of course requires downloading the JDK) download and use the attached class file (which requires trust of the poster that the .class is actually the compiled equivalent of the .java provided, not that there is any reason to think otherwise) and so forth. All this for a rather inefficient SOLUTION (well, ok, so the copy can easily just be replaced with a more appropriate java.nio implementation that uses chunks)


Quote
are you serious? you are appending the current date time to the destination file contents. don't think its what OP wants.
you're right... didn't read the OP quite thoroughly enough, they want the modification date of the original file. Batch has some goofy way of doing that with that weird tilde string manip or whatever it does... I think. Damned if I know, I stay away from the really freaky batch stuff like that whenever I can.

But I'm not sure if it would work to add to the filename (it's sort of hard to tell if that's what they want or wether they want it appended to the end of the logfile itself... they both sort of make sense), since it might have evil slashes in the date... (the java example works around that by providing it's own format.)






Quote from: BC_Programmer on December 24, 2010, 07:06:45 PM
java statements don't work in text files. and you cannot pass a .java file with the java or javaw executables. now if you use the compiled version, that would work, so one would need to either compile the provided java code using javac from the JDK (which of course requires downloading the JDK) download and use the attached class file (which requires trust of the poster that the .class is actually the compiled equivalent of the .java provided, not that there is any reason to think otherwise) and so forth. All this for a rather inefficient solution (well, ok, so the copy can easily just be replaced with a more appropriate java.nio implementation that uses chunks)
i am not a Java advocate, but I do know that it doesn't matter.  What matters is if OP has no worries about using the solution, then its his business. Its not for me or you to judge whether its right or wrong.

Quote
All this for a rather inefficient solution
Whenever such a statement is mentioned about inefficiency, its better to provide a benchmark to prove what you said is correct ( as pertaining to this particular case). Otherwise, its better not to assume that one thing is better than the other.
Quote from: ghostdog74 on December 24, 2010, 07:25:11 PM
Whenever such a statement is mentioned about inefficiency, its better to provide a benchmark to prove what you said is correct ( as pertaining to this particular case). Otherwise, its better not to assume that one thing is better than the other.

I don't need a benchmark. Copying a file a single byte at a time will be slower then copying it in larger chunks. It's common sense. Quote from: BC_Programmer on December 24, 2010, 09:14:18 PM
I don't need a benchmark. Copying a file a single byte at a time will be slower then copying it in larger chunks. It's common sense.
thanks for your comments.  i did not design the code with inefficiency in mind. It  can certainly be changed using BufferedInputStream/BufferedOutputStream which will be more efficient than copying byte by byte, or using NIO.
I've kinda never seen such an offtopic

So let's define my problem again, i've attached command and result to post.

I want to extract circled characters from output to some variable, then use that variable for creating new name of debug.html i just do not remember how to CHOOSE those characters.

[recovering disk space - old attachment deleted by admin]echo off
for /f "delims=" %%A in ('dir .\debug.html ^| find /i "debug.html"') do set line=%%A
set line=%line:~0,10%%line:~12,2%%line:~15,2%
Thats not way i remember i was doing, but works too, thanks alot Quote from: drag0nius on December 25, 2010, 08:20:33 AM
Thats not way i remember i was doing

I am sorry it doesn't look like you remember, but nevertheless this is how to extract a substring from a text string: we use the notation %variable:~X,Y% where X and Y are numbers. X is the offset from the start, (so to begin with the first character we use 0 (zero) ) and Y is the number of characters to take.



Discussion

No Comment Found