Solved

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

Posted on 2014-09-25
10
402 Views
Last Modified: 2014-10-10
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
Comment
Question by:100questions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40344340
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
 

Author Comment

by:100questions
ID: 40344447
Is this to be inserted in a batch file?
If yes, I tried and it just blinks fast and nothing happens.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40344466
Nope. It's a powershell script.
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

Author Comment

by:100questions
ID: 40344481
Ok, it works well!!
How can we modify it to look at all txt files in folders?
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 40344522
$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
 

Author Comment

by:100questions
ID: 40344558
Thanks. Will this form new files on it's own and it's own naming convention?
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40344565
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
 

Author Comment

by:100questions
ID: 40344571
If no files exist in the folder, will the script still run, but nothing will be created?
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40344574
yup
0
 

Author Closing Comment

by:100questions
ID: 40373819
May try this.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question