Batch to WSH

December 27, 2009 at 08:26:58
Specs: Windows XP
Dear Experts ,

I have made a batch script with the help of this
site what it does it ftp's a server and gets a file
which is updated at the server with yesterdays
date in it name. as it is mentioned in earlier
post batch is not good with dates and this
script will not work on the 1 of every month.

Can some one help me to modify it into WSH
VBscript thanks alot

SETLOCAL
echo open "10.111.111.111" >>event.txt
echo Username>>event.txt
echo password>>event.txt
echo cd /home/om/ma/som/ >>event.txt
SET dwMONTH=%DATE:~4,2%
SET /A dwDAY=%DATE:~7,2%-1
SET dwYEAR=%DATE:~10,4%
SET dwDate=get som-%dwDAY%-
%dwMONTH%.txt.Z
echo binary >>event.txt
echo %dwDate% >>event.txt

ftp -s:event.txt


See More: Batch to WSH

Report •


#1
December 27, 2009 at 13:53:03
Can't be that hard to get it correct for fixing that number of the month ... I mean, logically:

if day=0
if specific months day=30
else if other months day =31
else if "february" day=28

And we forget about that special day ;)


Report •

#2
December 27, 2009 at 14:10:10
yeah, this might do it in B.S mode:
:: your script setup as you have it here at top
::...
SET dwMONTH=%DATE:~4,2%
SET /A dwDAY=%DATE:~7,2%-1
SET dwYEAR=%DATE:~10,4%
call :subdate
SET dwDate=get som-%dwDAY%-%dwMONTH%.txt.Z
:... rest of your script here
ftp -s...
GOTO :EOF

:subdate
if %dwDAY% neq 0 goto :ok
setlocal enabledelayedexpansion
set /a offs=0
for /L %%a in (1 1 12) do (
if %%a equ 8 set /a offs=1
set /a test="(%%a+!offs!)%%2"
set /a nn%%a=30+!test!
)
set /a test="dwYEAR%%4"
set nn2=28
if !test! equ 0 set /a nn2+=1
set /a dwMONTH="(%dwMONTH%+10)%%12+1"
if !dwMONTH! equ 12 set /a dwYEAR-=1
set dwDAY=!nn%dwMONTH%!
endlocal
:ok

sort-of tested (as standalone, not embedded in your script)


Report •

#3
December 27, 2009 at 14:15:13
@tvc i like yours better. mine is kind of convoluted...
yeah, this looks MUCH cleaner... not tested..
SET dwMONTH=%DATE:~4,2%
SET /A dwDAY=%DATE:~7,2%-1
SET dwYEAR=%DATE:~10,4%
if dwDAY equ 0 (
set /a dwDAY=31
set /a dwMONTH="(%dwMONTH%+10)%%12+1"
call %dwMONTH% 2>nul)
SET dwDate=get som-%dwDAY%-%dwMONTH%.txt.Z
:... rest of your script here
ftp -s...
GOTO :EOF

:4
:6
:9
:11
set /a dwDAY-=1
goto :eof
:12 set /a dwYEAR -=1
goto :eof
:2
set /a dwDAY=28
set /a leap="dwYEAR%%4"
if %leap% equ 0 (set /a dwDAY)+=1


Report •

Related Solutions

#4
December 27, 2009 at 22:48:56
thank you all for the support

Report •

#5
December 28, 2009 at 00:10:59
I know this is late.....

From what I understand it is much easier in vbs(I'm surprised one of vbs people didn't jump in.....).

Here is a XP batch that is semi-independent of regional settings, it does need to be run in english though.

Includes leap year and jan 1st support.

Returns errorlevel of YYYYMMDD:

http://pastebin.com/m1e1e36e2


Batch Variable how to


Report •

#6
December 28, 2009 at 04:14:31
This will get the date regardless of language. Even works in DOS. LOL
=================================

@echo off
if not %OS%!==! setLocal enableDELAYedexpansion

::== YMD14.bat

@echo off > d.d

