-1

I'm super new to PowerShell, usually just using one line commands to get things done, but have been asked if I can take a batch of postscript files that are created from a mainframe and rename them based on the first line of that file then move them to a network location.

I've done a bit of research on google, but everything I've found talks about retrieving text from files and writing to a new file.

Any help would be greatly appreciated.

Edit:

It seems it will be easier if the text will be the last line of the file instead of the first. I've gotten it set so that I can do a single file name changed using the code below. Now just to get it setup to do all files in a directory.

$orgfilename = 'c:\postscript\test.PS'
$tempfilename = Get-Content $orgfilename | Select-Object -Last 1
$newfilename = $tempfilename.Substring(3)
move-item $orgfilename c:\newlocation\$newfilename'.ps' 

I added the .Substring(3) to ignore the " %" (space%) in the file.

NateL
  • 1
  • 1
  • `Get-Content -TotalCount 1` and `Rename-Item` and maybe `ForEach-Object` would be the cmdlets that would help you. If you get stuck update your question with what you tried. Welcome to SO! – Matt Feb 21 '15 at 01:53
  • You're probably going to need `Get-ChildItem` as well as the commands listed by @Matt – TheMadTechnician Feb 21 '15 at 02:16
  • 1
    The first line of a PostScript file will almost invariably be some variation of "%!PS-Adobe-3.0" It seems likely to me you are going to get an awful lot of name collisions. Given that most PostScript programs are machine-generated, the headers of these files tend to fall into a small (5-10) number of groups, all of whose members are identical in the first few kbytes. If you want some uniqueness, you probably do need to extract the 'text' from the PostScript program, rather than relying on the first few lines of the file content. – KenS Feb 21 '15 at 09:41

2 Answers2

0

Rename-Item testfile.xml -NewName (Get-Content testfile.xml | select -First 1)

Make sure there are no special characters like ? / \ ^ < > in the first line of your file.

0

This is what I ended up with. I added a filter just in case something other than the postscript file got added to the folder location.

 Get-ChildItem 'c:\postscript'  -FILTER  "*.ps" |  ForEach-Object { 
   $filename = Get-Content $_.FullName | Select-Object -Last 1
   $newfile = $filename.Substring(3) 
   move-item $_.FullName c:\newlocation\$newfile'.ps' } 

The biggest hurdle I had was to realize what needed to be piped and what did not.

NateL
  • 1
  • 1