No announcement yet.

Script for Adlib game

  • Filter
  • Time
  • Show
Clear All
new posts

  • Script for Adlib game

    My friend's kid has a birthday coming up and he likes Adlibs- where you make new stories based on inputting different words each time.
    I'm trying to figure out how to create a script to do this:

    I have a directory called C:\Story
    Inside the directory is an index.html file with the keyword ChangeMe scattered through-out it.

    Another directory with images at C:\Story\images
    Inside this directory are various images all named ChangeMeSomething.jpg.

    And, of course, the C:\Story\css directory will just be copied to the new directory.

    I need a script which will:
    1. Ask the user to input a city name. (We'll call the answer $City)
    2. Create a new directory called C:\$City.
    3. Open each .HTML file in C:\Story and change every instance of ChangeMe to $City and save the new .HTML file in C:\$City with the same file name.
    4. Create directory C:\$City\images and copy all pics in C:\Story\images to C:\$City\images with the new name $CitySomething.jpg
    5. Copy C:\Story\css to C:\$City\css

    Thanks in advance. I've never done anything like this and I'm have a terrible freakin' time figuring out how. I just thought it would be neat for the kid.

  • #2
    Re: Script for Adlib game

    maybe something like,
    sourceFolder = "C:\Story"
    sourceKeyword = "ChangeMe"
    CityName = AskMe
    destinationFolder = "C:\$" & CityName  'remove the $ sign if you don't like it.
    DIM objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    ' create new directory
    If objFSO.FolderExists(destinationFolder) = False Then
       MakeDir(destinationFolder & "\images")
       wscript.echo "A folder with that name already exists." _
           & vbNewLine & "Delete it or rename it first, then try again."
       CityName = AskMe   
    End IF
    ' copy css files
    objFSO.CopyFolder sourceFolder & "\css", destinationFolder & "\css"
    ' enum files in images subfoder, copy and rename to the new folder
    Set objFolder = objFSO.GetFolder(sourceFolder & "\images")
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
       ObjFSO.CopyFile objFile.path, destinationFolder & "\images\" _
         & Replace(objFile.Name, sourceKeyword, CityName,1,-1,1)
    ' copy and edit index.html file
    Set objFile = objFSO.OpenTextFile(sourceFolder & "\index.html", 1)
    strText = objFile.ReadAll
    strText = Replace(strText, sourceKeyword, CityName, 1,-1,1)
    Set objFile = objFSO.CreateTextFile(destinationFolder & "\index.html",true)
    objFile.WriteLine strText
    function AskMe
       AskMe = Trim(inputbox(vbNewLine & vbNewLine _
           & vbNewLine & vbNewLine & vbNewLine _
           & "Enter the name of a city" _
           , "Name of city", sourceKeyword))
       'If it was typed strip off the leading $ sign
       If instr(AskMe, "$") = 1 Then AskMe = mid(AskMe,2)
       If IsEmpty(AskMe) Then wscript.quit
    end function
    Function MakeDir(strPath)
      Dim strParentPath
      With objFSO
        If Not .FolderExists(strPath) Then
          strParentPath = .GetParentFolderName(strPath)
          If Not .FolderExists(strParentPath) Then MakeDir strParentPath
          If Not .FolderExists(strPath) Then .CreateFolder strPath
        End If 
        MakeDir = .FolderExists(strPath)
      End With
    End Function
    Paste the code into a new Notepad file. Give the file an .vbs extension.
    Start the script by dubbelclicking the vbs file.
    Last edited by Rems; 13th August 2012, 17:34.

    This posting is provided "AS IS" with no warranties, and confers no rights.


    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts