• Extract string from file as new folder name

    Home » Forums » AskWoody support » Windows » Windows – other » Extract string from file as new folder name

    Author
    Topic
    #394420

    A colleague of mine has a folder full of assorted files. Among these are a large number of text files (with the extension .tmp). These files contain a text string which needs to be extracted and used to create new directories (i.e the text is the new directory name). There are many many files, so to open them manually, read the text, create a new folder and rename it appropriately, would take too long.

    I need to find some code that will achieve the following:
    Loop through the directory to find the relevant files (i.e .tmp extension), search each file in turn for a piece of text, when found append text to another file, possible to create a batch file which could be run to create the new folders.

    I have attached a sample file (renamed .txt), in this case the text to be extracted would be BE-VER.102
    i.e the text after the ‘FOLDER NAME’ on the first line

    Any help gratefully received

    Viewing 3 reply threads
    Author
    Replies
    • #722663

      AutoIt and one of its add-ons (Atool or AutXtra) will do this if you can do a little easy coding. You can get AutoIt here along with, I think, a link to the two extras.
      Peter

    • #722664

      AutoIt and one of its add-ons (Atool or AutXtra) will do this if you can do a little easy coding. You can get AutoIt here along with, I think, a link to the two extras.
      Peter

    • #722697

      Darsha

      I hope that this hasty construction will do what you want. Read the assumptions CAREFULLY!! I’m doing the MD within this BATch file (once you remove the preceding ECHO). It’s safe to run this BATch file to see what you’d get since it doesn’t create anything until you remove the ECHO. Any questions, just ask…

      @echo off
      
      :: do what Darsha wants to do with a set of temp files
      
      :: assumptions:
      ::   1) this BATch file lives in the directory where all the *.tmp files are
      ::   2) each TMP file has only ONE instance of the string "FOLDER NAME"
      ::   3) the token following "'FOLDER NAME'" is the name of the required directory
      ::   4) the folder name should be created in C:
      
      setlocal
      for %%a in (*.tmp) do call :processTMPfile %%a
      endlocal
      goto :eof
      ::----------------------------------------------------------------------
      :processTMPfile
      set filename=%1
      set dirname=
      for /f "tokens=3" %%a in ('find /i "FOLDER NAME" %filename%') do set dirname=%%a
      echo.
      if defined dirname (
        echo Found string %dirname% in file %filename%
        rem  remove next ECHO to get MakeDir to work!
        echo md c:%dirname%
        ) else (
        echo Couldn't find string FOLDER NAME in file %filename%
        )
      :: pause for five seconds before going to process next file
      ping -n 6 127.0.0.1 >nul
      goto :eof
      • #723002

        Wow, thanks that should save someone some work.

        What I didn’t add to my first post was what we also need to do is once the new directories have been made is to move the tif files (all located in the same directory as the tmp files) into the appropriate directory. The tif files that should go in the each directory are listed at the bottom of each tmp file. Obviously, I’ve got no idea how to do this so I don’t know whether its asking a lot. If it is then I guess it would be simpler to add code to the previous batch file to rename each tmp file (with the same text string used to create the directories).

        Any thoughts?

        • #723067

          Here’s the update, but it’s a bit more difficult to test!

          @echo off
          :: do what Darsha wants to do with a set of temp files
          :: assumptions:
          ::   1) this BATch file lives in the directory where all the *.tmp files are
          ::   2) each TMP file has only ONE instance of the string "FOLDER NAME"
          ::   3) the token following "'FOLDER NAME'" is the name of the required directory
          ::   4) this directory name is in 8.3 format (i.e. no long file names)
          ::   5) this target directory is created in C:
          ::   6) the paths to the TIFs in each file are correct
          ::   7) the TIFs do NOT exist in the newly-created target directory
          ::   8) MOVE is required, rather than COPY
          
          setlocal
          set log=%~n0.txt
          for %%a in (*.tmp) do call :processTMPfile %%a
          endlocal
          goto :eof
          ::----------------------------------------------------------------------
          :processTMPfile to determine directory name and TIF files to move
          set filename=%1
          set dirname=
          for /f "tokens=3" %%a in ('find /i "FOLDER NAME" %filename%') do set dirname=%%a
          echo.
          if defined dirname (
            echo Found string %dirname% in file %filename%
            rem  remove next ECHO to get MakeDir to work!
            ECHO md c:%dirname%
            >>%log% echo.
            >>%log% echo Filename %filename%, creating directory c:%dirname%
            call :processTIFs
            ) else (
            echo Couldn't find string FOLDER NAME in file %filename%
            )
          :: pause for five seconds before going to process next file
          ping -n 6 127.0.0.1 > nul
          goto :eof
          ::----------------------------------------------------------------------
          :processTIFs whose path names are found in the current TMP file
          if not exist c:%dirname% echo Directory name C:%dirname% wasn't created
          for /f "tokens=1" %%a in ('findstr /i ".tif" %filename%') do (
            echo    Moving c:%%a to c:%dirname%
            rem  remove next ECHO to get Move to work!
            ECHO move c:%%a c:%dirname%
            >>%log% echo   c:%%a moved to c:%dirname%
            )
          goto :eof
          • #723152

            Cheers John
            Its not quite working but I think thats my fault.

            I tried to ‘sanitise’ the file paths in the sample file I attached, therein creating a problem
            The end part of tmp file would contain text more like that below i.e. a very long path name which could include spaces in the various sub directory names

            Image Field Image
            7202240 windowdataactivebox1f400000001.tif
            7202240 windowdataactivebox1f400000002.tif
            7202240 windowdataactivebox1f400000003.tif
            7202240 windowdataactivebox1f400000004.tif

            From the example above, I think the path 7202240 windowdataactivebox1f4 is where the tmp file and hopefully the batch file would be run from

            When the batch file is run it tries, for example, to copy the (non existent) file ‘7202240’ so I guess the space is throwing it.

            I tinkered with your code to try to create the directories in the same location as the tmp files (and also the tif files and the batch file) just be removing the ‘c:’ which worked ok. Anything more though and I’m lost.

            Can you just extract the tif name to be used at the %filename% variable and run the copy command without requiring the drive letter.

            In advance of more help, cheers

            • #723218

              I’m afraid won’t be able to work on this until late tomorrow morning (it’s 18:08 as I write).

              The blank space makes things a bit messier since I have to put double quotes in various places…! And the C: doesn’t really affect it but I’ll take it out everwhere…

            • #723587

              Third attempt below!

              @echo off
              :: do what Darsha wants to do with a set of temp files
              :: assumptions:
              ::   1) this BATch file lives in the directory where all the *.tmp files are
              ::   2) each TMP file has only ONE instance of the string "FOLDER NAME"
              ::   3) the token following "'FOLDER NAME'" is the name of the required directory
              ::   4) this directory name is in 8.3 format (i.e. no long file names)
              ::   5) this target directory is created in the root of the current drive
              ::   6) the paths to the TIFs in each file are correct, but can contain blanks
              ::   7) the TIFs do NOT exist in the newly-created target directory
              ::   8) MOVE is required, rather than COPY
              
              setlocal
              set log=%~n0.txt
              for %%a in (*.tmp) do call :processTMPfile %%a
              endlocal
              goto :eof
              ::----------------------------------------------------------------------
              :processTMPfile to determine directory name and TIF files to move
              set filename=%1
              set dirname=
              for /f "tokens=3" %%a in ('find /i "FOLDER NAME" %filename%') do set dirname=%%a
              echo.
              if defined dirname (
                echo Found string %dirname% in file %filename%
                rem  remove next ECHO to get MakeDir to work!
                ECHO md %dirname%
                >>%log% echo.
                >>%log% echo Filename %filename%, creating directory c:%dirname%
                call :processTIFs
                ) else (
                echo Couldn't find string FOLDER NAME in file %filename%
                )
              :: pause for five seconds before going to process next file
              ping -n 6 127.0.0.1 > nul
              goto :eof
              ::----------------------------------------------------------------------
              :processTIFs whose path names are found in the current TMP file
              if not exist %dirname% echo Directory name %dirname% wasn't created
              for /f "tokens=*" %%a in ('findstr /i ".tif" %filename%') do (
                echo    Moving "%%a" to %dirname%
                rem  remove next ECHO to get Move to work!
                ECHO move  "%%a" %dirname%
                >>%log% echo   "%%a" moved to %dirname%
                )
              goto :eof
            • #723642

              John , what can I say, it works a treat, thankyou.

              One last thing (when you’ve got a minute), can you explain simply how the %dirname% and %filename% variables are defined (this is just for my interest) and is there any way I can save the contents of the cmd prompt window before it closes.

              Cheers again

            • #723653

              When you say “can you explain simply how the %dirname% and %filename% variables are defined”, I’m not sure what you mean! Do you mean “how the variables are set up by the FOR /F statement”? If so, you’ll need to look at the help information from FOR (do FOR /? in a Command prompt windows). FOR /F is one of the most powerful, and difficult to get into, commands in the (extended) BATch command language.

              There’s no way to “save the contents of the cmd prompt window before it closes”, which is why I set up a LOG file to save what I thought was relevant information. If you called the BATch file DARSHA.BAT you would find that the log file is called DARSHA.TXT…

            • #723657

              Thanks, I’ll try to take a look at the help info on FOR.

              As for the log file, I stupidly just hadn’t noticed its creation. It contains all the info I need.

              Again, a very big THANKYOU clapping

            • #723658

              Thanks, I’ll try to take a look at the help info on FOR.

              As for the log file, I stupidly just hadn’t noticed its creation. It contains all the info I need.

              Again, a very big THANKYOU clapping

            • #723654

              When you say “can you explain simply how the %dirname% and %filename% variables are defined”, I’m not sure what you mean! Do you mean “how the variables are set up by the FOR /F statement”? If so, you’ll need to look at the help information from FOR (do FOR /? in a Command prompt windows). FOR /F is one of the most powerful, and difficult to get into, commands in the (extended) BATch command language.

              There’s no way to “save the contents of the cmd prompt window before it closes”, which is why I set up a LOG file to save what I thought was relevant information. If you called the BATch file DARSHA.BAT you would find that the log file is called DARSHA.TXT…

            • #723643

              John , what can I say, it works a treat, thankyou.

              One last thing (when you’ve got a minute), can you explain simply how the %dirname% and %filename% variables are defined (this is just for my interest) and is there any way I can save the contents of the cmd prompt window before it closes.

              Cheers again

            • #723588

              Third attempt below!

              @echo off
              :: do what Darsha wants to do with a set of temp files
              :: assumptions:
              ::   1) this BATch file lives in the directory where all the *.tmp files are
              ::   2) each TMP file has only ONE instance of the string "FOLDER NAME"
              ::   3) the token following "'FOLDER NAME'" is the name of the required directory
              ::   4) this directory name is in 8.3 format (i.e. no long file names)
              ::   5) this target directory is created in the root of the current drive
              ::   6) the paths to the TIFs in each file are correct, but can contain blanks
              ::   7) the TIFs do NOT exist in the newly-created target directory
              ::   8) MOVE is required, rather than COPY
              
              setlocal
              set log=%~n0.txt
              for %%a in (*.tmp) do call :processTMPfile %%a
              endlocal
              goto :eof
              ::----------------------------------------------------------------------
              :processTMPfile to determine directory name and TIF files to move
              set filename=%1
              set dirname=
              for /f "tokens=3" %%a in ('find /i "FOLDER NAME" %filename%') do set dirname=%%a
              echo.
              if defined dirname (
                echo Found string %dirname% in file %filename%
                rem  remove next ECHO to get MakeDir to work!
                ECHO md %dirname%
                >>%log% echo.
                >>%log% echo Filename %filename%, creating directory c:%dirname%
                call :processTIFs
                ) else (
                echo Couldn't find string FOLDER NAME in file %filename%
                )
              :: pause for five seconds before going to process next file
              ping -n 6 127.0.0.1 > nul
              goto :eof
              ::----------------------------------------------------------------------
              :processTIFs whose path names are found in the current TMP file
              if not exist %dirname% echo Directory name %dirname% wasn't created
              for /f "tokens=*" %%a in ('findstr /i ".tif" %filename%') do (
                echo    Moving "%%a" to %dirname%
                rem  remove next ECHO to get Move to work!
                ECHO move  "%%a" %dirname%
                >>%log% echo   "%%a" moved to %dirname%
                )
              goto :eof
            • #723219

              I’m afraid won’t be able to work on this until late tomorrow morning (it’s 18:08 as I write).

              The blank space makes things a bit messier since I have to put double quotes in various places…! And the C: doesn’t really affect it but I’ll take it out everwhere…

          • #723153

            Cheers John
            Its not quite working but I think thats my fault.

            I tried to ‘sanitise’ the file paths in the sample file I attached, therein creating a problem
            The end part of tmp file would contain text more like that below i.e. a very long path name which could include spaces in the various sub directory names

            Image Field Image
            7202240 windowdataactivebox1f400000001.tif
            7202240 windowdataactivebox1f400000002.tif
            7202240 windowdataactivebox1f400000003.tif
            7202240 windowdataactivebox1f400000004.tif

            From the example above, I think the path 7202240 windowdataactivebox1f4 is where the tmp file and hopefully the batch file would be run from

            When the batch file is run it tries, for example, to copy the (non existent) file ‘7202240’ so I guess the space is throwing it.

            I tinkered with your code to try to create the directories in the same location as the tmp files (and also the tif files and the batch file) just be removing the ‘c:’ which worked ok. Anything more though and I’m lost.

            Can you just extract the tif name to be used at the %filename% variable and run the copy command without requiring the drive letter.

            In advance of more help, cheers

        • #723068

          Here’s the update, but it’s a bit more difficult to test!

          @echo off
          :: do what Darsha wants to do with a set of temp files
          :: assumptions:
          ::   1) this BATch file lives in the directory where all the *.tmp files are
          ::   2) each TMP file has only ONE instance of the string "FOLDER NAME"
          ::   3) the token following "'FOLDER NAME'" is the name of the required directory
          ::   4) this directory name is in 8.3 format (i.e. no long file names)
          ::   5) this target directory is created in C:
          ::   6) the paths to the TIFs in each file are correct
          ::   7) the TIFs do NOT exist in the newly-created target directory
          ::   8) MOVE is required, rather than COPY
          
          setlocal
          set log=%~n0.txt
          for %%a in (*.tmp) do call :processTMPfile %%a
          endlocal
          goto :eof
          ::----------------------------------------------------------------------
          :processTMPfile to determine directory name and TIF files to move
          set filename=%1
          set dirname=
          for /f "tokens=3" %%a in ('find /i "FOLDER NAME" %filename%') do set dirname=%%a
          echo.
          if defined dirname (
            echo Found string %dirname% in file %filename%
            rem  remove next ECHO to get MakeDir to work!
            ECHO md c:%dirname%
            >>%log% echo.
            >>%log% echo Filename %filename%, creating directory c:%dirname%
            call :processTIFs
            ) else (
            echo Couldn't find string FOLDER NAME in file %filename%
            )
          :: pause for five seconds before going to process next file
          ping -n 6 127.0.0.1 > nul
          goto :eof
          ::----------------------------------------------------------------------
          :processTIFs whose path names are found in the current TMP file
          if not exist c:%dirname% echo Directory name C:%dirname% wasn't created
          for /f "tokens=1" %%a in ('findstr /i ".tif" %filename%') do (
            echo    Moving c:%%a to c:%dirname%
            rem  remove next ECHO to get Move to work!
            ECHO move c:%%a c:%dirname%
            >>%log% echo   c:%%a moved to c:%dirname%
            )
          goto :eof
      • #723003

        Wow, thanks that should save someone some work.

        What I didn’t add to my first post was what we also need to do is once the new directories have been made is to move the tif files (all located in the same directory as the tmp files) into the appropriate directory. The tif files that should go in the each directory are listed at the bottom of each tmp file. Obviously, I’ve got no idea how to do this so I don’t know whether its asking a lot. If it is then I guess it would be simpler to add code to the previous batch file to rename each tmp file (with the same text string used to create the directories).

        Any thoughts?

    • #722698

      Darsha

      I hope that this hasty construction will do what you want. Read the assumptions CAREFULLY!! I’m doing the MD within this BATch file (once you remove the preceding ECHO). It’s safe to run this BATch file to see what you’d get since it doesn’t create anything until you remove the ECHO. Any questions, just ask…

      @echo off
      
      :: do what Darsha wants to do with a set of temp files
      
      :: assumptions:
      ::   1) this BATch file lives in the directory where all the *.tmp files are
      ::   2) each TMP file has only ONE instance of the string "FOLDER NAME"
      ::   3) the token following "'FOLDER NAME'" is the name of the required directory
      ::   4) the folder name should be created in C:
      
      setlocal
      for %%a in (*.tmp) do call :processTMPfile %%a
      endlocal
      goto :eof
      ::----------------------------------------------------------------------
      :processTMPfile
      set filename=%1
      set dirname=
      for /f "tokens=3" %%a in ('find /i "FOLDER NAME" %filename%') do set dirname=%%a
      echo.
      if defined dirname (
        echo Found string %dirname% in file %filename%
        rem  remove next ECHO to get MakeDir to work!
        echo md c:%dirname%
        ) else (
        echo Couldn't find string FOLDER NAME in file %filename%
        )
      :: pause for five seconds before going to process next file
      ping -n 6 127.0.0.1 >nul
      goto :eof
    Viewing 3 reply threads
    Reply To: Extract string from file as new folder name

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: