IPO - batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
xThule
Posts: 3
Joined: 25 Jan 2017 18:02

IPO - batch

#1 Post by xThule » 25 Jan 2017 18:26

Hi guys,

i've got a little problem and i dont know where i failed cuz every tutorial i read didnt answer my specific question

everything works alone but together only the first part works

the plan:
take file 1 (file.txt)
read the lines
cut them
write them in file 2 (file_2.txt)


line syntax 123456789012testxxtest123456789012
i only want the "testxxtest" part

i got large files with up to 10000 lines with syntaxes look like the above
numbers in the front numbers in the back and a alphanumerical code in the middle
for the following steps i only need the code in the middle
re writing the existing output from the db is not possible
so i have to take the db output, manually change the whole txt (delete the unnecessary characters)
and then upload it again for further use


if necessary re write my batch code completely but please comment it =)

Code: Select all

@echo off
title EVA

FOR /F %%i in (file.txt) do (
set str=%%i
set str=%str:~11,-12%
echo.%str%>>"file_2.txt"
)
echo EVA has done her work
pause

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

Re: IPO - batch

#2 Post by Squashman » 25 Jan 2017 20:25

You said the code you need to keep is alphanumeric meaning it contains numbers and letters. How are we supposed to know where to start and stop?

Regardless you are inside a code block so you need to use delayed expansion.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: IPO - batch

#3 Post by Aacini » 25 Jan 2017 23:47

You have not explained what is your problem, nor what is the format of the input file (an example is certainly not a description), and this phrase: "everything works alone but together only the first part works" is not clear at all!

However, if you want to eliminate the first 12 and last 12 characters from each line, then your code is close:

Code: Select all

@echo off

rem Next line enable Delayed Expansion, that is needed to get the value of a variable
rem that was modified inside a FOR loop (using exclamation-marks instead of percent-signs)
setlocal EnableDelayedExpansion

title EVA

(FOR /F %%i in (file.txt) do (
set str=%%i
echo !str:~11,-12!
)) >"file_2.txt"
rem It is much faster to execute just one redirection for the whole file,
rem than execute one redirection *for each line*

echo EVA has done her work
pause

If this is not what you want, then you have to explain your problem more clearly...

Antonio

xThule
Posts: 3
Joined: 25 Jan 2017 18:02

Re: IPO - batch

#4 Post by xThule » 26 Jan 2017 04:40

Squashman wrote:You said the code you need to keep is alphanumeric meaning it contains numbers and letters. How are we supposed to know where to start and stop?

Regardless you are inside a code block so you need to use delayed expansion.


ok maybe it wasnt clear enough the syntax is everytime the same 12 numbers then the code and then again 12 numbers

Aacini wrote:You have not explained what is your problem, nor what is the format of the input file (an example is certainly not a description), and this phrase: "everything works alone but together only the first part works" is not clear at all!

However, if you want to eliminate the first 12 and last 12 characters from each line, then your code is close:


ok sorry my problem is that it doesnt cut the lines and only writes echo is off in the files

the file i work with is a simple txt that i got from the db with lines explained above and i have to reupload a simple txt
the program in the background does the rest but i cant change the db output atm so i needed this workaround

but your code solved my problem

last question it doesnt matter how long the file is ? (line count)

thx for the fast response
thx for the help
and sorry for that it wasnt clear enough explained

rodrigolima
Posts: 21
Joined: 19 Jul 2013 11:35
Location: Brazil

Re: IPO - batch

#5 Post by rodrigolima » 26 Jan 2017 17:26

Hello xThule

try this:

Code: Select all

@echo off
cls

:: Delete Files before start process
if exist "%~dp0output.txt" del "%~dp0output.txt"
if exist "%~dp0pipeline.txt" del "%~dp0pipeline.txt"

SetLocal EnableDelayedExpansion

::generate text file to get file names
dir /b text*.txt>pipeline.txt

:: Loop through Files
for /f %%a in (pipeline.txt) do (
echo Current File: %~dp0%%a
::Get File Content(Just 1 Line)
set /p str=<%~dp0%%a

:: Replace [0-9] to Empty
set str=!str:0=!
set str=!str:1=!
set str=!str:2=!
set str=!str:3=!
set str=!str:4=!
set str=!str:5=!
set str=!str:6=!
set str=!str:7=!
set str=!str:8=!
set str=!str:9=!

:: Get Current File Original Texto NumbersTextNumbers
set /p str1=<%~dp0%%a

:: Generate Log
:: FileName#BeforeReplace#AfterReplace(Text Extraction)
echo %%a#!str1!#!str!>>output.txt
)
endLocal

goto:eof


To test script, create 2 text files:

text.txt
Content:123456789012Greece123456789012

text1.txt
Content:123456789012Rome123456789012

After execution, open output.txt file and check the results.

I hope that script helps you in your problem.

See you

xThule
Posts: 3
Joined: 25 Jan 2017 18:02

Re: IPO - batch

#6 Post by xThule » 26 Jan 2017 17:42

thx for the input rodrigolima

beside the fact that Aacini already solved my problem the easiest way possible

your code will not work for me because i've got an alphanumerical code in the middle
the 10 symbols in the middle are numbers AND characters so i cant delete only numbers that would make my codes unusable for the following steps but thank you anyway =)

Post Reply