简体   繁体   English

在Windows环境中编辑文件夹白名单上的文件

[英]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: 对于以上文件夹结构,我的变量文件夹列表如下:

  • Folder1 资料夹1
  • Folder3 资料夹3
  • FolderC 资料夹

My variables would be: 我的变量是:

  • Search for: Variable1=TOMATO 搜索:Variable1 = TOMATO
  • Replace with: Variable1=Lettuce 替换为:Variable1 = Lettuce

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): 计划(与实现语言无关):

  1. Open the whitelist file for reading 打开白名单文件以供阅读
  2. For Each non-empty line build a complete/full file spec for the .ini file 对于每个非空行,为.ini文件构建完整/完整的文件规范
  3. If the .ini file exists, load it into memory, do the replacement, and write it back 如果.ini文件存在,则将其加载到内存中,进行替换,然后将其写回

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.

相关问题 在Windows 10上,授予用户创建,编辑和删除文件夹C:\\ VMs中的文件的权限,但禁止删除文件夹本身 - On Windows 10, grant a user rights to create, edit and delete files in the folder C:\VMs, but prevent deletion of the folder itself Windows 7不允许我编辑Common Application Data文件夹中的文件 - Windows 7 doesn't allow me edit files in Common Application Data folder 如何从安装在 Windows 10 上的 Wampserver 3 编辑/保存远程共享文件夹中的文件? - How to edit / save files on remote shared folder from Wampserver 3 installed on Windows 10? 如何在Windows和Linux环境中打开文件夹路径? - How to open a folder path on windows and linux environment? 从文件夹中删除文件[Windows] - Delete files from folder [Windows] 使用Python在Windows Server 2012上远程编辑文件 - Edit files remotely on Windows Server 2012 with Python Windows环境中的JMX身份验证文件路径 - JMX Authentication files path in windows environment Windows文件夹中的MD5哈希文件 - MD5 Hash of files in a Windows folder Windows CMD - 根据文件夹名称重命名文件 - Windows CMD - Rename files based on folder name 用于将文件复制到文件夹的 Windows 批处理脚本 - Windows batch script for copying files to folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM