Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: Search and Replace
To Download a zip file with all of E.R.'s examples
;***************************************************************************** ; SEARCH AND REPLACE UTILITY ; Written by E.Tippelt 100115.3301@compuserve.com ; Will do a search and replace for all instances of a specified file in a ; directory tree. The "File to be replaced" is deleted before the "Replacement ; file" is copied, so the replacement file can have a different filename to the ; original file. ;***************************************************************************** :TU1 sounds(0) overwrite=0 :CDTS1 SourceRoot=askline("GET STARTING POINT","Please specify starting directory","") if !DirExist(SourceRoot) Message("ENTRY ERROR","Starting Directory does not exist, Please re-enter") goto CDTS1 endif targetFile=askline("NAME OF FILE TO BE REPLACED","Please specify target file name","") :CDTS2 ReplacementFile=askline("NAME OF REPLACEMENT FILE","Please specify replacement filename and full path","") if !FileExist(ReplacementFile) Message("ENTRY ERROR","Replacement File does not exist, Please re-enter") goto CDTS2 endif if AskYesNo("Overwrite Options","Do you want to overwrite without prompting")==@yes then overwrite=1 ;got the entry parameters, now get the directory list level=0 treelist="" treelist%level%=SourceRoot :TU5 if treelist%level%=="" goto TU10 ;Got the directory list, now do something with it! else gosub TU50 endif goto TU5 :TU10 ;The variable TREELIST now contains a space delimited, fully pathed, list of ;all the subdirectories, using the source root directory as the starting point. ;Message("Treelist",treelist) ;This bit of code will save the directory list to a file, as it will be too ;long to display in a message box in most cases ;handle = fileopen("c:\dirlist.txt","write") ;filewrite(handle,treelist) ;fileclose(handle) dr=itemcount(treelist," ") ;message("Number of Directories",dr) SourceDir=SourceRoot ;do the root directory first gosub dsm5 ;do the directory copy ;Now make sure that SourceRoot has a backslash on the end ;to cater for the situation where the source is something like c: If StrSub(SourceRoot,StrLen(SourceRoot),1)!= "\" Then SourceRoot=StrCat(SourceRoot,"\") for k = 2 to dr SourceDir=itemextract(k,treelist," ") gosub dsm5 ;do the directory copy next k Return ;Go back to calling program ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;SUBROUTINES ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;***************************************************************************** ;GET DIRECTORIES AT A GIVEN LEVEL IN THE TREE ;***************************************************************************** :TU50 treelist=StrCat(treelist," ",treelist%level%) treelist=strtrim(treelist) ;This will be the space delimited list of all the ;directories in the tree workinglevel=treelist%level% treeno=ItemCount(workinglevel," ") ;get the number of entries in the current tree level level=level+1 treelist%level%="" ;get ready to generate the next level of the tree for i=1 to treeno srcdir=itemextract(i,workinglevel," ") gosub TU60 if PathedDirList!="" then treelist%level%=strcat(treelist%level%," ",PathedDirList) next i treelist%level%=strtrim(treelist%level%) return ;***************************************************************************** ;GET SUBDIRECTORIES AT A GIVEN LEVEL IN THE TREE ;***************************************************************************** :TU60 ;Entry parameters: ;SrcDir ;level slash="" length=strlen(SrcDir) Last=strsub(SrcDir,length,1) if last!="\" then slash="\" DirChange(SrcDir) DirList=DirItemize("*.*") DirNo=ItemCount(Dirlist," ") ;Count the entries in DirList PathedDirList="" if DirNo==0 then return for j=1 to DirNo Dir=ItemExtract(j,DirList," ") ;Get each entry in the directory list Dir=StrCat(SrcDir,slash,Dir) PathedDirList=strcat(PathedDirList," ",Dir) next j PathedDirList=strtrim(PathedDirList) ;Fully pathed list of subdirectories return ;at a given level in the tree ;Becomes the treelist%level% for the next level ;**************************************************************************** ; File Overwrite Module ; Written by E.Tippelt 100115.3301@compuserve.com ;**************************************************************************** :DSM5 ;Start Here OrigDir=DirGet() ;Save current Directory :DSM10 Dirchange("%Sourcedir%") if FileExist("%Sourcedir%\%targetFile%")==@true ErrorMode(@OFF) FileAttrSet("%targetFile%","rAsh") ErrorMode(@CANCEL) if overwrite==1 filedelete("%targetfile%") filecopy("%ReplacementFile%","%sourcedir%",@false) else if askyesno("REPLACE FILE: ?","%sourcedir%\%targetfile%")==@NO then goto DSM20 filedelete("%targetfile%") filecopy("%ReplacementFile%","%sourcedir%",@false) endif endif :DSM20 DirChange("%OrigDir%") ;Restore current Directory RETURN
Article ID: W13795
Filename: Search and Replace Utililty.txt
File Created: 2017:08:29:11:19:24
Last Updated: 2017:08:29:11:19:24