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
363 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
  • 5
  • 5
10 Comments
 
LVL 34

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 34

Expert Comment

by:Dan Craciun
ID: 40344466
Nope. It's a powershell script.
0
 

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 34

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

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

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 34

Expert Comment

by:Dan Craciun
ID: 40344574
yup
0
 

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Automate File Copy Tasks 8 96
"netsh firewall" or "netsh advfirewall firewall" 10 84
Vbscript 8 78
Issue with cURL on Windows 7 81
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A short film showing how OnPage and Connectwise integration works.

914 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now