Results 1 to 5 of 5

Thread: Rename Thousands of Recipe *.txt files

  1. #1
    VBAX Newbie
    Mar 2018

    Rename Thousands of Recipe *.txt files

    [QUOTE=fumei;219234]First off...
    Then the code should go through IF statement with the following rules.
    If keyword=”chief complaint”, make variable “T_Document”= chart_note
    IF keyword=”billing statemen”t, make variable “T_Document”=billing statement
    IF keyword=“Mark Gventer, D.P.M., F.A.C.F.O.”, make variable “T_Document”=prescription
    If keyword=”DAILY SIGNATURE FORM”, make variable “T_Document”=daily signature
    If keyword=“PRIVACY PRACTICES ACKNOWLEDGEMENT”, make variable “T_Document”=privacy
    [/quote}No! Do not use an bu7nch of IF statements. This is precisely wehat Select Case is used for. Here is why.

    ALL your IF statements will be executed, because they are independent instructions - there is NO inherent logic linking then (at least to VBA). Here is the same result using Select Case:[vba]
    ' Then the code should go through with the following rules.
    Select Case keyword
    Case ”chief complaint”
    T_Document = "chart_note"
    Case ”billing statement"
    T_Document = "billing statement"
    Case “Mark Gventer, D.P.M., F.A.C.F.O.”
    T_Document = "prescription"
    T_Document = "daily signature"
    T_Document = "privacy"
    End Select
    [/vba]Select Case testing multiple values for the same variable - in this case, the variable keyword (I am not happy with that name though, as it seems close to being a restricted term)

    As for: "Then I need to go through the text that is pasted by function Selection.Paste (please see the code above) "

    I do not follow. There is a Sub above (not a Function). And I am unclear as to what it is doing. Using Selection is not a good idea.

    BTW: I do not know if your posted file has macros, as I do not use 2007, and convertin git strips all code.

    Moderator Edit: This refers to :
    Last edited by SamT; 03-17-2018 at 01:49 PM.

  2. #2
    VBAX Newbie
    Mar 2018
    I'm a retired Chef with thousand and thousands of recipes named 1.txt, 2.txt, 3.txt and many many more. I want to rename them using the title of the recipe, very first line, inside the file. Such as; 1.txt file first line title---steak tartare, 2.txt file first line---shrimp pesto (so that the files are renamed) steak_tartare.txt and shrimp_scampi.txt

    I know how to run a module and run .vbs but no experience in coding. I found the original post through searching but it doesn't work. I'd greatly appreciate a point in any direction on this.

    Thanks, johnvins

  3. #3
    Moderator VBAX Wizard SamT's Avatar
    Oct 2006
    Near Columbia
    This and the above two posts posts were moved from an 8 yo thread to this new thread
    I always expect the student to do their homework and find all the errrors I leeve in.

    Please take the time to read the Forum FAQ

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Jul 2008
    You should be able to do the renaming with code like the following from any Office application:
    Sub UpdateRecipeFileNames()
    Dim strFolder As String, strFile As String, strFlNm As String
    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    strFile = Dir(strFolder & "\*.txt", vbNormal)
    While strFile <> ""
      Open strFolder & "\" & strFile For Input As #1
      Line Input #1, strFlNm
      strFlNm = Trim(Split(strFlNm, vbCrLf)(0))
      Close #1
      If strFlNm <> "" Then
        If Dir(strFolder & "\" & strFlNm) = "" Then
          Name strFolder & "\" & strFile As strFolder & "\" & strFlNm & ".txt"
        End If
      End If
      strFile = Dir()
    End Sub
    Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function
    The code won't rename files for which the first line is empty or a file already exists with the same name.

    Simply select the folder to process and all .txt files in it will be processed.
    Last edited by macropod; 03-18-2018 at 03:46 PM. Reason: Code refinements
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Newbie
    Mar 2018

    I solved this with 3 days of serious reading.

    I was able to come up with a solution for renaming text files based on the first line of the file itself. To begin, I have thousands of original recipes that I have created over the years. My goal is to have them all as text files so I can create a database. Many are Word .doc, .docx or .xlx and are named as 1_.doc, 2_doc, 3_doc, 005_.xls etc…

    To convert these to .txt I used a program called Doxcillion---that was easy as it converted a few thousand .doc files in a minute or two. Only later I found that the conversion was to UTF-8-BOM and that after running my rename.bat file, weird special characters were added before each file name. The files needed to be encoded as UTF-8 or UTF-16 before running the rename file on them. (It seems to be an issue that others have experienced from the posts I read online). So now I downloaded a free program, for personal use, called UTFCast Express. It changed the encoding of all my .txt files to UTF-8 in an instant.

    I ran rename .bat---(I'd like to post the .bat code but the forum is blocking me from doing so. How can I show it to you?)

    Continued from above--------------------------------------

    Most files were renamed as what the very first line of text was in the file, i.e. Bacon and Onion Muffins.txt, Chocolate Mousse.txt, Rumaki.txt and on and on. However, some of my recipes were written where the title was not on the first line but the second or third and they were not renamed, they stayed the same. Ugh, I had to go back and delete all the blank lines above the titles to move every title to the very first line. I used Notepad++ and Texpipe Lite with Easy Pattern Helper to do that. Once this was corrected and there was no white space in front of the tile, rename.bat ran like a charm!

    My files are now named from the first line of the .txt file although the file names are exactly as written in the text file, with capital letters and spaces like these---Boneless Center Cut Pork Loin Florentine .txt or Peach Cobbler with Cheddar Biscuits.txt (notice the spaces between words and in front of .txt) on the pork file---and that's how it and others are written in the recipe " Boneless Center Cut Pork Loin Florentine(with a single space after it. The biscuit title didn't have a single space after it, so there is no space before .txt).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts