1

How to get the Previous Sunday date based on current date in batch file. For getting current date i am using this...

echo %date:~-3,2%\%date:~7,2%\%date:~10,4%
RobinHood
  • 2,367
  • 11
  • 46
  • 73

1 Answers1

0
@if (@x)==(@y) @end /***** jscript comment ******
@echo off
for /f "delims=" %%d in ('cscript //E:JScript //nologo "%~f0"') do set "p_sund=%%d"
echo %p_sund%
exit /b 0
*****  end comment *********/
var beforeOneWeek = new Date(new Date().getTime() - 60 * 60 * 24 * 7 * 1000)
  , day = beforeOneWeek.getDay()
  , diffToMonday = beforeOneWeek.getDate() - day + (day === 0 ? -6 : 1)
  , lastMonday = new Date(beforeOneWeek.setDate(diffToMonday))
  , lastSunday = new Date(beforeOneWeek.setDate(diffToMonday + 6));

  WScript.Echo(beforeOneWeek);

Borrowed some code from here.

EDIT in case the windows script host exe is disabled the mshta cab be used:

@if (@x)==(@y) @end /***** jscript comment ******
@echo off
for /f "delims=" %%d in ('cscript //E:JScript //nologo "%~f0"') do set "p_sund=%%d"
rem echo %p_sund%
Echo ^<html^>^<head^>>"temp.hh"
Echo ^<hta:application windowstate="minimize"^>>>"temp.hh"
Echo ^<script type="text/jscript" src="%~nx0"^>>>"temp.hh"
Echo ^</script^>^</head^>^</html^>>>"temp.hh"

Mshta.exe "temp.hh"
type temp.res

del /q /f "temp.hh" ""temp.res"" >nul 2>&1


exit /b 0
*****  end comment *********/
var beforeOneWeek = new Date(new Date().getTime() - 60 * 60 * 24 * 7 * 1000)
  , day = beforeOneWeek.getDay()
  , diffToMonday = beforeOneWeek.getDate() - day + (day === 0 ? -6 : 1)
  , lastMonday = new Date(beforeOneWeek.setDate(diffToMonday))
  , lastSunday = new Date(beforeOneWeek.setDate(diffToMonday + 6));

  WScript.Echo(beforeOneWeek);

  var objFSO=new ActiveXObject("Scripting.FileSystemObject");
  var outFile="temp.res";
  var objFile = objFSO.CreateTextFile(outFile,true);
  objFile.Write(beforeOneWeek);
  objFile.Close();

later will think about powershell solution.

Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • I had used this code in.bat file and executed. The command prompt appears and disappears. I had also put "pause" command. – RobinHood Jan 22 '14 at 10:18
  • Could you check if your windows script host exe is disabled: http://technet.microsoft.com/en-us/library/ee198684.aspx – npocmaka Jan 22 '14 at 10:23
  • But in all the other .bat files, i can able to see the result in command prompt. – RobinHood Jan 22 '14 at 10:28
  • This is a hybrid bat/jscript (need to be saved as bat) and uses jscript/whs.If the windows host script is disablaed you cannot run this directly. – npocmaka Jan 22 '14 at 10:30
  • Instead of using "hybrid bat/jscript", we cant able to do in the normal batch script. Because i am going to use this in server and i cant able to change the registry. – RobinHood Jan 22 '14 at 10:42
  • Do you have powershell on your server? – npocmaka Jan 22 '14 at 10:44
  • Thanks Dude..still i cant to able to stop the command prompt. But whenever the prompt appears i can able to see the sunday date there. From there i can catch it up... – RobinHood Jan 22 '14 at 11:11