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
354 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

24 Experts available now in Live!

Get 1:1 Help Now