need help of hybrid jscript from vb DateLastModified

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
brinda
Posts: 78
Joined: 25 Apr 2012 23:51

need help of hybrid jscript from vb DateLastModified

#1 Post by brinda » 19 Jul 2013 12:51

hi all,

borrowed foxidrive's vb/batch code from below
http://www.dostips.com/forum/viewtopic.php?f=3&t=3250

and went through it and refer a few example scripts and came with a 3 line solution

Code: Select all

Set oFSO = CreateObject("Scripting.FileSystemObject")
n = oFSO.GetFile(WScript.Arguments(0)).DateLastModified
Wscript.StdOut.Write CStr(Year(n)) + Right(100+Month(n),2) + Right(100+Day(n),2) + "-" + Right(100+Hour(n),2) + Right(100+Minute(n),2) + Right(100+Second(n),2) + " "


used Wscript.StdOut.Write example from stackoverflow so there would be no CRLF left behind. since there is no CRLF, i could than use the existing old script to write on the same line before echo to new line.

so on using batch this works

Code: Select all

D:\>cscript /nologo vbsfiledate.vbs "d:\test test.txt"
20130720-011206


wanted to make a no temporary file so i could learn from this.

borrowed dave's jeval.bat from stackoverflow(it is the shortest compare to repl and antonio's findrepl)
http://stackoverflow.com/questions/4495576/echo-e-equivalent-in-windows

tried to look up from mdsn of different between jscript and do but still getting some errors. What i have now is this

Code: Select all

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment

::: Batch part ::::
@echo off
cscript //nologo //e:JScript "%~f0" %*
exit /b

*** JScript part ***/
var fso, n;
fso = new ActiveXObject("Scripting.FileSystemObject");
n = fso.GetFile(WScript.arguments(0)).DateLastModified;))
Wscript.StdOut.Write(CStr(n.Year) + Right(100+n.Month,2) + Right(100+n.Day,2) + "-" + Right(100+n.Hour,2) + Right(100+n.Minute,2) + Right(100+n.Second,2) + " ");


and the error below

Code: Select all

D:\>jdt "d:\test.txt"
D:\jdt.bat(11, 56) Microsoft JScript compilation error: Syntax error


needed some help on this. thanks.

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: need help of hybrid jscript from vb DateLastModified

#2 Post by jeb » 19 Jul 2013 14:49

Hi Brinda,

my notepad++ say that at position line11 col 56 is the ")", and as there isn't an opening one, that could be the problem

Code: Select all

n = fso.GetFile(WScript.arguments(0)).DateLastModified;

I removed the last two brackets.

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: need help of hybrid jscript from vb DateLastModified

#3 Post by npocmaka_ » 19 Jul 2013 16:23

I don't think jscript has CStr and RightRight functions..

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: need help of hybrid jscript from vb DateLastModified

#4 Post by brinda » 19 Jul 2013 21:18

jeb,

seeing your name ringed a bell and bought me to your script dave's showed when i wanted to do a hybrid script
http://stackoverflow.com/questions/9074476/is-it-possible-to-embed-and-execute-vbscript-within-a-batch-file-without-using-a

SUB is Alt-026

Code: Select all

::'SUB@cscript //nologo //e:vbscript "%~f0" %* & exit /b
Set oFSO = CreateObject("Scripting.FileSystemObject")
n = oFSO.GetFile(WScript.Arguments(0)).DateLastModified
Wscript.StdOut.Write CStr(Year(n)) + Right(100+Month(n),2) + Right(100+Day(n),2) + "-" + Right(100+Hour(n),2) + Right(100+Minute(n),2) + Right(100+Second(n),2) + " "


on dos

Code: Select all

D:\>v "d:\test.txt"
20130720-104613


wow. thanks jeb :D at least i have 1 working solution. this is great :D

on jscript, try removing the ")" (probably brain stopped working :oops: ). thanks for informing on notepad++ was using scite, i will download it

