what is wrong with that code? [SOLVED]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

what is wrong with that code? [SOLVED]

#1 Post by abc0502 » 07 Aug 2012 08:06

I'm trying to extract each line from a txt file to a separate files, this separate file's name will be taken from a nother file

all.txt file is the file that has the lines needs to be extracted,
c_list.txt is the files that contain the file's names in every line one name

Code: Select all

@echo off
cls
setlocal enabledelayedexpansion
set c=0
For /F "tokens=*" %%a in (all.txt) Do (
set /a c=c+1
set line=%%a
   For /F "tokens=*" %%b in (c_list.txt) do (
      echo !line!>>%%b.txt
)
)

so the first line from all.txt echo to a file named with the first name in the c_list.txt
and the 2nd line echo to a file named with the name of the 2nd name in the c_list and so is the rest of the lines

lines in all.txt files is equal to lines in the c_list.txt
each line in all.txt has a name in the c_list.txt file.
Last edited by abc0502 on 07 Aug 2012 11:25, edited 1 time in total.

Mark Heim
Posts: 2
Joined: 05 Aug 2012 08:52

Re: what is wrong with that code?

#2 Post by Mark Heim » 07 Aug 2012 08:32

Code: Select all

copy all.txt  c_list.txt

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: what is wrong with that code?

#3 Post by Ed Dyreen » 07 Aug 2012 08:36

'

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$srcFile=all.txt"
set "$indFile=c_list.txt"

set "i=" &for /f "usebackq delims=" %%? in (

       "!$indFile!"

) do   set /a i += 1 &set "$index!i!=%%~?"

