Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: simple batch file needs little modding  (Read 2654 times)

0 Members and 1 Guest are viewing this topic.

mrwonker

    Topic Starter


    Rookie

    simple batch file needs little modding
    « on: March 10, 2010, 04:01:00 PM »
    Hi there I need to create a batch file that will read a text document and create a file for each line of text I have one that almost works but the trouble is the lines of texts have spaces in them and it is creating a file for each section of text so if i have a line of text that reads 'I want only one file' it will create files called I, want, only, one and file the script I am using is

    for /f %%i in (file.txt) do @echo off> %%i

    have tried

    for /f %%i in (file.txt) do @echo off> "%%i"

    and

    for /f "%%i" in (file.txt) do @echo off> "%%i"

    with no luck any help much apreciated.
    thanks, James.

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: simple batch file needs little modding
    « Reply #1 on: March 10, 2010, 04:14:15 PM »
    Try:

    @echo off
    for /f "delims=" %%i in (file.txt) do echo. > %%i

    or if you're running it from the command prompt,

    for /f "delims=" %i in (file.txt) do echo. > %i

    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: simple batch file needs little modding
    « Reply #2 on: March 10, 2010, 04:15:04 PM »
    Code: [Select]
    for /f "tokens=*" %%i in (file.txt) do echo %%i


    echoed the list of filenames I had in the file (including spaces).

    I'm pretty sure @echo off will echo nothing to the file though.
    I was trying to dereference Null Pointers before it was cool.

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: simple batch file needs little modding
    « Reply #3 on: March 10, 2010, 04:21:14 PM »
    Try:

    @echo off
    for /f "delims=" %%i in (file.txt) do echo. > %%i

    or if you're running it from the command prompt,

    for /f "delims=" %i in (file.txt) do echo. > %i


    HEY! That was my 3000th post!!!

    Anyway, BC, wouldn't "tokens=*" and "delims=" pretty much do the same thing?
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    greg



      Intermediate

      Thanked: 7
      Re: simple batch file needs little modding
      « Reply #4 on: March 10, 2010, 04:31:02 PM »
      C:\batch>type wonker.bat
      Code: [Select]
      @echo  off
      setlocal enabledelayedexpansion
      for /f  "delims=" %%i in (file.txt) do (
      echo %%i
      set newf=%%i
      echo Turn off > "!newf!.txt"
      dir "!newf!.txt"
      )

      Output:

      C:\batch> wonker.bat
      wonker
       Volume in drive C has no label.
       Volume Serial Number is F4A3-D6B3

       Directory of C:\batch

      03/10/2010  05:28 PM                11 wonker.txt
                     1 File(s)             11 bytes
                     0 Dir(s)  298,567,569,408 bytes free
      hope
       Volume in drive C has no label.
       Volume Serial Number is F4A3-D6B3

       Directory of C:\batch

      03/10/2010  05:28 PM                11 hope.txt
                     1 File(s)             11 bytes
                     0 Dir(s)  298,567,569,408 bytes free
      works
       Volume in drive C has no label.
       Volume Serial Number is F4A3-D6B3

       Directory of C:\batch

      03/10/2010  05:28 PM                11 works.txt
                     1 File(s)             11 bytes
                     0 Dir(s)  298,567,569,408 bytes free

      C:\batch>

      Input:


      C:\batch>type  file.txt
      wonker
      hope
      works
      C:\batch>
      « Last Edit: March 10, 2010, 04:42:02 PM by greg »
      Have a Nice Day

      mrwonker

        Topic Starter


        Rookie

        Re: simple batch file needs little modding
        « Reply #5 on: March 10, 2010, 05:07:17 PM »
        thanks to all who replied from all your suggestions I tried the easiest looking one first and with a little modding got it working perfect I ended up using

        for /f "tokens=*" %%i in (file.txt) do @echo off> %%i

        not that its that important for me at the moment but I have in past wanted to do same thing but to make folders instead of files but used to have same problem with it creating new folder for each word so thought Id see if this would now work same for folders so I tried

        for /f "tokens=*" %%i in (file.txt) do mkdir %%i

        but this still made the folders for each word any ideas why that is?


        Helpmeh



          Guru

        • Roar.
        • Thanked: 123
          • Yes
          • Yes
        • Computer: Specs
        • Experience: Familiar
        • OS: Windows 8
        Re: simple batch file needs little modding
        « Reply #6 on: March 10, 2010, 05:15:25 PM »
        Wrap it in quotes. Like this:

        "%%i"
        Where's MagicSpeed?
        Quote from: 'matt'
        He's playing a game called IRL. Great graphics, *censored* gameplay.

        mrwonker

          Topic Starter


          Rookie

          Re: simple batch file needs little modding
          « Reply #7 on: March 10, 2010, 05:22:15 PM »
          brilliant had tried similar when trying to mod my original script for making files which didnt work so didnt try doing same again, but it worked on that one thanks so much for all your help this forum is great, get plenty of quick succesful responses. I'm just beginning with trying to learn some basics with making scripts and have got a few ideas of scripts I would like to create so no doubt you will here from me again within the next few days.
          thanks again, James.