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
369 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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
 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Update a registry value to every user hive 15 83
Batch Login Script - Update default homepage 8 50
Batch Files - Error Handling 4 27
To check for the file in a batch script 8 36
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

810 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