I have created a batch file which retrieves T-1
data from the database. But I want to retrieve data of backdated data of date which user enter through cmd.
Can you please help me on this?
I want to retrieve data of particular date If user provide? Otherwise, it retrieves T-1 date which is currently fetching:
set qty=-1
:loop4weekends
set "separator1=-"
set "separator2=_"
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,now)
echo>>"%temp%\%~n0.vbs" d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs" right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs" right(100+day(s),2)^&_
echo>>"%temp%\%~n0.vbs" d
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "YY=%result:~0,4%" & set "MM=%result:~4,2%" & set "DD=%result:~6,2%" & set "daynum=%result:~-1%"
:: if the daynum is a weekend then loop to get the friday
set "weekend="
if %daynum% EQU 1 set weekend=1&set "qty=-3"
if %daynum% EQU 7 set weekend=1&set "qty=-2"
if defined weekend goto :loop4weekends
set "day=%YY%%separator1%%MM%%separator1%%DD%"
set "day1=%YY%%separator2%%MM%%separator2%%DD%"
sqlcmd -U %SQLServerUserName% -P %SQLServerPassword% -S %MachineIP% -Q "exec P_SP @companyFundIDs=N'',@inputDate=N''" -d %ClientDB% -o "Fil_Trade_%day1%.csv" -s"," -W
ECHO CSV file has been generated successfully
pause
You want an default value, unless the user enters something different. That's quite easy, because set /p
leaves the variable unchanged, when user just presses ENTER. So just define a default value before asking the user:
set "result=defaultValue"
set /p "result=Enter date ([ENTER] for default "%result%"): "
echo %result%
You should add a bit of code to verify, if the string the user entered is a valid date. Here is an example.