Move data and create new file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
foncesa
Posts: 42
Joined: 12 Sep 2013 00:09

Move data and create new file

#1 Post by foncesa » 18 May 2014 14:24

Hi,

I need to move the rows of data from my c:\main.txt file and create a new text file named based on three digits of the line. The newly created file to be saved in D:\Records\(particular folder). The folders on the last digits are there in D:\Records. The data in main.txt is numeric.

Data Sample (main.txt)
1154720520034900200434500900000000353000100099010 0
1155720520035000200434600900000000764600100099010 0
1205201422628018546001034000000061060044000099012 0
1305201422627928224009117000000015940044000069020 0

The above sample data will create 3 new files
(1) 010.txt and saved at D:\Records\010
1154720520034900200434500900000000353000100099010 0
1155720520035000200434600900000000764600100099010 0

(2) 012.txt and saved at D:\Records\012
1205201422628018546001034000000061060044000099012 0

(3) 020.txt and saved at D:\Records\020
1305201422627928224009117000000015940044000069020 0

Thanks in advance.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Move data and create new file

#2 Post by penpen » 18 May 2014 15:01

This may help:

Code: Select all

@echo off
setlocal enableDelayedExpansion

for /F "tokens=* usebackq delims=" %%a in ("main.txt") do (
   set "line=%%~a"
   set "location=D:\Records\!line:~-5,3!"
   set "file=!location!\!line:~-5,3!.txt"
   mkdir "!location!" 2>nul
   (echo(!line!)>>"!file!"
)
endlocal
exit /B 0

penpen

Edit: No debug verson & patched: Forgotten to create the subfolder.
Last edited by penpen on 18 May 2014 23:43, edited 1 time in total.

foncesa
Posts: 42
Joined: 12 Sep 2013 00:09

Re: Move data and create new file

#3 Post by foncesa » 18 May 2014 22:29

Hi,

I tried, but it does not create the text files & I need to save them in D:\Records\(particular folder)xxx.txt

(1) 010.txt and saved at D:\Records\010\010.txt

Thanks.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Move data and create new file

#4 Post by penpen » 18 May 2014 23:46

I've changed the code above:
I've forgotten to create the (sub)folders (i just have overseen this part of the task, sorry).
The above code is no debug version anymore (so it should create the files instead of displaying it).

penpen

foncesa
Posts: 42
Joined: 12 Sep 2013 00:09

Re: Move data and create new file

#5 Post by foncesa » 19 May 2014 00:07

Hi,

Thanks Penpen its perfect.

Thanks.

Post Reply