>> d.d echo E 0100 B4 2A CD 21 B4 4C CD 21
>> d.d echo N DAY.COM
>> d.d echo RCX
>> d.d echo 8
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 F0 B4 4C CD 21
>> d.d echo N MONTH.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 89 C8 B4 4C CD 21
>> d.d echo N YEAR.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 D0 B4 4C CD 21
>> d.d echo N DAT.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo Q

debug < d.d > nul
del d.d

dat
for %%a in (1 2 3 4 5 6 7 8 9                                                                  ) do if errorlevel %%a set DD=0%%a
for %%a in (                  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) do if errorlevel %%a set DD=%%a
echo Date %DD%

day
if errorlevel 0 set day=Sunday
if errorlevel 1 set day=Monday
if errorlevel 2 set day=Tuesday
if errorlevel 3 set day=Wednesday
if errorlevel 4 set day=Thursday
if errorlevel 5 set day=Friday
if errorlevel 6 set day=Saturday
echo Day %day%

month
for %%a in (1 2 3 4 5 6 7 8 9         ) do if errorlevel %%a set MM=0%%a
for %%a in (                  10 11 12) do if errorlevel %%a set MM=%%a
echo Month %MM%

year
if errorlevel 215 set YYYY=2007
if errorlevel 216 set YYYY=2008
if errorlevel 217 set YYYY=2009
if errorlevel 218 set YYYY=2010
if errorlevel 219 set YYYY=2011
if errorlevel 220 set YYYY=2012
echo Year %YYYY%

set   YMD=%YYYY%%MM%%DD%
echo YMD is: %YMD%

del day.com
del month.com
del year.com
del dat.com

::============================ DONE YMD14 ==========================


=====================================
Helping others achieve escape felicity

M2


Report •

#7
December 28, 2009 at 07:45:58
@nbrane

the script now shows like this ..

ET dwMONTH=%DATE:~4,2%
SET /A dwDAY=%DATE:~7,2%-1
SET dwYEAR=%DATE:~10,4%
call :subdate
SET dwDate=get som-%dwDAY%-%dwMONTH%.txt.Z
echo open "ftp.server.com" >>event.txt
echo username>>event.txt
echo password>>event.txt
echo cd /ho/om/ma/somc/ >>event.txt
echo binary >>event.txt
echo %dwDate% >>event.txt
echo bye >>event.txt

GOTO :EOF
:subdate
if %dwDAY% neq 0 goto :ok
setlocal enabledelayedexpansion
set /a offs=0
for /L %%a in (1 1 12) do (
if %%a equ 8 set /a offs=1
set /a test="(%%a+!offs!)%%2"
set /a nn%%a=30+!test!
)
set /a test="dwYEAR%%4"
set nn2=28
if !test! equ 0 set /a nn2+=1
set /a dwMONTH="(%dwMONTH%+10)%%12+1"
if !dwMONTH! equ 12 set /a dwYEAR-=1
set dwDAY=!nn%dwMONTH%!
endlocal

I am sure i am making mistake as if i set the date on my
computer to 1-Dec-09 this doesn't seems to work the out is
mentioned below

open "ftp.server.com"
username
password
cd /ho/om/ma/somc/
binary
get som-0-.txt.Z
bye

Kindly help me out thanks


Report •

#8
December 28, 2009 at 08:38:52
There *IS NO* general DATE substring solution.

It might help to post the filenames on the ftp server.


=====================================
Helping others achieve escape felicity

M2


Report •

#9
December 28, 2009 at 09:35:20
The File name on ftp server is like this

"som-28-12.txt.Z" and it will be created on 29th December thats
why i require yesterdays date .


Report •

#10
December 28, 2009 at 09:41:55
Would it not do to get the latest file of the form:

som-??-??.txt.Z


=====================================
Helping others achieve escape felicity

M2


Report •

#11
December 28, 2009 at 10:35:53
it can do but sometimes there are different files present on the
server .

Thanks alot I really appreciate all the help from all you guys M2
nbrain and all


Report •

#12
December 28, 2009 at 11:12:25
sounds like you gave up already?
remove the "setlocal enabledelayedexpansion" and the "endlocal" lines from the :subdate, and change your "SETLOCAL" at the very top to:
setlocal enabledelayedexpansion

