|
Answer» Alright, so I'm trying to make a batch file that finds a string in a .INI file and replaces it with another. The string in the .INI file looks like this: "DOB=06 03 2007" (but without quotation marks)
I've created the part of the batch file that gets the current date of business from the timestamp and places it into a variable named DOB with the same format as the string above. I just need to a.) Find the string in the .INI file b.) replace the above string in the .INI file with the DOB variable.
Bottom line, this will basically updates the date of business to the current date on the system. Can anyone help me with this? Thanks.
Maybe to help out, here's what I have so far: ------------------------------------------------------------------- set Year=%date:~-4,4% set Day=%date:~-7,2% set Month=%date:~-10,2% Set DOB=DOB=%Month% %Day% %Year%
C: For /f "delims=" %%a in ('FindStr /C:"DOB=" C:\Temp\File.INI') do ( Echo %%a Pause ) Goto :EOF -------------------------------------------------------------------
This will echo the line "DOB=06 03 2007" (without Quotes) currently. So, I've found the line and have been able to capture it completely, but I still can't seem to replace it with the current date or the DOB variable.So, if your file read
apples pears bananas DOB=06 03 2007 cheese ham eggs
To me, that date says 6th March 2007, but hey, i'm European, I'll assume you're using US date format
so if you processed it today 5th June
you'd want it to read, afterwards...
apples pears bananas DOB=06 05 2007 cheese ham eggs
Is that right?
That is exactly correct. Everytime I run this batch file, or fire it off, it'll basically overwrite that string with the correct date. Thanks.This uses your date code, so should be good for your locale. Suggest you test it on dummy data before letting it LOOSE on your real stuff!!!! cut out not needed stuff to suit...
I used the %temp% system variable because I don't use C:\TEMP, so you may have to *censored* around with that...
Code: [Select]@echo off setlocal enableextensions setlocal enabledelayedexpansion
REM set up test file echo Purchase Order # : 123456 > "%temp%\file.ini" echo Filing code : ZQ >> "%temp%\file.ini" echo Item Name : Reverse Action Flange Clamp >> "%temp%\file.ini" echo Item Price : $45.50 >> "%temp%\file.ini" echo DOB=01 01 2000 >> "%temp%\file.ini" echo Customer name : Joe Smith >> "%temp%\file.ini" echo Customer address 1 : 123 Main St >> "%temp%\file.ini" echo Customer address 2 : Hackensack NJ >> "%temp%\file.ini" echo Customer address 3 : 123456789 >> "%temp%\file.ini" echo Customer phone : (200) 123-45678 >> "%temp%\file.ini"
set Year=%date:~-4,4% set Day=%date:~-7,2% set Month=%date:~-10,2% Set DOB=%Month% %Day% %Year% set newline=DOB=%DOB%
REM show test file before processing echo. & echo Before... & echo. & type "%temp%\file.ini" & echo.
REM delete output file if it exists if exist "%temp%\file.ini.new" del "%temp%\file.ini.new"
REM for each line in input file for /F "delims=" %%L in (%temp%\file.ini) do (
REM copy input line to output line set outputline=%%L
REM EXCEPT if input line contains DOB= REM in which case replace with new line REM Meaning of && operator is that statement to right of && REM is only executed if operation to left of && REM is successful. Opposite is || operator (execute if NOT sucessful) REM sending echo to nul to avoid screen clutter echo %%L | findstr "DOB=">nul && set outputline=%newline%
REM WRITE output line to output file REM need delayed expansion as we are in a FOR loop echo !outputline! >> "%temp%\file.ini.new" )
REM delete input file del "%temp%\file.ini"
REM replace input file with output file copy "%temp%\file.ini.new" "%temp%\file.ini">nul del "%temp%\file.ini.new">nul
REM show altered file after processing echo After... & echo. & type "%temp%\file.ini" & echo.
Code: [Select]Before...
Purchase Order # : 123456 Filing code : ZQ Item Name : Reverse Action Flange Clamp Item Price : $45.50 DOB=01 01 2000 Customer name : Joe Smith Customer address 1 : 123 Main St Customer address 2 : Hackensack NJ Customer address 3 : 123456789 Customer phone : (200) 123-45678
After...
Purchase Order # : 123456 Filing code : ZQ Item Name : Reverse Action Flange Clamp Item Price : $45.50 DOB=06 06 2007 Customer name : Joe Smith Customer address 1 : 123 Main St Customer address 2 : Hackensack NJ Customer address 3 : 123456789 Customer phone : (200) 123-45678
here's a vbscript. not the BEST, but does what you want. Code: [Select]Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile ("c:\temp\test.txt", ForReading) TheDate=Date theYear = DatePart("yyyy", TheDate) theMonth= DatePart("m",TheDate) theDay = DatePart("d",TheDate) If theMonth < 10 Then theMonth = "0" & theMonth End If If theDay < 10 Then theDay = "0" & theDay End If Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.ReadLine If InStr(strNextLine,"DOB") Then dob=split(strNextLine,"=",-1) Wscript.Echo dob(0) & "=" & theMonth & " " & theDay & " " & theYear Else WScript.Echo strNextLine End If Loop Hey Contrex...
Just so I get this correct, I'm basically recreating the file from scratch and when the system finds that one file, it basically puts in the correct date into that line, right? Thanks for all your help.Not sure what you are asking here. The batch file creates a temporary copy of the original file, except that the line starting eg DOB=01 01 1999 will be altered so that the date part is today's date according to your system's date format convention (mm dd yyyy). Then the original file is deleted and the new one copied into its place.
The batch file will read a file called file.ini located in your system temp folder. You can alter this behaviour by editing the batch file as desired. Your system temp folder is held in the system variable %temp%.
Here is a short version with demo and comments deleted. You need to edit it to suit your needs.
Quote @echo off setlocal enableextensions setlocal enabledelayedexpansion set Year=%date:~-4,4% set Day=%date:~-7,2% set Month=%date:~-10,2% Set today=%Month% %Day% %Year% set newline=DOB=%today% if exist "%temp%\file.ini.new" del "%temp%\file.ini.new" for /F "delims=" %%L in (%temp%\file.ini) do ( set outputline=%%L echo %%L | findstr "DOB=">nul && set outputline=%newline% echo !outputline! >> "%temp%\file.ini.new" ) del "%temp%\file.ini" copy "%temp%\file.ini.new" "%temp%\file.ini">nul del "%temp%\file.ini.new">nul
|