Identifying a specific character to split lines to another .txt

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Megsie
Posts: 13
Joined: 05 May 2016 11:06

Identifying a specific character to split lines to another .txt

#1 Post by Megsie » 05 May 2016 11:20

Hello everyone,
I'm new to batch scripts and hope I can properly communicate my needs in order to receive help. Here's what I'd like to do:
1) set file path: set "filepath=U:\My Documents\"
2) look at a .txt file in a specific location: set "ffile=%filepath%\Extracts\*.txt"

Ok...now the stuff I'm having trouble with.....
3) within this .txt file if a specific character is N, pull this line from the original and include it in a new .txt with "-N" added to it's name.....does this make sense?
This is what I've gotten so far...but I'm struggling.... :?
if !line:~195,1! = "N"

HELP PLEASE!

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

Re: Identifying a specific character to split lines to another .txt

#2 Post by foxidrive » 05 May 2016 11:26

Here's what I'd like to do:

2) look at a .txt file in a specific location: set "ffile=%filepath%\Extracts\*.txt"

The filename here is unclear.

Ok...now the stuff I'm having trouble with.....
3) within this .txt file if a specific character is N, pull this line from the original and include it in a new .txt with "-N" added to it's name.....does this make sense?


How are you getting the filename? I could make an educated guess, but coding is a specific kind of thing to the task, so guessing isn't too useful.

Megsie
Posts: 13
Joined: 05 May 2016 11:06

Re: Identifying a specific character to split lines to another .txt

#3 Post by Megsie » 05 May 2016 11:37

Maybe it would help if I shared more of that I originally had. I have a .txt file that I would reference to identify products I want to separate, but it's becoming too hard to maintain the identifiers in this .txt so I'm wanting to just look at a specific character field instead. In this character field, if "N" exists then it splits (instead of referencing the original identifiers.txt file).

Here's what I had (also names the files, sets the date, and moves the file... but thought I should include everything):

Code: Select all

setlocal enabledelayedexpansion

set "filepath=U:\My Documents\"

set "ffile=%filepath%\Extracts\*.txt"
set "fsearch=U:\My Documents\Separation_Script\Idenfitiers.txt"
  set TmpFile="%temp%.\tmp.vbs"
  echo>>%TmpFile% n=Now
  echo>>%TmpFile% With WScript
  echo>>%TmpFile% .Echo "set m1="   + monthname(month(n), true)
  echo>>%TmpFile% .Echo "set m2="   + monthname(month(n), false)
  echo>>%TmpFile% .Echo "set woy="  + CStr(datepart("ww", n))
  echo>>%TmpFile% .Echo "set year=" + CStr(Year(n))
  echo>>%TmpFile% .Echo "set yr="   + Right(Year(n),2)
  echo>>%TmpFile% .Echo "set month="+ Right(100+Month(n),2)
  echo>>%TmpFile% .Echo "set day="  + Right(100+Day(n),2)
  echo>>%TmpFile% .Echo "set hour=" + Right(100+Hour(n),2)
  echo>>%TmpFile% .Echo "set min="  + Right(100+Minute(n),2)
  echo>>%TmpFile% .Echo "set sec="  + Right(100+Second(n),2)
  echo>>%TmpFile% .Echo "set dow="  + WeekDayName(Weekday(n),1)
  echo>>%TmpFile% .Echo "set dow2=" + WeekDayName(Weekday(n))
  echo>>%TmpFile% .Echo "set iso="  + CStr(1 + Int(n-2) mod 7)
  echo>>%TmpFile% .Echo "set iso2=" + CStr(Weekday(n,2))
  echo>>%TmpFile% End With
  cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
  call "%temp%.\tmp.bat"
  del  "%temp%.\tmp.bat"
  del  %TmpFile%
  set TmpFile=

set "filenum=%day%%month%%year%"

set "num=0"

for %%z in ("%ffile%") do (
set "name="
set /p "name=" < "%%z"
set "name=!name:~42,3!

echo name is set to "!name!"
pause

set /a num=num+1


findstr /lg:"%fsearch%" "%%z">>"%filepath%\N\!name!-%filenum%-N.txt"
findstr /lvg:"%fsearch%" "%%z">>"%filepath%\Original\!name!-%filenum%-orig.txt"
)
move /y "%filepath%\N\*txt*" "%filepath%\N\"
move /y "%filepath%\Original\*txt*" "%filepath%\orig\"

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

Re: Identifying a specific character to split lines to another .txt