that fixed it on my end of town.


Report •

#13
December 28, 2009 at 11:17:30
"it can do but sometimes there are different files present on the
server . "

Sure, but why not concentrate on thr filrs needed?


=====================================
Helping others achieve escape felicity

M2


Report •

#14
December 28, 2009 at 12:31:30
This gets the latest on. It'll need help for New Year.

===========================

:: To turn off prompting for mget/mput use prompt

@echo off

>  s.ftp echo open mysite
>> s.ftp echo user
>> s.ftp echo pass
>> s.ftp echo bin
>> s.ftp echo ls som-*.Z ZLIST
>> s.ftp echo bye

ftp  -s:s.ftp

for /f "tokens=* delims= " %%a in ('sort ^< ZLIST') do (
set F=%%a
)

>  s.ftp echo open mysite
>> s.ftp echo user
>> s.ftp echo pass
>> s.ftp echo bin
>> s.ftp echo get %F%
>> s.ftp echo bye

ftp  -s:s.ftp


=====================================
Helping others achieve escape felicity

M2


Report •

#15
January 16, 2010 at 07:27:20
Thanks alot NBRANE the script is working great with you expert
suggestions.
I would also like to thank M2 for helping me for another script i
was building . Thanks Alot guys.

Report •

#16
January 16, 2010 at 08:55:27
SETlocal enabledelayedexpansion
SET dwMONTH=%DATE:~4,2%
SET /A dwDAY=%DATE:~7,2%-1
SET dwYEAR=%DATE:~10,4%
call :subdate
SET dwDate=unload_dir.%dwYEAR%.%dwMONTH%.%dwDAY%
mkdir %dwDate%

GOTO :EOF

:subdate
if %dwDAY% neq 0 goto :ok
set /a offs=0
for /L %%a in (1 1 12) do (
if %%a equ 8 set /a offs=1
set /a test="(%%a+!offs!)%%2"
set /a nn%%a=30+!test!
)
set /a test="dwYEAR%%4"
set nn2=28
if !test! equ 0 set /a nn2+=1
set /a dwMONTH="(%dwMONTH%+10)%%12+1"
if !dwMONTH! equ 12 set /a dwYEAR-=1
set dwDAY=!nn%dwMONTH%!

@Nbrane :This script is working great but i am unable to sort a problem in this on the 1st of every month (Lets suppose 1-Feb-2010) the output of a directory name comes up like unload_dir.2010.1.31

What i required is "unload_dir.2010.01.31" , i tried manipulating the script but its no match for a newbie like me.

Thanks in advance for suggestions.


Report •

#17
January 16, 2010 at 11:26:05
add one line at bottom, after
set dwDAY=!nn%dwMONTH%!
add:
if "!dwDAY:~1!" equ "" set dwDAY=0!dwDAY!

and if you want the same thing for month (f/e, 1=01), at bottom add:
if "!dwMONTH:~1!" equ "" set dwMONTH=0!dwMONTH!


Report •

#18
January 16, 2010 at 11:40:00
thanks alot Nbrane for your support.

Report •

#19
February 8, 2010 at 06:29:42
Dear Experts ,

Another Query ,

Getting this error while running this script today,


D:\>stats.bat

D:\>SETlocal enabledelayedexpansion

D:\>SET dwMONTH=02

D:\>SET /A dwDAY=08-1
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

D:\>SET dwYEAR=2010

D:\>if "!dwDAY:~1!" EQU "" set dwDAY=0!dwDAY!

D:\>call :subdate
0 was unexpected at this time.

D:\>if neq 0 goto :ok

D:\>

Help required. Thanks.


Report •

#20
February 8, 2010 at 06:40:55
Further More this error is coming on only 8th and 9th of the date .

Report •

#21
February 8, 2010 at 12:03:55
sorry about that, i don't know how it worked up till now, as i wrote this up wrong:
SET /A dwDAY=%DATE:~7,2%-1
change this to:
SET /A dwDAY=%DATE:~8,1%-1

I'll just repost it all, with the mods:
SETlocal enabledelayedexpansion
SET dwMONTH=%DATE:~5,1%
SET dwYEAR=%DATE:~10,4%
SET /A dwDAY=%DATE:~8,1%-1
call :subdate
SET dwDate=unload_dir.%dwYEAR%.%dwMONTH%.%dwDAY%
mkdir %dwDate%
goto :eof

:subdate
if dwDAY neq 0 goto :ok
set /a offs=0
for /L %%a in (1 1 12) do (
if %%a equ 8 set /a offs=1
set /a test="(%%a+!offs!)%%2"
set /a nn%%a=30+!test!
)
set /a test="dwYEAR%%4"
set nn2=28
if !test! equ 0 set /a nn2+=1
set /a dwMONTH="(%dwMONTH%+10)%%12+1"
if !dwMONTH! equ 12 set /a dwYEAR-=1
set dwDAY=!nn%dwMONTH%!
:ok
if "%dwDAY:~1%" equ "" set dwDAY=0%DwDAY%
if "%dwMONTH:~1%" equ "" set dwMONTH=0%dwMONTH%

(edited)
@Razor: yeah, i knew vbscript could do date math but op was using my original "stab" at solution, so i just went with that. You're right though, but I wrote that thing over a month ago and I have also learned some things since (from you, among others!)
Dates are a pita.


Report •

#22
February 8, 2010 at 13:09:04
You know what? I'm just going back to the original post, and throw up a VBScript:
sDay = Right("00" & Day(Date - 1), 2)
sMon = Right("00" & Month(Date - 1), 2)
f = "som-" & sDay & "-" & sMon
Set exec = CreateObject("WScript.Shell").Exec("ftp")
exec.StdIn.Write "o 10.111.111.111"    & vbNewLine _
               & "Username"            & vbNewLine _
               & "password"            & vbNewLine _
               & "cd /home/om/ma/som/" & vbNewLine _
               & "bi"                  & vbNewLine _
               & "get" & f             & vbNewLine _
               & "quit"                & vbNewLine

Do Until exec.Status
  WScript.Sleep 200
Loop
On Error Resume Next
WScript.StdOut.WriteLine exec.StdOut.ReadAll
WScript.Quit exec.ExitCode


Report •

#23
February 9, 2010 at 05:20:45
Hi R2,

Yeah I knew there was a mess ahead.

Date math in BAT is always a bad idea.

I guess my suggestion to get the latest file of form blabla was way too simple. Not yet responded to. LOL


=====================================
Helping others achieve escape felicity

M2


Report •

#24
March 5, 2010 at 11:59:02
I would like to thank you all guys once again for helping me out specially nbrane (Sorry for my late reply)

Report •

#25
March 9, 2010 at 19:18:23
Now this script doesn't work after the 9th, Kindly provide assistance. Now i am thinking that playing with dates in BAT is not an ordinary task :))

Report •

#26
March 9, 2010 at 21:33:51
@makenzie: man, i can't concentrate with all this laughter ringing in my ears!
i will acknowledge incompentance and accept ridicule... failure is only allowed
so many times, then it becomes a Darwinian filter.
i'm sorry for not testing this mother as i should have before rendering it up.
I promise i'll run a test on a full scan of dates today or tomorrow. I redid the whole
shebang, as the errors were accreting even faster than the patches!
This revision "seemed" to work. The math part wasn't faulty, it was just the damn
parsing part (which is always, without exception the hardest part in any prog.
again, my apologies.
(i was going to PM the new script, but what the hell, i want it up for public scrutiny).

@echo off & SETlocal enabledelayedexpansion
set xx=%date: =/%
set xx=%xx:/0=/%
for /f "tokens=2-4 delims=/" %%a in ("%xx%") do (
set xx1=%%a
set xx2=%%b
set xx3=%%c
)
set /a xx2-=1
call :subdate
set mdir=unload_dir.%xx3%.%xx1%.%xx2%
echo %mdir%
mkdir %mdir%
goto :eof

:subdate
if %xx2% neq 0 goto :ok
set /a offs=0
for /L %%a in (1 1 12) do (
if %%a equ 8 set /a offs=1
set /a test="(%%a+!offs!)%%2"
set /a nn%%a=30+!test!
)
set /a test="xx3%%4"
set nn2=28
if !test! equ 0 set /a nn2+=1
set /a xx1="(%xx1%+10)%%12+1"
if !xx1! equ 12 set /a xx3-=1
set xx2=!nn%xx1%!
:ok
for %%i in (1 2) do if "!xx%%i:~1!" equ "" set xx%%i=0!xx%%i!
:: ------ end of script -----
good luck, and be sure to let me know if it kicks a fuss.


Report •

#27
March 9, 2010 at 22:42:36
hahaha :) thanks alot mate for always being so helpful , i will let you know if i get some error . Thanks once again.

