存储文件夹下所有.cpp和.h的代码到对应的txt文件里

最近大半年刷了160多天的题,每次刷的时候都要新建一个VS文件,所以文件内存太大了,又舍不得删,就用ai整了一个脚本,可将当前路径下的所有文件里的.cpp和.h文件储存到相应名字的txt文件里,若文件夹下还有文件则会将其保存在对应的文件夹下,并最终将所有文件保存在一个文件夹里。(中文会乱码,可能是保存格式的问题,懒得改。注意此脚本统计的是当前路径下的所有文件里的cpp和.h所以最好不要有环境什么的.可能还有少许bug)以下是效果图

代码为

Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(".")

' Create a new folder in the current directory to store the txt files
outputRootFolder = fso.BuildPath(folder.Path, "txt_files")
If Not fso.FolderExists(outputRootFolder) Then
    fso.CreateFolder(outputRootFolder)
End If

ProcessFolder folder

Sub ProcessFolder(folder)
    For Each subFolder In folder.SubFolders
        ProcessFolder subFolder
    Next

    For Each file In folder.Files
        If LCase(fso.GetExtensionName(file)) = "cpp" Or LCase(fso.GetExtensionName(file)) = "h" Then
            SaveFileContentToTxt file, folder
        End If
    Next
End Sub

Sub SaveFileContentToTxt(file, folder)
    Dim inputFile, outputFile, fileContent, outputFolder, outputFileName, parentFolderName
    Set inputFile = fso.OpenTextFile(file, 1)
    
    ' Check if the file is empty
    If inputFile.AtEndOfStream Then
        inputFile.Close
        Exit Sub
    End If
    
    fileContent = inputFile.ReadAll
    inputFile.Close

    parentFolderName = fso.GetBaseName(folder.Path)
    outputFolder = fso.BuildPath(outputRootFolder, parentFolderName & "txt")

    If Not fso.FolderExists(outputFolder) Then
        fso.CreateFolder(outputFolder)
    End If

    outputFileName = fso.BuildPath(outputFolder, fso.GetBaseName(file) & ".txt")
    Set outputFile = fso.CreateTextFile(outputFileName, True)
    outputFile.Write fileContent
    outputFile.Close
End Sub
将其复制进一个新建的txt文件,在将后缀名改为.vbs再双击即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值