Link to home
Start Free TrialLog in
Avatar of Danny Kon
Danny KonFlag for Netherlands

asked on

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
  }
https://www.experts-exchange.com/questions/28649471/Why-is-this-script-not-running-from-powershell.html?anchorAnswerId=40705140#a40705140
test.txt
Avatar of Mark Bullock
Mark Bullock
Flag of United States of America image

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

Avatar of Danny Kon

ASKER

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
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

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
ASKER CERTIFIED SOLUTION
Avatar of Mark Bullock
Mark Bullock
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Yes, that's correct. Remove Write-Host and you will not see any messages on the console.