Report •

#28
March 10, 2010 at 02:51:08
I get some error, but that may not be surprising. Took the above script, replaced ECHO OFF with ECHO ON, and ran it a couple of times:

D:\Temp>set xx=10/03/2010

D:\Temp>set xx=10/3/2010

D:\Temp>for /F "tokens=2-4 delims=/" %a in ("10/3/2010") do (
set xx1=%a
set xx2=%b
set xx3=%c
)

D:\Temp>(
set xx1=3
set xx2=2010
set xx3=
)

D:\Temp>set /a xx2-=1

D:\Temp>call :subdate

D:\Temp>if 2009 NEQ 0 goto :ok

D:\Temp>for %i in (1 2) do if "!xx%i:~1!" EQU "" set xx%i=0!xx%i!

D:\Temp>if "!xx1:~1!" EQU "" set xx1=0!xx1!

D:\Temp>if "!xx2:~1!" EQU "" set xx2=0!xx2!

D:\Temp>set mdir=unload_dir..03 .2009

D:\Temp>echo unload_dir..03 .2009
unload_dir..03 .2009

D:\Temp>mkdir unload_dir..03 .2009
A subdirectory or file unload_dir..03 already exists.
Error occurred while processing: unload_dir..03.
A subdirectory or file .2009 already exists.
Error occurred while processing: .2009.

D:\Temp>goto :eof

D:\Temp>


Report •

#29
March 10, 2010 at 15:52:06
@tvc: its that godawful date thing again!
my date has the day-of-week as first item, and order as mm/dd/yyyy:
tue 03/10/2010
and converted to look like: tue/3/10/2010
then get tokens 2-4 delimited by "/"

I thought about steering toward the debug-to-errorlevel method but decided against it due to it's complexity.


Report •

#30
March 10, 2010 at 15:57:02
" I thought about steering toward the debug-to-errorlevel method but decided against it due to it's complexity.

"

Yeah, it's much easier to flog the %DATE% month after month.

LOL


=====================================
Helping others achieve escape felicity

M2


Report •

#31
March 10, 2010 at 17:19:23
actually, most of the problems, in this case, arose from my defective scripting and not the date format.
Reviewing the original post, he wanted vbscript in the first place! (dateadd now-1, or yest=date-1) which was nicely rendered by Razor in response #22, so at least he has something else to work with if he so chooses.
dates are a pita, but so are my mistakes! LL

Report •

#32
March 11, 2010 at 02:43:49
> actually, most of the problems, in this case, arose
> from my defective scripting and not the date format.
>

No problem, as long as you know some code is less good, it's all forgiven


Report •

#33
March 11, 2010 at 02:47:01
M2, response nr.6 does not work on my XP:

(it currently is thursday, 11th of march)

C:\>new.cmd
Date 01
Day Sunday
Month 01
Year 2007
YMD is: 20070101

C:\>date
The current date is: 11/03/2010
Enter the new date: (dd-mm-yy)

C:\>time
The current time is: 11:47:33,03
Enter the new time:

C:\>


Report •

#34
March 11, 2010 at 02:50:24
Judago, the URL in Response 5 does not show a lot of code:

- Unknown paste ID, it may have been expired or deleted!


Report •

