Question

I have the below task and I am wondering what would be the best and quick way to do this. I am thinking scripting rather than a C# app but struggling with scripting in DOS. I wanted to use powershell but not sure if all the machines have powershell installed :

I have around 15 windows machines and in each machine, I want to find all *.config files (in say C:\ and D:) and replace a specific word (db name).

I can execute this locally or execute this remotely, since I have login access to the machines. But the tough part is the script.

Any pointers would be great to start.

Thanks.

Was it helpful?

Solution 2

This is untested and destructive so test it first.- it is designed to search C: and D: drives and all subdirectories and modifies all *.config files.

It is set to look in c:\abc and d:\abc trees atm so put some files and folders in there and test it.

@echo off
for /f "delims=" %%a in ('dir c:\abc\*.config d:\abc\*.config /b /s /a-d') do (
call :sar "%%a" "%%a.tmp" "db_name" "new_db_name"
move /y "%%a.tmp" "%%a" >nul
)
goto :EOF
:sar
:: inputfile outputfile regexp_search replacement
if "%~5"=="" (set global=true) else (set global=false)
set s=regex.replace(wscript.stdin.readall,"%~4")
 >_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True
>>_.vbs echo regex.pattern="%~3"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"%~1" >"%~2"
del _.vbs
goto :EOF

OTHER TIPS

Wikipedia has some info on what versions of Windows have PowerShell.

PowerShell version 1.0 was released in 2006 for Windows XP SP2/SP3, Windows Server 2003, and Windows Vista....Version 2.0 is integrated with Windows 7 and Windows Server 2008 R2 and is released for Windows XP with Service Pack 3, Windows Server 2003 with Service Pack 2, and Windows Vista with Service Pack 1. Version 3.0 is integrated with Windows 8 and with Windows Server 2012. Microsoft has also made PowerShell 3 available for Windows 7 with Service Pack 1, for Windows Server 2008 with Service Pack 1, and for Windows Server 2008 R2 with Service Pack 1.

To install version 3 on Win 7 you need the WMF 3.0 package

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top