Code: Select all

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment

::: Batch part ::::
@echo off
cscript //nologo //e:JScript "%~f0" %*
exit /b

*** JScript part ***/
var fso, n;
fso = new ActiveXObject("Scripting.FileSystemObject");
n = fso.GetFile(WScript.arguments(0)).DateLastModified;
Wscript.StdOut.Write(CStr(n.Year) + Right(100+n.Month,2) + Right(100+n.Day,2) + "-" + Right(100+n.Hour,2) + Right(100+n.Minute,2) + Right(100+n.Second,2) + " ");



getting a different error. Oh boy. this is getting even more error

Code: Select all

D:\>j "d:\test.txt"
D:\j.bat(12, 1) Microsoft JScript runtime error: 'Wscript' is undefined


npocmaka_,
thanks - searching google. probably need to do more homework on jscript before getting it to work. Believe i could not cut and chop like foxidrive's vb code which seems somewhat easier than this jscript. :roll:

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: need help of hybrid jscript from vb DateLastModified

#5 Post by npocmaka_ » 20 Jul 2013 01:19

var args=WScript.Arguments;

WshShell.AppActivate(args.Item(0));

Code: Select all

n = fso.GetFile(WScript.arguments(0)).DateLastModified;)


rewrite this to

Code: Select all

var args=WScript.Arguments;
n = fso.GetFile(args.Item(0).DateLastModified);


This is the way to get the arguments in jscript. Not sure if DateLastModified will work.
Instead CInt use parseInt.May you'll need to write your own Right.

BTW. You can create batch/vbscript hybrid without temp file too...

penpen
Expert
Posts: 1992
Joined: 23 Jun 2013 06:15
Location: Germany

Re: need help of hybrid jscript from vb DateLastModified

#6 Post by penpen » 20 Jul 2013 12:08

There are some errors. Here is a fixed version:

Code: Select all

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment

::: Batch part ::::
@echo off
cscript //nologo //e:JScript "%~f0" %*
exit /b

*** JScript part ***/
var fso = new ActiveXObject("Scripting.FileSystemObject");
var n   = new Date (fso.getFile (WScript.Arguments.Unnamed.Item (0)).dateLastModified);

// WScript.echo ("" + n.getYear () + (n.getMonth () + 1) + n.getDate () + "-" + n.getHours () + n.getMinutes () + n.getSeconds () + n.getMilliseconds ());
WScript.echo ("" + ("" + (10000 + n.getYear ())).substring (1)
   + ("" + (100 + (n.getMonth () + 1))).substring (1)
   + ("" + (100 + n.getDate ())).substring (1)
   + "-"
   + ("" + (100 + n.getHours ())).substring (1)
   + ("" + (100 + n.getMinutes ())).substring (1)
   + ("" + (100 + n.getSeconds ())).substring (1)
//   + ("" + (1000 + n.getMilliseconds ())).substring (1)
);

penpen

Edit: Changed the WScript.echo line, i had forgotten to normalize the number representations.

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: need help of hybrid jscript from vb DateLastModified

#7 Post by brinda » 20 Jul 2013 18:53

npocmaka_,

:D thank for showing the way. was still searching after seeing your response.

penpen,
thank you so much. :D you have completed the entire things done. and it even have milliseconds :D

added WScript.StdOut.Write in place of WScript.echo. so the output does not produce CrLF.
took me a while to know that this // means comment after a few trial :)

ammended one

Code: Select all

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment

::: Batch part ::::
@echo off
cscript //nologo //e:JScript "%~f0" %*
exit /b

*** JScript part ***/
var fso = new ActiveXObject("Scripting.FileSystemObject");
var n   = new Date (fso.getFile (WScript.Arguments.Unnamed.Item (0)).dateLastModified);