#35
March 11, 2010 at 03:45:44
@M2: Touche!
LL!
@tvc: try to narrow it down (re: M2 resp #6)
put this into a .com file:
b8044c
cd 21
or from batch:
@echo off & goto :aa
e100 b8 04 4c cd 21
rcx
20
w
q
:: end of pipe
:aa
debug f.com <a.bat
f
echo %errorlevel%

run from batch and see if errorlevel is 4.
next try with date material.


Report •

#36
March 11, 2010 at 11:51:17
tvc,

It's not a cmd it's a BAT; LOL.


=====================================
Helping others achieve escape felicity

M2


Report •

#37
March 11, 2010 at 18:03:39
too much time on my hands?
@goto :AAA
e 100 8A 1E 82 00 80 CB 20 B4 2A CD 21 80 FB 77 74 10
e 110 88 C8 80 FB 79 74 09 88 F0 80 FB 6D 74 02 88 D0
e 120 B4 4C CD 21 03 B4 4C CD 21
RCX
30
W
Q

:: ******************** DATE BUSTER!!! *************************
:: ************ THE UNIVERSAL DATEMATH FOR BATCH!!!! *************
:: add or subtract any no. of days from either current date or given date!
:: generate current date then decrement by 1 day and normalize output.
::With no "setlocal", the script can be called from another script which can
::access the var.s via the env.table
:AAA
@echo off
if not exist dmy.com debug dmy.com < %0.bat >nul 2>nul
if not defined dlm set dlm=-
if "%1"=="" (set sub=0) else (set sub=%1)
set test=%1
set leap=%1
set test=%test:?=%
if "%test%" neq "%leap%" goto :help
if "%2" neq "" goto :indate
dmy d
set day=%errorlevel%
dmy m
set month=%errorlevel%
dmy y
set /a year=%errorlevel%+1792
goto :start
:indate
set test=/%2
set test=%test:-=/%
set test=%test:/0=/%
for /f "tokens=1-3 delims=/" %%m in ("%test%") do (
set month=%%m
set day=%%n
set year=%%o
)

:start
echo.starting from (m d y): %month% %day% %year%
pause
if "%sub:~0,1%" equ "-" (
set adj=subdate
set /a sub=0-sub
) else (
set adj=adddate
)
for /L %%a in (1 1 %sub%) do call :%adj%
if "%month:~1%"=="" set month=0%month%
if "%day:~1%"==""set day=0%day%
set ddate=%month%%dlm%%day%%dlm%%year%
echo finished: %ddate%
goto :erase

:subdate
set /a day-=1
if %day% neq 0 goto :show
set /a test=31
set /a month="(%month%+10)%%12+1"
call :%month% 2>nul
if %month% equ 12 set /a year-=1
set /a day=test
goto :show

:adddate
set /a day+=1
set /a test=31
call :%month% 2>nul
if %day% leq %test% goto :show
set /a day=1
set /a month="(%month%+12)%%12+1"
if %month% equ 1 set /a year+=1
goto :show

:4
:6
:9
:11
set /a test-=1
goto :eof
:2
set /a test=28
set /a leap="year%%4"
if %leap% equ 0 set /a test+=1
goto :eof


:show
echo.%month% %day% %year%
goto :eof

:help
echo.
echo USAGE: DATEMATH [#days] [startdate]
echo #days is either plus (no sign req'd) or minus for decrement.
echo startdate defaults to curr. system date, or specify using either
echo 1 or 2-digit elements (day,month), either - or / separator.
echo EXAMPLES: 1) DATEMATH -11 2/1/2010 2) DATEMATH 366

:erase
set test=
set leap=
set sub=
set adj=


Report •

#38
March 11, 2010 at 18:20:39
nbrane,

YIKES!


=====================================
Helping others achieve escape felicity

M2


Report •

#39
March 12, 2010 at 07:48:46
> It's not a cmd it's a BAT; LOL.
>

Indeed, works perfectly as BAT file ...
No clue why it doesn't work as CMD, but the result is rather surprising

I'll have a check, Nbrane


Report •

Ask Question