Reading a txt file and create others

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
acsm99
Posts: 3
Joined: 17 Jan 2012 12:07

Reading a txt file and create others

#1 Post by acsm99 » 17 Jan 2012 12:27

Hello,

I need help, please.

I'm trying to read a txt file, something like this:
01dkdkdkkd
01dfksdfkdfksd
02 dffv fvfv dfv
03 frerf erferf efr
03 erferf erferfe

And i want to create new files named with the 2 first character of the line and put the rest of the line inside the file.

I try a lot of things and i just don't get it.

@echo off
for /F %%a in (prod.txt) do
(
set linha=%%a
echo %%a >>%linha:~0,2%mati.pc rem this create just one file name 01mati.pc with all the lines inside

echo %linha% rem this give me allways the last line why???
)


thanks in advance

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

Re: Reading a txt file and create others

#2 Post by Squashman » 17 Jan 2012 15:40

You need to use delayed expansion

acsm99
Posts: 3
Joined: 17 Jan 2012 12:07

Re: Reading a txt file and create others

#3 Post by acsm99 » 18 Jan 2012 03:42

I use
SETLOCAL ENABLEDELAYEDEXPANSION

and still doesn't work :(
I got the same result

acsm99
Posts: 3
Joined: 17 Jan 2012 12:07

Re: Reading a txt file and create others

#4 Post by acsm99 » 18 Jan 2012 04:07

Well,

I got it. I also replace de %linha% for !linha!

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

for /F "tokens=*" %%a in (prod.txt) do (

set linha=%%a
set nlinha=!linha:~0,2!
echo %%a >>!nlinha!mati.pc
echo !linha!
echo !nlinha!
)

ENDLOCAL

Post Reply