WScript.StdOut.Write ("" + ("" + (10000 + n.getYear ())).substring (1)
   + ("" + (100 + (n.getMonth () + 1))).substring (1)
   + ("" + (100 + n.getDate ())).substring (1)
   + "-"
   + ("" + (100 + n.getHours ())).substring (1)
   + ("" + (100 + n.getMinutes ())).substring (1)
   + ("" + (100 + n.getSeconds ())).substring (1)
   + ("" + (1000 + n.getMilliseconds ())).substring (1)
   + " "
);


Code: Select all

D:\Documents\Downloads>jd test.txt
20130621-223922000


thank you so much all. time to learn a lot more from this examples + compare between vbscript and jscript

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: need help of hybrid jscript from vb DateLastModified

#8 Post by Ed Dyreen » 21 Jul 2013 20:10

With vbsEdit ( free ), clicking help on a highlighted command gets you to the help for that command, where examples and explanations are found for vb, jscript and more. It could help when doing a translation battle.

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: need help of hybrid jscript from vb DateLastModified

#9 Post by brinda » 21 Jul 2013 23:41

Ed,

thanks for providing the links :) . It helps to see the errors much clearly except for the nag screen :D

i get an insight why penpen have commented the milliseconds. it always show 000 for files in winxp and win 2000.

this is using the jscript

Code: Select all

D:\Documents\Downloads>jd twister.txt
20130721-235651000

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: need help of hybrid jscript from vb DateLastModified

#10 Post by brinda » 24 Jul 2013 07:26

sorry, this is off topic(due to C usage).

getting filedate in millisecond could not be found on vbscript or js. There is one small program in C from http://myc01.free.fr/filedate/

after reading and gettings some help from penpen(ansi C book). downloaded Tiny C compiler(gives a smaller size around 3KB http://bellard.org/tcc/. This outputs no CRLF and good for sort. working in win2000 and winxp

example

Code: Select all

D:\Documents\Downloads>filedate /w test.txt
2013-07-21 23:56:51.203


the shorten code

Code: Select all

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

FILETIME GetFileLastWriteTime (char *name, char test)
{
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;

    if ( (hFind = FindFirstFile (name, &FindFileData)) == INVALID_HANDLE_VALUE)
    {
        puts ("Error: file not found.");
        exit (1);
    }
    else
        FindClose (hFind);

    if (test == 'A') return FindFileData.ftLastAccessTime;
    if (test == 'C') return FindFileData.ftCreationTime;
    if (test == 'W') return FindFileData.ftLastWriteTime;


void PrintMessage (void)
{
    puts ("Usage: filedate.exe /[ACW] file");
    puts ("  A = last Access time");
    puts ("  C = Creation time");
    puts ("  W = last Write time");
    puts ("  Date format: Year-Month-Day Hour:Minute:Second.Milliseconds ");
    exit (2);


int main (int argc, char *argv[])
{
    int nResult = 0;
    char test;
    SYSTEMTIME st;
    FILETIME ft1;

    if (argc                      <=  2 ) PrintMessage ();
    if (strlen ( (char*) argv[1]) !=  2 ) PrintMessage ();
    if (*argv[1]                  != '/') PrintMessage ();

    test = toupper (* (argv[1] + 1));

        ft1 = GetFileLastWriteTime (argv[2], test);

        FileTimeToLocalFileTime (&ft1, &ft1);
        FileTimeToSystemTime    (&ft1, &st );

        printf ("%04d-%02d-%02d %02d:%02d:%02d.%03d ",
            st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);

    return nResult;



interesting just found this link from Frank P. Westlake
main link

Code: Select all

http://ss64.net/westlake/


download the file below.

Code: Select all

link      http://ss64.net/westlake/nt/index.html

fi.zip (FileInfo) Like DIR, but more. Also lists file link, file stream, file security, and device information and filters by file age and size.

attribnt.zip: Similar to ATTRIB but also sets and clears the file and directory attributes for compression, offline, and temporary file. 

Post Reply