1.

Solve : batch file to replace string?

Answer»

i working on a script that detect weird date in the FILE and replace them with yesterday date. here the raw data assuming yesterday date
is 2012-07-19

rawfile.txt
Code: [Select]AAAAAAAA 0001-01-01-00.00.00.000000 Change Password RSO part
BBBBBBBB 0001-01-01-00.00.00.000000 Change Password RSO part
CCCCCCC 0001-01-01-00.00.00.000000 Set Nbr try of password
DDDDDDD 2012-07-19-09.44.25.634000 Change Password

result.txt
Code: [Select]AAAAAAAA 2012-07-19-00.00.00.000000 Change Password RSO part
BBBBBBBB 2012-07-19-00.00.00.000000 Change Password RSO part
CCCCCCC 2012-07-19-00.00.00.000000 Set Nbr try of password
DDDDDDD 2012-07-19-09.44.25.634000 Change Password



I am new to dos batch file. However, COME out a basic strategy which i will first loop through each line to detect this scenario (0001-01-01) in fact there is only this scenario I then replace the date to yesterday.

i have try to write the code as follow
Code: [Select]REM Code to Find YEsterday's date
set yyyy=

set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
set %%x=%%u
set %%y=%%v
set %%z=%%w
set $d1=
set $tok=))

if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100

set CurDate=%mm%/%dd%/%yyyy%
set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY
if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31
set /A dd=31 + %dd%
goto CHKDAY

:SET30
set /A dd=30 + %dd%
goto CHKDAY

:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29

:SET28
set /A dd=28 + %dd%
goto CHKDAY

:SET29
set /A dd=29 + %dd%
goto CHKDAY

CALL :process <..\input.txt>> ..\output.txt

:process
set line=
if "!line:~12,22!" neq "0001-01-01" goto process
## for /F between line:~12,22 do (
------------------------------------------------
this part onward i not very sure already.
intend to do a for loop each line replace "0001-01-01" to %yyyy%-%mm%-%dd%


)


i wonder how to continue the code to make it work.. would appreciate if anyone can help!!This works here with rawfile.txt

Code: [Select]@echo off
setlocal EnableExtensions EnableDelayedExpansion
call :yesterday today -1
for /f "delims=" %%a in (rawfile.txt) do (
set "val=%%a"
if "!val:~12,10!"=="0001-01-01" (
>> newfile.txt echo !val:~0,12!%day%!val:~22!
) else (
>> newfile.txt echo %%a
)
)
pause
GOTO:EOF
:yesterday
:: Date foward & backward
@echo off
:: from code by Phil Robyn
setlocal
if [%1]==[] (
echo to get todays date use
echo call "%~n0" today 0
echo.
echo to get yesterdays date use
echo call "%~n0" today -1
echo.
echo to get the date 25 days ago:
echo call "%~n0" today -25
echo.
echo to get the date 1250 days in the future
echo call "%~n0" today +1250
goto :EOF)

set date1=%1
set qty=%2
if /i "%date1%" EQU "TODAY" (
set date1=now
) else (
set date1="%date1%"
)
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
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)
for /f %%a in (
'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set day=%result:~0,4%-%result:~4,2%-%result:~6,2%
:: echo %%day%% is set to "%day%" (without the quotes)HI Foxidrive,

thank you for your help. the script work nice.!

Btw, i want to loop through the folder for each file inside folder do the change date code.

so i try to do this but failed.

Code: [Select]cd ..\input ##(to the folder that has all the data)



for %%f in (*.txt) do (

echo "%%~nf"
"D:\something\deployment code\change_date" .\"%%~nf".txt
)

Code: [Select]@echo off
setlocal EnableExtensions EnableDelayedExpansion
call :yesterday today -1
for /f "delims=" %%a in (%1) do ( ## (which i try to pass the file from the folder to this line, i thought is to loop through.. )
set "val=%%a"
if "!val:~12,10!"=="0001-01-01" (
>> newfile.txt echo !val:~0,12!%day%!val:~22!
) else (
>> newfile.txt echo %%a
)
)
however, it doesnt work. would you be able to enlighten me,
thanks!!!hi,

thanks,.
i figure it out to do already

Code: [Select]@echo off
setlocal EnableExtensions EnableDelayedExpansion
call :yesterday today -1
for /f "delims=" %%a in ('type %1') do (
set "val=%%a"
if "!val:~12,10!"=="0001-01-01" (
>> ..\temp1\%1_date.txt echo !val:~0,12!%day%!val:~22!
) else (
>> ..\temp1\%1_date.txt echo %%a
)
)



Discussion

No Comment Found