#4 Post by foxidrive » 05 May 2016 13:02

This section can be simplified as I've shown - and the num variable isn't being used.

Code: Select all

for %%z in ("%ffile%") do (
set "name=%%~nxz"
set "name=!name:~42,3!

echo name is set to "!name!"
pause

set /a num=num+1

findstr /lg:"%fsearch%" "%%z">>"%filepath%\N\!name!-%filenum%-N.txt"
findstr /lvg:"%fsearch%" "%%z">>"%filepath%\Original\!name!-%filenum%-orig.txt"
)



if a specific character is N, pull this line from the original and include it in a new .txt with "-N" added to it's name.....does this make sense?


Is the problem filtering the line itself? We'd need to see the line and know which part needs to be an N.

Megsie
Posts: 13
Joined: 05 May 2016 11:06

Re: Identifying a specific character to split lines to another .txt

#5 Post by Megsie » 05 May 2016 13:20

if a specific character is N, pull this line from the original and include it in a new .txt with "-N" added to it's name.....does this make sense?

Is the problem filtering the line itself? We'd need to see the line and know which part needs to be an N.



I can't include an actual line from my extract because it includes personal information (and I need to be considerate of privacy issues), but I've copied the line and replaced all characters with "ABCs" and "123s." I've included 2 lines as examples (one with an N in the 195 character space and one without). Also note, each line is 529 characters long...

ABCDEFGH-ABC,ABC ABCDE ABC AB1-A123-12 A1234567 ABCD1 12345678 1234 1 12 A1234567 A12345678 A12345678 ABCD 1234N12 ABCDEFGHIJKLM 123456 ABCDEF,ABCDEFGHI 12345 1 ABC ABCD ABCDEFGHI ABCDE ABCD-ABCD ABCDEF12345678 123456ABC1234 1234A1234567 1234 12ABC1234 1234A1234567 1234 1 12345678@12:34 A

ABCDEFGH-ABC,ABC ABCDE ABC AB1-A123-12 A1234567 ABCD1 12345678 1234 1 12 A1234567 A12345678 A12345678 ABCD 1234 12 ABCDEFGHIJKLM 123456 ABCDEF,ABCDEFGHI 12345 1 ABC ABCD ABCDEFGHI ABCDE ABCD-ABCD ABCDEF12345678 123456ABC1234 1234A1234567 1234 12ABC1234 1234A1234567 1234 1 12345678@12:34 A

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

Re: Identifying a specific character to split lines to another .txt

#6 Post by foxidrive » 05 May 2016 13:59

The N is at position 112


Privacy is fine, there's a description here of how data is best hidden
viewtopic.php?f=3&t=6108

Megsie
Posts: 13
Joined: 05 May 2016 11:06

Re: Identifying a specific character to split lines to another .txt

#7 Post by Megsie » 05 May 2016 14:22

Does this help? I think it stripped out a bunch of spaces when I posted my previous example...hopefully this works

Code: Select all

LASTNAME-FIRSTNAME             TYPEE      LOC  LC1-B123-01          A0000000   MEDA1      10000000 0400 1         10                          A0000000   A11000100      A11000100      AAUD   1234N10          PRODUCTNAME                                                                     680000         LASTNA,FIRSTNAME              11111 1 BAT  MOUT  INSTRUCTIONS GO HERE     RANDO RAND-RAND IDONTK00000000  040030MAR2016 0400A0100000 0400  30MAR2016 0400A0100000 0400  1                   20160330@13:26    P 


Nope...it keeps stripping out all the spaces. Any suggestions on how to better provide an example line? Or would you be able to help knowing that with all the spaces the "N" character is suppose to be in position 195?
Last edited by Squashman on 05 May 2016 20:29, edited 1 time in total.
Reason: MOD EDIT: Use code tags to format your data.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Identifying a specific character to split lines to another .txt

#8 Post by Compo » 06 May 2016 01:27

But there are 12 different N characters on that specific line in your example! Are you only looking always at character 195?

You appear not to be actually searching for the character N because you look like you're searching for a series of characters as found in a file, (identifiers.txt).

If you cannot provide us with a real example showing us the actual characters on the lines being searched and you haven't given us the proper identifiers, how do you expect us to be able to provide you with a solution?

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

Re: Identifying a specific character to split lines to another .txt

#9 Post by foxidrive » 06 May 2016 08:06

You're looking for the N in this term: Searching for the N is possible but the details are sketchy. The best and most reliable and simplest method will depend on the other parts of this term, and if this can be found at any other position in the line or the file.

