cmdSubstringsOfvariablesLikeDate
TOC
Example 1: Using DOS DATE for year
Example 2: Using DOS DATE for month
Example 3: Using substring of user defined variable.
Warning: When using DATE variables it is affected by your system so different strings can show different results.
Example 1: (Taking more or less parts of the DATE variable)
cmd file:
@echo off
echo DATE = %DATE%
echo set year=DATE:~-4
set year=%DATE:~-4%
echo year is %year%
echo set year=DATE:~-2
set year=%DATE:~-2%
echo year is %year%
echo set year=DATE:~-5
set year=%DATE:~-5%
echo year is %year%
echo set year=DATE:~-10
set year=%DATE:~-10%
echo year is %year%
echo done
Output:
C:\Test>test
DATE = Tue 04/27/2010
set year=DATE:~-4
year is 2010
set year=DATE:~-2
year is 10
set year=DATE:~-5
year is /2010
set year=DATE:~-10
year is 04/27/2010
done
Example 2:
cmd file:
@echo off
echo DATE = %DATE%
echo set year=DATE:~-10,2
set month=%DATE:~-10,2%
echo month is %month%
echo done
Output:
C:\Test>test
DATE = Tue 04/27/2010
set year=DATE:~-10,2
month is 04
done
Example 3: (Your own defined variable)
cmd file:
@echo off
set myVariable=ABCDE
echo myVariable = %myVariable%
echo set year=myVariable:~-4
set year=%myVariable:~-4%
echo year is %year%
echo set year=myVariable:~-2
set year=%myVariable:~-2%
echo year is %year%
echo set year=myVariable:~-5
set year=%myVariable:~-5%
echo year is %year%
echo done
Output:
C:\Test>test
myVariable = ABCDE
set year=myVariable:~-4
year is BCDE
set year=myVariable:~-2
year is DE
set year=myVariable:~-5
year is ABCDE
done
No comments:
Post a Comment