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?
 
Mark BullockQA Engineer IIICommented:
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
 
Mark BullockQA Engineer IIICommented:
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
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
Mark BullockQA Engineer IIICommented:
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
 
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
 
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 Engineer IIICommented:
Yes, that's correct. Remove Write-Host and you will not see any messages on the console.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.