Results 1 to 11 of 11
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Extract string from file as new folder name

    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

  2. #2
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wetherby, Yorkshire, England
    Posts
    283
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract string from file as new folder name

    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

  3. #3
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Extract string from file as new folder name

    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...

    <pre>@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 rocessTMPfile %%a
    endlocal
    goto :eof
    ::----------------------------------------------------------------------
    rocessTMPfile
    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</pre>

    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract string from file as new folder name

    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?

  5. #5
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Extract string from file as new folder name

    Here's the update, but it's a bit more difficult to test!<pre>@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 rocessTMPfile %%a
    endlocal
    goto :eof
    ::----------------------------------------------------------------------
    rocessTMPfile 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 rocessTIFs
    ) 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
    ::----------------------------------------------------------------------
    rocessTIFs 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</pre>

    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  6. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract string from file as new folder name

    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

  7. #7
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Extract string from file as new folder name

    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...
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  8. #8
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Extract string from file as new folder name

    Third attempt below!<pre>@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 rocessTMPfile %%a
    endlocal
    goto :eof
    ::----------------------------------------------------------------------
    rocessTMPfile 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 rocessTIFs
    ) 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
    ::----------------------------------------------------------------------
    rocessTIFs 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</pre>

    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  9. #9
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract string from file as new folder name

    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

  10. #10
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Extract string from file as new folder name

    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...
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  11. #11
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract string from file as new folder name

    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 <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>

Posting Permissions

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