Reputation: 33
trying to replace 11/22/2016 to 11222016. for this I have tried the following and is working as expected.
set string=dd/mm/yyyy.
set find=/
set replace=
call set string=%%string:!find!=!replace!%%
echo %string%
output is ddmmyyyy
but when I tried this in the if condition this is not working as expected.
setlocal enabledelayedexpansion
set isDateFound=false
set string=dd/mm/yyyy.
if "%isDateFound%" == "false" (
echo %isDateFound%
set find=/
set replace=
call set string=%%string:!find!=!replace!%%
echo %string%
)
output dd/mm/yyyy
looks like the delayedexpressions is playing some role here. but I am not able to overcome this. How replace substring in side the if condition.
Upvotes: 2
Views: 326
Reputation:
You are using delayed expansion in your set call so in this case you need to access the value via !string! not %string%, so:
setlocal enabledelayedexpansion
set isDateFound=false
set string=dd/mm/yyyy.
if "%isDateFound%" == "false" (
echo %isDateFound%
set find=/
set replace=
call set string=%%string:!find!=!replace!%%
echo !string!
)
note echo !string! instead of echo %string%
Upvotes: 1
Reputation: 9545
Here is a better way to do that :
@echo off
FOR /F "tokens=1-3" %%a IN ('WMIC Path Win32_LocalTime Get Day^,Month^,Year ^| findstr [0-9]') DO (
set "$Day=0%%a"
set "$Month=0%%b"
set "$Year=%%c"
)
echo %$Year%%$Month:~-2%%$Day:~-2%
The leading 0 before before Month and Day is automatically added.
Upvotes: 2