Code: Select all

1234N10


From a programmer's perspective your question can't reliably be solved, because the term you are searching for or the N in that position, could be in any line in the file. We don't know how many lines are in the file, if there's 1 or 2000.

Look here and you'll find the kinds of things which make the job of answering for the volunteers much easier.

See here: viewtopic.php?f=3&t=6108

Megsie
Posts: 13
Joined: 05 May 2016 11:06

Re: Identifying a specific character to split lines to another .txt

#10 Post by Megsie » 06 May 2016 08:56

Ok, I'm going to try to explain what I'm asking for again as I feel my original request is being misinterpreted.

-I already know how to identify the .txt file I want the script to look at
-Within this .txt file if for any line, the 195th character = N then remove it from the original .txt file and place it on a new .txt file (effectively splitting the file into 2)
-NOTE: I cannot simply search every line for any "N" character, as the .txt file includes names (so if Nancy or Nicolas were to be included, it would incorrectly pull their lines). I need it to look specifically at character 195.
-I don't require a whole script to be written for me, just a line that will see if "N" exists in the 195 character spot. Then I can replace the "identifiers.txt" search line in my original script with this 195 character "N" search logic.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Identifying a specific character to split lines to another .txt

#11 Post by Compo » 06 May 2016 09:01

...but you're not looking for the character N, you are looking for any characters as diefined on lines of an identifiers.txt file aren't you?

Don't expect that a reply using N can be taken by you and then edited accordingly to work with whatever is hidden in that text file.

Megsie
Posts: 13
Joined: 05 May 2016 11:06

Re: Identifying a specific character to split lines to another .txt

#12 Post by Megsie » 06 May 2016 09:14

Compo wrote:...but you're not looking for the character N, you are looking for any characters as diefined on lines of an identifiers.txt file aren't you?

Don't expect that a reply using N can be taken by you and then edited accordingly to work with whatever is hidden in that text file.


The current script that I'm using is referencing that identifiers.txt file; this is no longer feasible to support so I need to remove that line from my script and replace it with something that will hopefully identify if there is an "N" in the 195th character spot for any line included in the .txt file. Also note, the "N" in character space represents "No" which is why I want to separate those lines from the others.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Identifying a specific character to split lines to another .txt

#13 Post by Compo » 06 May 2016 18:26

Here is a completely untested example, (please do not run it in your live environment without great care).

Code: Select all

@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
If Exist "U:\My Documents\" (Set filepath=U:\My Documents) Else (Exit/B)
If Exist "%filepath%\Extracts\*.txt" (Set ffile=%filepath%\Extracts\*.txt
   ) Else (Exit/B)

For /F "EOL=L" %%a In ('WMIc OS Get LocalDateTime') Do (For %%b In (%%a
   ) Do Set _dt=%%b)
Set "filenum=%_dt:~6,2%%_dt:~4,2%%_dt:~,4%"

For %%a In ("%ffile%") Do (Set "name=%%~na"
   Set "name=!name:~17,3!-%filenum%-"
   For /F "UseBackQ Tokens=*" %%b In ("%%a") Do (Set "line=%%b"
      If /I "!line:~194,1!" Equ "N" (>>"%filepath%\N\!name!N.txt" Echo=%%b
         ) Else (>>"%filepath%\Original\!name!orig.txt" Echo=%%b)))
I haven't included the Move commands, because frankly I didn't understand them.

Megsie
Posts: 13
Joined: 05 May 2016 11:06

Re: Identifying a specific character to split lines to another .txt

#14 Post by Megsie » 09 May 2016 09:43

IT WORKS! You have no idea how relieved and thankful I am! This will make things so much easier for me.

One little thing though (I hope it's little), the naming of the file isn't quite working the way I had hoped. In the script you provided I see

Code: Select all

Set "name=!name:~17,3!-%filenum%-"

In my original script file I was pulling the name from the 43 character x3 characters after

Code: Select all

set "name=!name:~42,3!

I tried changing the character identifiers in the script you provided, but it still leaves it blank (name looks like "-09052016-N" after script run).

Could you help trouble shoot the naming?

Megsie
Posts: 13
Joined: 05 May 2016 11:06

Re: Identifying a specific character to split lines to another .txt

#15 Post by Megsie » 09 May 2016 10:02

Don't worry about trying to adjust the name, I got it to work :D

Thank you so much Compo and Foxidrive for helping me with this!

Post Reply