• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

Using Windows Batch or VBScript or Powershell, how can I remove an end of file character in text files?

I need to remove a small right arrow at the end of the data (an ASCII EOF marker) in a text file without removing any other data.

A script would need to look in a specific folder, ie c:\data\ and look at each .txt file and if it sees this end EOF marker then it needs to remove it and resave the file.
0
100questions
Asked:
100questions
  • 5
  • 5
1 Solution
 
Dan CraciunIT ConsultantCommented:
Test if this works for one file:
Get-Content "X:\path\file.txt" -Encoding Byte | ? {$_ -ne 0x1A} | Set-Content "X:\path\newfile.txt" -Encoding Byte

If it does, we can use this for the entire folder. It basically reads a file byte by byte and writes only those bytes not equal to 0x1A (or 26 decimal,  which is the code for ASCII EOF)

HTH,
Dan
0
 
100questionsAuthor Commented:
Is this to be inserted in a batch file?
If yes, I tried and it just blinks fast and nothing happens.
0
 
Dan CraciunIT ConsultantCommented:
Nope. It's a powershell script.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
100questionsAuthor Commented:
Ok, it works well!!
How can we modify it to look at all txt files in folders?
0
 
Dan CraciunIT ConsultantCommented:
$origFolder = "X:\your\path"
$newFolder = "X:\your\new\path"

$files = gci $origFolder -Recurse -filter "*.txt"
foreach ($file in $files) {
    Get-Content $file.FullName -Encoding Byte | ? {$_ -ne 0x1A} | Set-Content "$newFolder\$file" -Encoding Byte
}
0
 
100questionsAuthor Commented:
Thanks. Will this form new files on it's own and it's own naming convention?
0
 
Dan CraciunIT ConsultantCommented:
This will create new files in the newFolder folder. Basically it will copy the original files, minus the offending character.

That folder must exist, or the script will throw an error.
0
 
100questionsAuthor Commented:
If no files exist in the folder, will the script still run, but nothing will be created?
0
 
Dan CraciunIT ConsultantCommented:
yup
0
 
100questionsAuthor Commented:
May try this.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now