Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.
hey Sidewinderi'm tring to create a batch file that searches and replaces text in files .src and .dat.this is the code i'm using@echo onecho in progresssetlocal enabledelayedexpansionfor /f %%x in ('dir /a:-d /s /b C:\transfer\*.txt') do ( for /f "tokens=* delims=" %%i in (%%x) do ( set input=%%i set input=!input:bob=emil! set input=!input:smith=mark! set input=!input:peter=kevin! echo !input! >> %%x.chg )set oldname=%%xset newname=%%x.chgren !newname! !oldname!)endlocal i'm having a problem changing the name of the file that gets created back to original name .the file that gets created has a extension .chg and i need to loose that extention.can you helpPlease i spent 3 day on this i get nowhere.
Please i spent 3 day on this i get nowhere
@echo onecho in progresssetlocal enabledelayedexpansionfor /f "tokens=* delims=" %%x in ('dir /a:-d /s /b C:\temp\*.txt') do ( for /f "tokens=* delims=" %%i in (%%x) do ( set input=%%i set input=!input:bob=emil! set input=!input:smith=mark! set input=!input:peter=kevin! echo !input! >> "%%x.chg" ) ren "%%x" "%%~nxx.old" ren "%%x.chg" "%%~nxx")
Const ForReading = 1Const ForWriting = 2Const FileIn = "C:\transfer\test.txt"Const FileOut = "C:\transfer\test.txt" '<== Change to prevent overwrite of original fileSet objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile(FileIn, ForReading)strText = objFile.ReadAllobjFile.ClosestrNewText = Replace(strText, "bob", "emil")Set objFile = objFSO.OpenTextFile(FileOut, ForWriting)objFile.WriteLine strNewTextobjFile.Close
I tried writing VBS but with out wildcard i cant get it to open and change all files with extension .SRC and .DAT in the folder.
Const ForReading = 1Const ForWriting = 2Set objFSO = CreateObject("Scripting.FileSystemObject")Set f = objFSO.GetFolder("c:\transfer")Set fc = f.FilesFor Each fs In fc If objFSO.GetExtensionName(fs) = "SRC" Or objFSO.GetExtensionName(fs) = "DAT" Then Set objFile = objFSO.OpenTextFile(fs.Path, ForReading) strOldText = objFile.ReadAll objFile.Close strNewText = Replace(strOldText, "bob", "emil", 1, -1 ,1) 'First replace use strOldtext strNewText = Replace(strNewText, "smith", "mark", 1, -1, 1) 'Subsequent replace use strNewText strNewText = Replace(strNewText, "peter", "kevin", 1, -1, 1) Set objFile = objFSO.OpenTextFile(fs.Path, ForWriting) objFile.WriteLine strNewText objFile.Close Set objFile = objFSO.CreateTextFile(fs.Path & ".old") objFile.WriteLine strOldText objFile.Close End IfNext