Avatar of Danny Kon
Danny Kon
Flag for Netherlands asked on

Why is this script not running from powershell

Bill Prew made a VBscript what i want to run from powershell but its not working,
If i run it from the prompt it works perfect.
The command i use in a unrestricted powershell environment
cscript.exe C:\temp\test.vbs

https://www.experts-exchange.com/questions/28648240/I-want-to-split-a-text-file-in-how-many-names-are-in-the-text-file-sorry-for-the-bad-explanation.html?anchorAnswerId=40704369#a40704369

Thanks for helping

Danny
Scripting LanguagesPowershellVB Script

Avatar of undefined
Last Comment
Danny Kon

8/22/2022 - Mon
Qlemo

Is that all you use? Because it should not make any difference whether you run the VBS from a cmd or powershell prompt ... Doesn't it perform anything, are you getting an error message, or what happens?

Anyway, I would use a PS script here. You should have insisted on one in your prior question ;-).
This should do the same as the VBS code, it is using almost the same logic:
$FileIn = 'test.txt'
if (!(Test-Path $FileIn))
{
  Write-Error "ERROR: Input file `"$FileIn`" does not exist."
  return
}

$header = Get-Content $FileIn | select -First 2
Get-Content $FileIn | Select -Skip 2 | ? { $_ } |
  % {
    $line = $_ -replace "0:00:00", "       " 
    $Client = $line.SubString(20,50).Trim()
    if (!(Test-Path $client+'.txt')) { $line = "$header`r`n$line" }
    $line | Out-File -Append $client+'.txt'
  }

Open in new window

Danny Kon

ASKER
Olemo,

Because i tested you script i see what is the problem, i expected the output will go to c:\temp but the output files are in c:\windows\system32

There is some difference between the script from Bill Brew and yours
your output shows a + sign after the name eg Cafe Engel+, Cafe Tetra+ and there is a file
called ----------+.txt and is empty .
Can you also make that i can adjust the output directory?

Thanks

Danny
Qlemo

None of the scripts (natively) supplies a path, so the result files will be in the current working directory you call the script in.

One approach is to first change the current directory to the one you want the files to be created in, using set-location c:\temp (the cmdlet alias is cd).

Another approach is that we use the path info from the input file, and create the files in that path. I'll take this one:
$FileIn = 'C:\Temp\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`r`n$line" }
    $line | Out-File -Append $FileOut
  }

Open in new window

This also resolves the issue with '-------' and the added plus in the file names. (I hate PowerShell trying to do smart things with strings while I don't want it to.)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Danny Kon

ASKER
Olemo,

I don't know what i am doing wrong here? I don't get any output but also no error

 Thanks

Danny
Qlemo

The files are created in the Temp folder (where the input file is located). Did you check that?
Danny Kon

ASKER
Olemo

Yes i did but nothing ??

Danny
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Danny Kon

ASKER
Olemo,

Works perfect thanks so much

Danny