[英]Edit files on folder whitelist in Windows environment
I have a folder with ~4000 folders inside of it. 我有一个文件夹,里面有〜4000个文件夹。 In each of these folders is a .ini file that I wish to edit.
在这些文件夹中的每个文件夹中都有一个我要编辑的.ini文件。 Inside of the .ini file, I want to search for a string and replace it with another string.
在.ini文件中,我想搜索一个字符串并将其替换为另一个字符串。
The caveat is that I have a specific list of folders, a whitelist if you will. 需要注意的是,我有一个特定的文件夹列表,如果可以的话,会列入白名单。 I only want to change the .ini file inside of the folders I specify in my whitelist and not the rest of the folders.
我只想更改在白名单中指定的文件夹内的.ini文件,而不要更改其余文件夹。
My platform is Windows. 我的平台是Windows。
Example Folder Structure: 文件夹结构示例:
Parent Folder
Folder1
file.ini
Variable1=TOMATO
^^^^^^^^^^^^^^^^^ > Replace with: Variable1=Lettuce
Folder2
file.ini
DO NOTHING
Folder3
file.ini
Variable1=TOMATO
^^^^^^^^^^^^^^^^^ > Replace with: Variable1=Lettuce
FolderA
file.ini
DO NOTHING
FolderB
file.ini
DO NOTHING
FolderC
file.ini
Variable1=TOMATO
^^^^^^^^^^^^^^^^^ > Replace with: Variable1=Lettuce
My variable Folder list for the above folder structure would be as follows: 对于以上文件夹结构,我的变量文件夹列表如下:
My variables would be: 我的变量是:
I tried to use WinGrep and the like, but I only saw one-line excludes versus being able to call a list of folders as above. 我尝试使用WinGrep之类的工具,但只能看到单行排除项,而不能像上面那样调用文件夹列表。 My production run of this one-time script will pull ~1000 folders out of the list of ~4000, so a manual process is not possible.
我的一次性脚本的生产运行会将〜1000个文件夹从〜4000个列表中拉出,因此无法进行手动处理。
A plan (independent of the implementation language): 计划(与实现语言无关):
If you want a VBScript version, post some details wrt the .ini file and the replacing. 如果要使用VBScript版本,请在.ini文件和替换文件中发布一些详细信息。
Update: 更新:
VBScript Code to get you started: VBScript代码可以帮助您入门:
Const csFrom = "Variable1=TOMATO"
Const csTo = "Variable1=Lettuce"
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sDir : sDir = oFS.GetAbsolutePathName("..\test\13921972")
Dim tsFL : Set tsFL = oFS.OpenTextFile(oFS.BuildPath(sDir, "whitelist.txt"))
Do Until tsFL.AtEndOfStream
Dim sFSpec : sFSpec = Trim(tsFL.ReadLine())
If "" <> sFSpec Then
WScript.Echo "wl:", sFSpec
sFSpec = oFS.BuildPath(sDir, oFS.BuildPath(sFSpec, "some.ini"))
WScript.Echo "fs:", sFSpec
If oFS.FileExists(sFSpec) Then
Dim sAll : sAll = oFS.OpenTextFile(sFSpec).ReadAll()
WScript.Echo sAll
oFS.CreateTextFile(sFSpec, True).Write Replace(sAll, csFrom, csTo)
WScript.Echo oFS.OpenTextFile(sFSpec).ReadAll()
End If
End If
Loop
tsFL.Close
output of two runs: 两次运行的输出:
wl: dir1
fs: E:\trials\SoTrials\answers\13415663\test\13921972\dir1\some.ini
Variable0=NoliMeTangere
Variable1=TOMATO
Variable2=NoliMeTangere
Variable0=NoliMeTangere
Variable1=Lettuce
Variable2=NoliMeTangere
wl: dir1
fs: E:\trials\SoTrials\answers\13415663\test\13921972\dir1\some.ini
Variable0=NoliMeTangere
Variable1=Lettuce
Variable2=NoliMeTangere
Variable0=NoliMeTangere
Variable1=Lettuce
Variable2=NoliMeTangere
I'll submit this as a Powershell-solution. 我将其作为Powershell解决方案提交。 It requires static "before" and "after" strings for all files.
对于所有文件,它都需要静态的“ before”和“ after”字符串。 If this is not the case, you might need to use an hashtable to store the values.
如果不是这种情况,则可能需要使用哈希表来存储值。 As you may see it also requires the whitelist to have one path per line and paths are absolute (ex. C:\\windows)
如您所见,它还要求白名单每行有一个路径,并且路径是绝对路径(例如C:\\ windows)
$before = "this string is getting replaced"
$after = "this is my replacement"
$whitelist = [array](Get-content "path\to\whitelist")
foreach ($path in $whitelist) {
$files = Get-ChildItem *.ini -Path $path -Recurse
foreach ($file in $files) {
(Get-Content $file.fullname).Replace($before,$after) | set-content $file.fullname
}
}
Whitelist.txt sample: Whitelist.txt示例:
C:\test\whats up
C:\test\1
C:\test\2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.