1

Have this code:

gci D:\Files\Folder |
    sort LastWriteTime | select -Last 1 |
    foreach-object {$line -replace "\<", ""}

Not working. Tried many variations. Need to replace the "<" character in the file last modified in Folder. Managed to have the correct file selected and written to powershell console. Just cannot remove the "<" character from the file with LastWriteTime.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user2743693
  • 33
  • 1
  • 5
  • 1
    possible duplicate of [string replace file content with powershell](http://stackoverflow.com/questions/17144355/string-replace-file-content-with-powershell) – alroc May 23 '14 at 14:49
  • above referenced link as possible duplicate is for a static path to a specific file. There will be 7 files referenced over a 30 minute period. Need dynamic function. – user2743693 May 23 '14 at 15:10
  • The meat of your problem isn't finding the file(s), it's doing the replacement itself. The technique linked will work for you, you just have to adapt *how* you're referencing the file. – alroc May 23 '14 at 16:05
  • The meat of my problem is that I can do it with a static path and specific file. Can't do it dynamically. This works: (Get-Content D:\Files\Files_To_TIPWEB\astclasses.txt) | Foreach-Object {$_ -replace "\<", ""} | Set-Content D:\Files\Files_To_TIPWEB\astclasses.csv But it does me no good as it is not dynamic. – user2743693 May 23 '14 at 16:14
  • Appreciate your feedback. Also looking at a vbs alternative to powershell script. – user2743693 May 23 '14 at 16:30

2 Answers2

2

Windows doesn't allow the < character in file names so I guess you want to modify the file contents removing all occurrences. If so, there are many ways to do that. Example:

# Getting the name of the last modified file.
$file_name = Get-ChildItem D:\Files\Folder | Sort-Object LastWriteTime `
    | ? { ! $_.PSIsContainer } | Select-Object -Last 1 | % {$_.FullName }
# Reading the file into a single string.
$string = Get-Content $file_name | Out-String
# Modifying the string and writing the output back to the file.
$string -replace "<", "" | Out-File $file_name 

The problem with your initial code is that $line is not defined anywhere. You need to read text from the file first.

Alexander Obersht
  • 3,215
  • 2
  • 22
  • 26
  • trying to mod the content of a text file that is dynamically selected based on being the most recently modified file in a folder. Want to take the "<" character and make it nothing within a text file. – user2743693 May 23 '14 at 16:17
  • Ok, awesome, that works wonderfully! However, instead of decreasing the file size by 750KB, the file size almost doubles. File size before your ps script = 350KB. File size after mod = 552KB. When I run the static path ps script it drops the file size down to 272KB. Row count is the same. . Thanks! – user2743693 May 23 '14 at 16:41
  • It's outputting in a different character set. Use `-encoding ascii` and it'll probably shrink to what you're expecting. – alroc May 23 '14 at 16:44
  • That may be what is creating the issue in the first place. Using a Talend job to generate the file. Currently generating file with vbs script. DB2 LUW. Attempted "<" removal with Talend, decided to just create a ps or vbs script and call via the Talend command line object. So Thanks! Have a great weekend! -Edit to previous post: The file size is decreasing by 75KB not 750KB. – user2743693 May 23 '14 at 17:59
  • 1
    Note that in PSv3+, you can use **Get-ChildItem**'s **-File** switch instead of piping through `?{! $_.PSIsContainer}`. Also, although it doesn't apply in this case, you can avoid annoyances related to how .NET methods handle paths (using a different working directory than PowerShell) by using native PowerShell to read the file into a single string: `$string = gc $file_name | Out-String`. – Adi Inbar May 23 '14 at 19:28
0

If you're trying to replace the < in the filename then this should work:

foreach-object {$_.Name.Replace("<", "")}

To edit the contents of the file you could do this:

$file = gci D:\Files\Folder | sort LastWriteTime | select -Last 1
$temp = $file | gc | foreach-object {$_.Replace("<", "")}
$temp | Out-File $file.FullName
Tim Ferrill
  • 1,648
  • 1
  • 12
  • 15
  • Trying to replace the < within the text file that was last modified in Folder. Do not wish to modify the file name. I can have the < within the text file be any other character that will not appear within the 7 data extracts. – user2743693 May 23 '14 at 16:11
  • Added another option to change the contents. It's more or less the same as what Alexander offered. – Tim Ferrill May 23 '14 at 16:39
  • Will add to personal ps library also. Appreciate your time. – user2743693 May 23 '14 at 18:06
  • Marking an answer as good and/or up-voting is always a nice gesture if it works for you or puts you on the right path... – Tim Ferrill May 23 '14 at 18:10
  • Don't have enough reputation points to up vote. Since I used Alexander's code, green check there. Have an awesome weekend! Thanks again. – user2743693 May 23 '14 at 18:44