views:

104

answers:

2

I got the code

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "C:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    If InStr(strLine,"ex3")> 0 Then
        strLine = Replace(strLine,"ex3","ex5")
    End If 
    WScript.Echo strLine
Loop

The strLine replacing part i can fix myself to use with my own purposes, but how do i do something like this so that it doesn't require the file's name, it just edits all text files within the document?

+1  A: 

you can do it like this,

strFolder = "c:\myfolder"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
    strFileName =strFile.Name
    strFilePath = strFile.Path
    strFileExt = objFS.GetExtensionName(strFile)
    If strFileExt = "txt" Then
        Set objFile = objFS.OpenTextFile(strFile)
            ' your current code here..
        objFile.Close()
    End If
Next 
ghostdog74
A: 

so it should look like the following? because i get an error with it like that.

 @echo off
    @echo Welcome to the admin demoter V1.0!
    @echo This file will simply filter through all of your character files demoting them to members.
    @echo Press 1 to demote all, or press 2 to exit. 
    set /P CH=[1-2] 
    if "%CH%"=="1" goto demote 
    if "%CH%"=="2" goto end

    :demote

    strFolder = "C:\Users\andrew\Desktop\GUS V3\GUS V3\characters"
    Set objFolder = objFS.GetFolder(strFolder)
    For Each strFile In objFolder.Files
        strFileName =strFile.Name
        strFilePath = strFile.Path
        strFileExt = objFS.GetExtensionName(strFile)
        If strFileExt = "txt" Then
            Set objFile = objFS.OpenTextFile(strFile)
                Set objFS = CreateObject("Scripting.FileSystemObject")
    strFile = "C:\Users\andrew\Desktop\GUS V3\GUS V3\characters\%~f1"
    Set objFile = objFS.OpenTextFile(strFile)
    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        If InStr(strLine,"character-rights = 2")> 0 Then
            strLine = Replace(strLine,"character-rights = 2","character-rights = 1")
        End If 
        WScript.Echo strLine
    Loop
            objFile.Close()
        End If
    Next 
    :end
    exit
William
You are mixing Windows batch commands and VBScript in the same file, which is not going to work. You need to put your VBScript in a separate file and call it from the batch file.
aphoria