set "c=" &for /f "usebackq delims=" %%? in (

       "!$indFile!"

) do (
       if !c! lss !i! ( set /a c += 1 ) else set c = 1

       for %%§ in ( "$index!c!" ) do >>"!%%~§!" (echo(%%?)
)

pause
exit
untested

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: what is wrong with that code?

#4 Post by abc0502 » 07 Aug 2012 08:36

Mark Heim wrote:

Code: Select all

copy all.txt  c_list.txt

this will just copy the content of the file all to the c_list

what i mean is that the file all has some lines in it, and i need to take each line and put it in separate files so the first line in a file and the 2nd line in a nother file, and these file's names sould be named with names taken from a list in the c_list file

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: what is wrong with that code?

#5 Post by abc0502 » 07 Aug 2012 08:44

hi Ed Dyreen, thanks alot :D your code working fine
it just needed a very tiny change you missed it
it's in the file name in the red
@echo off &setlocal enableDelayedExpansion

set "$srcFile=all.txt"
set "$indFile=c_list.txt"

set "i=" &for /f "usebackq delims=" %%? in (

"!$indFile!"

) do set /a i += 1 &set "$index!i!=%%~?"

set "c=" &for /f "usebackq delims=" %%? in (

"!$srcFile!"

) do (
if !c! lss !i! ( set /a c += 1 ) else set c = 1

for %%§ in ( "$index!c!" ) do >>"!%%~§!.txt" (echo(%%?)
)

pause
exit

Thanks alot :D

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: what is wrong with that code?

#6 Post by Squashman » 07 Aug 2012 09:18

Ed I gotta know what special character you are using for that last for loop. I don't recognize it.
You definitely get style points for that one! :D

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: what is wrong with that code?

#7 Post by Ed Dyreen » 07 Aug 2012 10:12

'
Just another key from my french keyboard :) àlors ça passe.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: what is wrong with that code? [SOLVED]

#8 Post by Ed Dyreen » 07 Aug 2012 13:08

'
This is better, it uses the modulus.

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$srcFile=all.txt"
set "$indFile=c_list.txt"

set "i=0" &for /f "usebackq delims=" %%? in (

       "!$indFile!"

) do   set "$index!i!=%%~?" &set /a i += 1

set "c=-1" &for /f "usebackq delims=" %%? in (

       "!$srcFile!"

) do   set /a c += 1, c %%= i &for %%§ in (

       "$index!c!"

) do   >>"!%%~§!.txt" (echo(%%?)

pause
exit

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: what is wrong with that code? [SOLVED]

#9 Post by foxidrive » 07 Aug 2012 13:24

This style of batch should also work:

Code: Select all

@echo off
setlocal DisableDelayedExpansion
< file2.txt (
   for /F "delims=" %%a in (file1.txt) do (
      set file2Line=
      set /P file2Line=
      set "file1Line=%%a"
      setlocal EnableDelayedExpansion   
     >>"!file2Line!" echo(!file1Line!
      endlocal
   )
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: what is wrong with that code? [SOLVED]

#10 Post by foxidrive » 07 Aug 2012 15:08

Here you are, this creates the two input files and you can test it.

Code: Select all

@echo off

(
echo abc
echo def
echo ghi
)>file1.txt

(
echo abc123.txt
echo def123.txt
echo ghi123.txt
)>file2.txt

setlocal DisableDelayedExpansion
< file2.txt (
   for /F "delims=" %%a in (file1.txt) do (
      set file2Line=
      set /P file2Line=
      set "file1Line=%%a"
      setlocal EnableDelayedExpansion   
     >>"!file2Line!" echo(!file1Line!
      endlocal
   )
)
pause

Ken Cline
Posts: 1
Joined: 07 Aug 2012 14:10

Re: what is wrong with that code? [SOLVED]

#11 Post by Ken Cline » 07 Aug 2012 15:46

foxidrive wrote:Here you are, this creates the two input files and you can test it.

Code: Select all

@echo off

(
echo abc
echo def
echo ghi
)>file1.txt

(
echo abc123.txt
echo def123.txt
echo ghi123.txt
)>file2.txt

setlocal DisableDelayedExpansion
< file2.txt (
   for /F "delims=" %%a in (file1.txt) do (
      set file2Line=
      set /P file2Line=
      set "file1Line=%%a"
      setlocal EnableDelayedExpansion   
     >>"!file2Line!" echo(!file1Line!
      endlocal
   )
)
pause


I ran the above code with two input files and got no outout. Unless some output is saved in !file2line! or !file1line!

I don't know what kind of output I should I look for. What does the code do?

No input, no output. What are we looking for? We start with nothing, look for nothing and find nothing.

Edit by foxidrive: remove inane comment.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: what is wrong with that code? [SOLVED]

#12 Post by foxidrive » 07 Aug 2012 16:02

See if you can try a bit harder to understand the aspects of the code, by reading the original post and finding out what it was meant to do.

But to help you here - this creates two files, and it takes one line from file1.txt and one line from file2.txt and then echoes the first line - into a file with the name that is taken from file2.txt, and it repeats until all the lines are used.

So run this batch version with some debugging lines.

Then examine file1.txt and file2.txt and see how they were used to create the extra files.



Code: Select all

@echo off

(
echo abc
echo def
echo ghi
)>file1.txt

(
echo abc123.txt
echo def123.txt
echo ghi123.txt
)>file2.txt

dir *.txt /b
pause

setlocal DisableDelayedExpansion
< file2.txt (
   for /F "delims=" %%a in (file1.txt) do (
      set file2Line=
      set /P file2Line=
      set "file1Line=%%a"
      setlocal EnableDelayedExpansion   
     >>"!file2Line!" echo(!file1Line!
      endlocal
   )
)
dir *.txt /b
pause

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: what is wrong with that code? [SOLVED]

#13 Post by Squashman » 07 Aug 2012 16:16

Your code works good Foxidrive.

Code: Select all

C:\Users\Squash\batch files\Test>dir /b
foxidrive.bat

C:\Users\Squash\batch files\Test>foxidrive.bat
Press any key to continue . . .

C:\Users\Squash\batch files\Test>dir /b
abc123.txt
def123.txt
file1.txt
file2.txt
foxidrive.bat
ghi123.txt

C:\Users\Squash\batch files\Test>

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: what is wrong with that code? [SOLVED]

#14 Post by Ed Dyreen » 07 Aug 2012 18:55

'
I misread the request, my code loops the name-index until all lines are processed.
abc0502 wrote:lines in all.txt files is equal to lines in the c_list.txt
each line in all.txt has a name in the c_list.txt file.
Use foxi's code.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: what is wrong with that code? [SOLVED]

#15 Post by abc0502 » 08 Aug 2012 03:17

Ed Dyreen wrote:'
I misread the request, my code loops the name-index until all lines are processed.
abc0502 wrote:lines in all.txt files is equal to lines in the c_list.txt
each line in all.txt has a name in the c_list.txt file.
Use foxi's code.

I don't understand, what do you mean.
I used your code altered it a bit so it use multi input files and name files, and it works
what i was talking about was that the two files will have equal line numbers if one has 6 lines the other will have the same lines 6 also.

Post Reply