I'm writing a batch file to copy some data, the issue I have is, I want to capture the date in a variable so that I can use it as the file name. I would like it in the format of dd-mm-yyyy.
I found this example here, but I was unable to get it into the format I need.
this solution is independent of locale settings:
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
It will give you (independent of locale settings!):
20130802203023.304000+120
( YYYYMMDDhhmmss.<fraction>+/-<timedifference to UTC> )
from here, it is easy:
set mydate=%datetime:~6,2%-%datetime:~4,2%-%datetime:~0,4%
echo %mydate%
Solution
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
set newdate=%dd%%mm%%yy%
echo %newdate%
:date
set dateyear=%date:~-4%
if %date:~-7,-5%==01 set datemonth=January
if %date:~-7,-5%==02 set datemonth=February
if %date:~-7,-5%==03 set datemonth=march
if %date:~-7,-5%==04 set datemonth=April
if %date:~-7,-5%==05 set datemonth=may
if %date:~-7,-5%==06 set datemonth=June
if %date:~-7,-5%==07 set datemonth=July
if %date:~-7,-5%==08 set datemonth=August
if %date:~-7,-5%==09 set datemonth=September
if %date:~-7,-5%==10 set datemonth=October
if %date:~-7,-5%==11 set datemonth=November
if %date:~-7,-5%==12 set datemonth=December
set dateday=%date:~-10,-8%
echo It is %dateday% %datemonth% %dateyear%, sir.
exit /B 0
:xpdate
echo It is %date%, sir.
%speech% "It is %date%, sir."
exit /B 0
:w7dat
if %time:~0,-9% GTR 12 (set /a timehour=%time:~0,-9%-12) else set timehour=%time:~0,-9%
if %time:~3,1%==0 (set timeminute=%time:~4,1%) else set timeminute=%time:~3,2%
set dateyear=%date:~-4%
if %date:~-7,-5%==01 set datemonth=January
if %date:~-7,-5%==02 set datemonth=February
if %date:~-7,-5%==03 set datemonth=march
if %date:~-7,-5%==04 set datemonth=April
if %date:~-7,-5%==05 set datemonth=may
if %date:~-7,-5%==06 set datemonth=June
if %date:~-7,-5%==07 set datemonth=July
if %date:~-7,-5%==08 set datemonth=August
if %date:~-7,-5%==09 set datemonth=September
if %date:~-7,-5%==10 set datemonth=October
if %date:~-7,-5%==11 set datemonth=November
if %date:~-7,-5%==12 set datemonth=December
set dateday=%date:~-10,-8%
echo It is currently %timeminute% minutes past %timehour% on %dateday% %datemonth% %dateyear%, sir.
exit /B 0
This is what i use in my batch AI just change the :date :w7dat and all those to what you need
Here is a solution using PowerShell, which is pre-installed on nearly all versions of Windows and is far more powerful than batch.
PowerShell code
Get-Date -Format 'yyyy-MM-dd'
Calling from a batch file
for /f %%a in ('powershell -nop -c "Get-Date -Format 'yyyy-MM-dd'"') do set newdate=%%a
Related
I'm trying to deploy my lambdas (.NET Core 2.0) into AWS by using AWS CLI + TeamCity + Powershell script. I have some steps , including deploy step and applying new aliase number step.
While deploy step I don't get alias number (lambda version). A script returns me something like "function = ", but it should be like "result = 44 (or any other lambda version)".
Here's a script (mix of Powershell + AWS CLI commands).
FOR ( /F "tokens=* delims=" %%%A IN ('dotnet lambda deploy-function MyLambda --profile default --region my-server-region --configuration Release --function-publish true') DO ( set result=%%%A )){
FOR ( /F "tokens=1,2,3,4,5,6 delims= " %%%A IN ('echo %%%result%%%') DO ( set result=%%%F )){
#echo on
echo %env.MyLambdaVersion%
#echo off
echo "##teamcity[setParameter name='env.MyLambdaVersion' value='%%result%%']"
}
}
I actually don't know what I do wrong, actually this script was written before I started to work on this project. Any advice or any link will be helpfull, thanks :)
EDIT
I expect this kind of respinse, where i can get MyLambda version
Succesfull build scrinshot1: https://i.stack.imgur.com/bOIGr.jpg
But instead of it I'm getting this result.
Invalid response
EDIT 2
Output of the script in text format
Step 17/36: Deploying new version of Lambda "MyLambda" (Command Line) (3m:16s)
[13:27:03][Step 17/36] Starting: C:\BuildAgent\temp\agentTmp\custom_script1964828788270486366.cmd
[13:27:03][Step 17/36] in directory: C:\BuildAgent\work\3f3a1ea85ff2ff09
[13:30:10][Step 17/36]
[13:30:11][Step 17/36] C:\BuildAgent\work\3f3a1ea85ff2ff09\Proj\Lambda\Proj.Lambda.MyLambda>echo
[13:30:12][Step 17/36] ECHO is on.
[13:30:16][Step 17/36] "
[13:30:16][Step 17/36] ##teamcity[setParameter name='env.MyLambdaVersion' value='function: ']
[13:30:16][Step 17/36] "
[13:30:20][Step 17/36] Process exited with code 0
I've run sbt clean stage and am trying to run my application in prod mode using the generated script. When running target\universal\stage\bin\app-name.bat . -Dconfig.resource=application.prod.conf it complains that the application secret is not set. After some troubleshooting I realised that it was still loading the default config filebecause after deleting target\universal\stage\conf\application.conf I get the following error:
Oops, cannot start the server.
Configuration error: Configuration error[application: application.conf: java.io.IOException: resource not found on classpath: application.conf, application.json: java.io.IOException: resource not found on classpath: application.json, application.properties: java.io.IOException: resource not found on classpath: application.properties]
I've tried refering to it with the full path using -Dconfig.file, with the same result. How do I get it to load the right config file, other than renaming it to application.conf?
Here is the full command contained in the .bat file, autogenerated by sbt:
#REM game-check-match launcher script
#REM
#REM Environment:
#REM JAVA_HOME - location of a JDK home dir (optional if java on path)
#REM CFG_OPTS - JVM options (optional)
#REM Configuration:
#REM GAME_CHECK_MATCH_config.txt found in the GAME_CHECK_MATCH_HOME.
#setlocal enabledelayedexpansion
#echo off
if "%GAME_CHECK_MATCH_HOME%"=="" set "GAME_CHECK_MATCH_HOME=%~dp0\\.."
set "APP_LIB_DIR=%GAME_CHECK_MATCH_HOME%\lib\"
rem Detect if we were double clicked, although theoretically A user could
rem manually run cmd /c
for %%x in (!cmdcmdline!) do if %%~x==/c set DOUBLECLICKED=1
rem FIRST we load the config file of extra options.
set "CFG_FILE=%GAME_CHECK_MATCH_HOME%\GAME_CHECK_MATCH_config.txt"
set CFG_OPTS=
if exist "%CFG_FILE%" (
FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%CFG_FILE%") DO (
set DO_NOT_REUSE_ME=%%i
rem ZOMG (Part #2) WE use !! here to delay the expansion of
rem CFG_OPTS, otherwise it remains "" for this loop.
set CFG_OPTS=!CFG_OPTS! !DO_NOT_REUSE_ME!
)
)
rem We use the value of the JAVACMD environment variable if defined
set _JAVACMD=%JAVACMD%
if "%_JAVACMD%"=="" (
if not "%JAVA_HOME%"=="" (
if exist "%JAVA_HOME%\bin\java.exe" set "_JAVACMD=%JAVA_HOME%\bin\java.exe"
)
)
if "%_JAVACMD%"=="" set _JAVACMD=java
rem Detect if this java is ok to use.
for /F %%j in ('"%_JAVACMD%" -version 2^>^&1') do (
if %%~j==java set JAVAINSTALLED=1
if %%~j==openjdk set JAVAINSTALLED=1
)
rem BAT has no logical or, so we do it OLD SCHOOL! Oppan Redmond Style
set JAVAOK=true
if not defined JAVAINSTALLED set JAVAOK=false
if "%JAVAOK%"=="false" (
echo.
echo A Java JDK is not installed or can't be found.
if not "%JAVA_HOME%"=="" (
echo JAVA_HOME = "%JAVA_HOME%"
)
echo.
echo Please go to
echo http://www.oracle.com/technetwork/java/javase/downloads/index.html
echo and download a valid Java JDK and install before running game-check-match.
echo.
echo If you think this message is in error, please check
echo your environment variables to see if "java.exe" and "javac.exe" are
echo available via JAVA_HOME or PATH.
echo.
if defined DOUBLECLICKED pause
exit /B 1
)
rem We use the value of the JAVA_OPTS environment variable if defined, rather than the config.
set _JAVA_OPTS=%JAVA_OPTS%
if "!_JAVA_OPTS!"=="" set _JAVA_OPTS=!CFG_OPTS!
rem We keep in _JAVA_PARAMS all -J-prefixed and -D-prefixed arguments
rem "-J" is stripped, "-D" is left as is, and everything is appended to JAVA_OPTS
set _JAVA_PARAMS=
set _APP_ARGS=
:param_loop
call set _PARAM1=%%1
set "_TEST_PARAM=%~1"
if ["!_PARAM1!"]==[""] goto param_afterloop
rem ignore arguments that do not start with '-'
if "%_TEST_PARAM:~0,1%"=="-" goto param_java_check
set _APP_ARGS=!_APP_ARGS! !_PARAM1!
shift
goto param_loop
:param_java_check
if "!_TEST_PARAM:~0,2!"=="-J" (
rem strip -J prefix
set _JAVA_PARAMS=!_JAVA_PARAMS! !_TEST_PARAM:~2!
shift
goto param_loop
)
if "!_TEST_PARAM:~0,2!"=="-D" (
rem test if this was double-quoted property "-Dprop=42"
for /F "delims== tokens=1,*" %%G in ("!_TEST_PARAM!") DO (
if not ["%%H"] == [""] (
set _JAVA_PARAMS=!_JAVA_PARAMS! !_PARAM1!
) else if [%2] neq [] (
rem it was a normal property: -Dprop=42 or -Drop="42"
call set _PARAM1=%%1=%%2
set _JAVA_PARAMS=!_JAVA_PARAMS! !_PARAM1!
shift
)
)
) else (
if "!_TEST_PARAM!"=="-main" (
call set CUSTOM_MAIN_CLASS=%%2
shift
) else (
set _APP_ARGS=!_APP_ARGS! !_PARAM1!
)
)
shift
goto param_loop
:param_afterloop
set _JAVA_OPTS=!_JAVA_OPTS! !_JAVA_PARAMS!
:run
set "APP_CLASSPATH="
set "APP_MAIN_CLASS=-jar %APP_LIB_DIR%\game-check-match.game-check-match-0.1.0-launcher.jar"
if defined CUSTOM_MAIN_CLASS (
set MAIN_CLASS=!CUSTOM_MAIN_CLASS!
) else (
set MAIN_CLASS=!APP_MAIN_CLASS!
)
rem Call the application and pass all arguments unchanged.
"%_JAVACMD%" !_JAVA_OPTS! !GAME_CHECK_MATCH_OPTS! -cp "%APP_CLASSPATH%" %MAIN_CLASS% !_APP_ARGS!
#endlocal
:end
exit /B %ERRORLEVEL%
Can someone let me know how (or point me to right direction) I can create an application pool and set an application to use the newly created pool using commandline? Application has already been added to IIS but need to create a new pool and use it for this new application.
This is what I gathered, in case anyone else is looking for the same thing:
rem // Creates a new application pool called MyAppPool
CSCRIPT %SYSTEMDRIVE%\Inetpub\AdminScripts\ADSUTIL.VBS CREATE w3svc/AppPools/MyAppPool IIsApplicationPool
rem // if running under different user name and password
rem CSCRIPT %SYSTEMDRIVE%\Inetpub\AdminScripts\ADSUTIL.VBS SET w3svc/AppPools/MyAppPool/WamUserName "domain\username"
rem CSCRIPT %SYSTEMDRIVE%\Inetpub\AdminScripts\ADSUTIL.VBS SET w3svc/AppPools/MyAppPool/WamUserPass "pass"
rem CSCRIPT %SYSTEMDRIVE%\Inetpub\AdminScripts\ADSUTIL.VBS SET w3svc/AppPools/MyAppPool/AppPoolIdentityType 3
========================================
rem // create a new virtual directory (creates vdir "Test" and set it to D:\WebSites\MyWebSite
CSCRIPT %SystemRoot%\system32\iisvdir.vbs /Create W3SVC/1/ROOT Test D:\WebSites\MyWebSite
=================================
rem // assign MyAppPool to "Test" web site
CSCRIPT %SYSTEMDRIVE%\Inetpub\AdminScripts\ADSUTIL.VBS SET W3SVC/1/ROOT/Test/AppPoolId MyAppPool
I have the following few lines in a batch file:
#echo off
echo Running dump...
"D:\wamp\bin\mysql\mysql5.5.16\bin\mysqldump" --host="localhost" --user="****" --password="****" database> "D:\wamp\backup\mysql\"back.%date:~0,2%.sql
echo Done!
Supposedly, it should create a backup file with today's day as 01, 02, 03, ..., 31, that is,
back.01.sql
back.02.sql
back.03.sql
When I run it from CMD, it actually creates it as expected, but when it is run from the scheduler it looks like:
back.Mo.sql
back.Tu.sql
back.We.sql
What date format should I use to ensure it actually created with number of the day of the month?
Working with dates and times in Windows batch is a pain.
The most robust solution is to use WMIC to get the local date and time. It returns a value in the following format:
YYYYMMDDhhmmss.dddddd-ttt
YYYY = year
MM = month
DD = day
hh = hour in 24 hour format
mm = minutes
ss = seconds
dddddd = fractional seconds
ttt = time zone (minutes difference from Greenwich Mean Time)
So you can use the following to get the current day of the month in a variable
Edit - corrected starting substring location from 7 to 6
set curDate=
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined curDate set curDate=%%x
set day=%curDate:~6,2%
The big advantage of this technique is it will work on any Windows machine in the world, regardless of the locale settings.
Here is your code with the technique inserted
Edit - corrected starting substring location from 7 to 6
#echo off
echo Running dump...
set curDate=
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined curDate set curDate=%%x
set "day=%curDate:~6,2%"
"D:\wamp\bin\mysql\mysql5.5.16\bin\mysqldump" --host="localhost" --user="****" --password="****" database> "D:\wamp\backup\mysql\back.%day%.sql"
echo Done!
Just setup regional settings dd.MM.yyyy for the account under which your task is run. (use control panel or registry HKCU\Control Panel\International\sShortDate for Windows XP at least.
I would like to create an environment variable to hold a filename something like:
PREFIX-2010-AUG-09.zip
I can get close if I use something like this:
SET filename=PREFIX-%date:~-4,4%-%date:~-7,2%-%date:~0,2%.zip
Result:
PREFIX-2010-08-09.zip
but in this case, I get the month as two digits (08).
Is there any easy trick in Windows batch files to get the three-letter month abbreviation from the numeric month (e.g. 08 for "AUG" = August) ??
Update: this needs to be run on a Windows 2008 R2 Server, and yes, if someone can show me a PowerShell solution, that would work, too :-) Thanks!
This is something like a look up table:
set month_01=JAN
set month_02=FEB
set month_03=MAR
#rem ...
set number=02
for %%a in (month_%number%) do call set month_as_text=%%%%a%%
echo %month_as_text%
The value in %number% in the for loop is used to dereference the matching variable name.
Or even shorter:
set number=02
for /f "tokens=%number%" %%m in ("JAN FEB MAR APR ...") do set month_as_text=%m
echo %month_as_text%
EDIT:
Johannes suggests a shorthand for the 1st version:
set month_01=JAN
set month_02=FEB
set month_03=MAR
#rem ...
set number=02
setlocal enabledelayedexpansion
set month_as_text=!month_%number%!
echo %month_as_text%
You could always do the number-to-text translation by hand, like:
if %MM%==01 set MM=Jan
if %MM%==02 set MM=Feb
if %MM%==03 set MM=Mar
if %MM%==04 set MM=Apr
etc.
The first answer is incorrect for August and September. The reason is that the shell interprets a leading zero as octal, and 08 and 09 are not valid octal numbers. You can see this easily for yourself by running this command:
for /f "tokens=08" %a in ("A B C D E F G H I J") do echo %a
If you replace the 08 above with 09 it will also fail. It will work if you use 07 or 10. A working implementation is as follows:
months=JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
:: get the month as a 2-digit number
set number=%date:~4,2%
:: remove leading zero
for /f "tokens=* delims=0" %%a in ("%number%") do set number=%%a
:: index into array to get month name
for /f "tokens=%number%" %%m in ("%months%") do set month_as_text=%%m
:: print month name
echo %month_as_text%