Powerhell adjust text file to read postcode and generate time

Olemo created the underneath powershell script but i want to adjust it that its checking the postcode in the text file and that it will make 2 directories
First directory are all the postcodes 1011 en 1012
2nd directory are the rest of the postcode's
Attached the text file

Thanks Danny
----
$FileIn = 'C:\Temp\EE\test.txt'
if (!(Test-Path $FileIn))
{
  Write-Error "ERROR: Input file `"$FileIn`" does not exist."
  return
}

$path = split-path -parent $FileIn
$header = Get-Content $FileIn | ? { $_ } | select -First 2
Get-Content $FileIn | ? { $_ } | Select -Skip 2 |
  % {
    $line   = $_ -replace "0:00:00", "       "
    $FileOut= $path + '\' + $line.SubString(20,50).Trim() + '.txt'
    if (!(Test-Path $FileOut)) { $line = @($header, $line) }
    $line | Out-File -Append $FileOut
  }
http://www.experts-exchange.com/Programming/Languages/Scripting/Q_28649471.html#a40705140
test.txt
LVL 6
Danny KonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BullockQA EngineerCommented:
Something like this should do it. You didn't have 1011 or 1012 in your input data though, so you'll only see files in the Second directory.
$FileIn = 'C:\Temp\EE\test.txt'
 if (!(Test-Path $FileIn))
 {
   Write-Error "ERROR: Input file `"$FileIn`" does not exist."
   return
 }

 $path = split-path -parent $FileIn 
 $header = Get-Content $FileIn | ? { $_ } | select -First 2
 Get-Content $FileIn | ? { $_ } | Select -Skip 2 | 
   % {
     $line   = $_ -replace "0:00:00", "       " 
	 $postcode = $line.Substring(72,5)
	 If (($postcode -eq "1011") -or ($postcode -eq "1012")) {
	    $directory = "First"
	 }
	 Else
	 {
		$directory = "Second"
	 }
     $FileOut= $path + '\' + $directory + '\' + $line.SubString(20,50).Trim() + '.txt'
     if (!(Test-Path $FileOut)) { $line = @($header, $line) }
     $line | Out-File -Append $FileOut
   }

Open in new window

0
Danny KonAuthor Commented:
Mark,

That's sloppy work sorry for that, i will test what you make and i put the correct test file.

Thanks for helping

Danny
test.txt
0
Mark BullockQA EngineerCommented:
You data is hard to parse because it looks like it's fixed position, but some characters, like accented e take two bytes.

Try this.

 
$FileIn = 'C:\Temp\EE\test.txt'
 if (!(Test-Path $FileIn))
 {
   Write-Error "ERROR: Input file `"$FileIn`" does not exist."
   return
 }

 $path = split-path -parent $FileIn 
 $header = Get-Content $FileIn | ? { $_ } | select -First 2
 Get-Content $FileIn | ? { $_ } | Select -Skip 2 | 
   % {
     $line   = $_ -replace "0:00:00", "       " 
	 $indexPostcode = 84
	 if ($line.Substring($indexPostcode,1) -eq ' ') {
		$indexPostcode++
	 }
	 $postcode = $line.Substring($indexPostcode,4)
	 If (($postcode -eq "1011") -or ($postcode -eq "1012")) {
	    $directory = "First"
	 }
	 Else
	 {
		$directory = "Second"
	 }
     $FileOut= $path + '\' + $directory + '\' + $line.SubString(20,50).Trim() + '.txt'
     if (!(Test-Path $FileOut)) { $line = @($header, $line) }
     $line | Out-File -Append $FileOut
   }

Open in new window

0
Defend Against the Q2 Top Security Threats

Were you aware that overall malware worldwide was down a surprising 42% from Q1'18? Every quarter, the WatchGuard Threat Lab releases an Internet Security Report that analyzes the top threat trends impacting companies worldwide. Learn more by viewing our on-demand webinar today!

Danny KonAuthor Commented:
Mark,

The output goes direct to the second directory and nothing is going to the first its also hard for me to see where it goes wrong because i don't see what is happening (is it possible in powerhell to see what its doing (like remove the @echo off in a dosscript?)) i dont get any errors.

Thanks for your help

Danny
0
Mark BullockQA EngineerCommented:
This version will print the data for each line.

You may have to change the indexes used to get the substrings depending on whether the characters are double-byte. Again, it's hard to parse because the data in the input file appears to be in fixed positions, but it's not.

$FileIn = 'C:\Temp\EE\test.txt'
 if (!(Test-Path $FileIn))
 {
   Write-Error "ERROR: Input file `"$FileIn`" does not exist."
   return
 }

 $path = split-path -parent $FileIn 
 $header = Get-Content $FileIn | ? { $_ } | select -First 2
 Get-Content $FileIn | ? { $_ } | Select -Skip 2 | 
   % {
     $line   = $_ -replace "0:00:00", "       " 
	 $indexPostcode = 84
	 if ($line.Substring($indexPostcode,1) -eq ' ') {
		$indexPostcode++
	 }
	 $postcode = $line.Substring($indexPostcode,4)
	 If (($postcode -eq "1011") -or ($postcode -eq "1012")) {
	    $directory = "First"
	 }
	 Else
	 {
		$directory = "Second"
	 }
	 $name = $line.SubString(20,50).Trim()
	 $postcode + '|' + $directory + '|' + $name | Write-Host
     $FileOut= $path + '\' + $directory + '\' + $name + '.txt'
     if (!(Test-Path $FileOut)) { $line = @($header, $line) }
     $line | Out-File -Append $FileOut
   }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Danny KonAuthor Commented:
Mark,

Thanks for keep hanging in there(i hope this is correct English)I am learning a lot from this kind of solutions
I changed the $indexPostcode to 73 and my output looks perfect (different test file)
If i am correct i only have to remove the | write-host to remove the output to the screen?

Again thanks for your help

Danny

1011|First|8888 Slijterij verkopen      Binnen Bantammerstra
1012|First|A-Fusion Food & Drinks       Zeedijk 130
1012|First|A-Fusion Food & Drinks       Zeedijk 130
1012|First|A-Fusion Food & Drinks       Zeedijk 130
1012|First|A-Fusion Food & Drinks       Zeedijk 130
1012|First|A-Fusion Food & Drinks       Zeedijk 130
1012|First|A-Fusion Food & Drinks       Zeedijk 130
1012|First|A-Fusion Food & Drinks       Zeedijk 130
1012|First|A-Fusion Food & Drinks       Zeedijk 130
1012|First|A-Fusion Food & Drinks       Zeedijk 130
1012|First|A-Fusion Food & Drinks       Zeedijk 130
1012|First|Black Tiger                  Oudezijds Achterburg
1012|First|Black Tiger                  Oudezijds Achterburg
1012|First|Black Tiger                  Oudezijds Achterburg
1072|Second|Café Anita's                 Ferdinand bol 158
1072|Second|Café Anita's                 Ferdinand bol 158
1072|Second|Café Anita's                 Ferdinand bol 158
1072|Second|Café Anita's                 Ferdinand bol 158
1072|Second|Café Anita's                 Ferdinand bol 158
1012|First|Cafe Corso                   Oudezijds Achterburg
1012|First|Cafe Corso                   Oudezijds Achterburg
1012|First|Cafe Corso                   Oudezijds Achterburg
1012|First|Cafe Corso                   Oudezijds Achterburg
1012|First|Cafe Corso                   Oudezijds Achterburg
1012|First|Cafe Corso                   Oudezijds Achterburg
1012|First|Cafe Corso                   Oudezijds Achterburg
1012|First|Cafe Corso                   Oudezijds Achterburg
1012|First|Cafe Corso                   Oudezijds Achterburg
1012|First|Cafe Corso                   Oudezijds Achterburg
1011|First|Cafe Cotton Club             Nieuwmarkt 5 H
1011|First|Cafe Cotton Club             Nieuwmarkt 5 H
1014|Second|Cafe Dansen in de nacht      Nieuwezijds Kolk 12
1012|First|Cafe de Hartjes              Nieuwebrugsteeg 25
1012|First|Cafe de Hartjes              Nieuwebrugsteeg 25
1055|Second|Café de Zevende Hemel        Hofwijckstraat 43
1055|Second|Café de Zevende Hemel        Hofwijckstraat 43
1055|Second|Café de Zevende Hemel        Hofwijckstraat 43
1055|Second|Café de Zevende Hemel        Hofwijckstraat 43
1055|Second|Café de Zevende Hemel        Hofwijckstraat 43
1012|First|Cafe Drink 'n Sink BV        Warmoestraat 58
1012|First|Cafe Engel                   Zeedijk 21
1012|First|Cafe Engel                   Zeedijk 21
1018|Second|Cafe Kadijk                  Kadijksplein 5
1018|Second|Cafe Kadijk                  Kadijksplein 5
1018|Second|Cafe Kadijk                  Kadijksplein 5
1018|Second|Cafe Kadijk                  Kadijksplein 5
1018|Second|Cafe Kadijk                  Kadijksplein 5
1018|Second|Cafe Kadijk                  Kadijksplein 5
1018|Second|Cafe Kadijk                  Kadijksplein 5
1018|Second|Cafe Kadijk                  Kadijksplein 5
1011|First|Café Stevens Nieuwmarkt B.V. Geldersekade 123
1011|First|Café Stevens Nieuwmarkt B.V. Geldersekade 123
1011|First|Café Stevens Nieuwmarkt B.V. Geldersekade 123
1011|First|Café Stevens Nieuwmarkt B.V. Geldersekade 123
1011|First|Café Stevens Nieuwmarkt B.V. Geldersekade 123
1011|First|Café Stevens Nieuwmarkt B.V. Geldersekade 123
1011|First|Café Stevens Nieuwmarkt B.V. Geldersekade 123
1011|First|Café Stevens Nieuwmarkt B.V. Geldersekade 123
1011|First|Café Stevens Nieuwmarkt B.V. Geldersekade 123
1011|First|Café Stevens Nieuwmarkt B.V. Geldersekade 123
1011|First|Café Stevens Nieuwmarkt B.V. Geldersekade 123
0
Mark BullockQA EngineerCommented:
Yes, that's correct. Remove Write-Host and you will not see any messages on the console.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.