Page 1 of 1
need help of hybrid jscript from vb DateLastModified
Posted: 19 Jul 2013 12:51
by brinda
hi all,
borrowed foxidrive's vb/batch code from below
http://www.dostips.com/forum/viewtopic.php?f=3&t=3250and 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-windowstried 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.
Re: need help of hybrid jscript from vb DateLastModified
Posted: 19 Jul 2013 14:49
by jeb
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.
Re: need help of hybrid jscript from vb DateLastModified
Posted: 19 Jul 2013 16:23
by npocmaka_
I don't think jscript has CStr and RightRight functions..
Re: need help of hybrid jscript from vb DateLastModified
Posted: 19 Jul 2013 21:18
by brinda
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-aSUB 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
at least i have 1 working solution. this is great
on jscript, try removing the ")" (probably brain stopped working
). 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.
Re: need help of hybrid jscript from vb DateLastModified
Posted: 20 Jul 2013 01:19
by npocmaka_
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...
Re: need help of hybrid jscript from vb DateLastModified
Posted: 20 Jul 2013 12:08
by penpen
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.
Re: need help of hybrid jscript from vb DateLastModified
Posted: 20 Jul 2013 18:53
by brinda
npocmaka_,
thank for showing the way. was still searching after seeing your response.
penpen,
thank you so much.
you have completed the entire things done. and it even have milliseconds
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
Re: need help of hybrid jscript from vb DateLastModified
Posted: 21 Jul 2013 20:10
by Ed Dyreen
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.
Re: need help of hybrid jscript from vb DateLastModified
Posted: 21 Jul 2013 23:41
by brinda
Ed,
thanks for providing the links
. It helps to see the errors much clearly except for the nag screen
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
Re: need help of hybrid jscript from vb DateLastModified
Posted: 24 Jul 2013 07:26
by brinda
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
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.