Manipulating a text file from Windows batch script

We have  VERY old SNA server infrastructure with an A/S400 (just nod your head in agreement).

Every night a file gets copied to our local network share from the central HQ.

The file is a .spl printer spool file.

We now have another department that needs a copy of this data for other purposes.

We have started copying the spool file out of the nightly process.

The issue is that the file that is downloaded from the A/S400 always contains ALL historical data dating back 15+ years and the file now over 11MB and growing daily

We only need the most current data out of this file.

What the department has gotten in the habit of doing is
  • open the saved file
  • delete all data prior to today's date
  • save the file
  • import the edited file in to their application

What I want to do is to script opening the file an delete all data prior to today's date.

Is there a way any knows to do this from a Windows script?
LVL 32
nappy_dThere are a 1000 ways to skin the technology cat.Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun VermaakTechnical Specialist/DeveloperCommented:
Yes, post some sample data
Lee W, MVPTechnology and Business Process AdvisorCommented:
You could skip a set number of lines consistently and adjust periodically with a single command (the command below I tested on a file called "files.txt":

for /f "tokens=* skip=170000 delims=" %a in (files.txt) do @echo %a>>newfile.txt

In a batch file, double-up the %:

for /f "tokens=* skip=170000 delims=" %%a in (files.txt) do @echo %%a>>newfile.txt

skip is the number of lines to skip.

You could also do this with a vbscript and the file system object - open the file, look for a specific line, then start writing the file back out.
Pretty easy to do with PowerShell, but since you didn't give any hint at all how to identify "all data prior to today's date", here's only a generic way.
For a customized solution, you'll have to post some sample data.
$pattern is a regular expression and needs to be set to a pattern matching the line that actually indicates the start of "today's data"
$inputFile = "C:\Temp\in.spl"
$outputFile = "C:\Temp\out.spl"
$pattern = "Today's data starts $(Get-Date -Format 'yyyy-MM-dd')"

$copy = $false
Get-Content -Path $inputFile | ForEach-Object {
	If ($copy -or ($_ -match $pattern)) {
		$copy = $true
} | Set-Content -Path $outputFile

Open in new window

Sample data:
Discard line 1
Discard line 2
Discard line 3
Discard line 4
Today's data starts 2018-04-17
Keep line 1
Keep line 2
Keep line 3
Keep line 4

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
We really need to see a GOOD sample of the data file, please post one to this question.

Depending on the data you might be able to do this with a medium complex GREP type utility, or certainly BAT, VBS or PS1 are all viable approaches.  Providing the data sample will help a lot to guide solutions.

nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
Thanks and sorry it's taking me so long.   I am redacting the file as it contain a lot of sensitive info.

Will upload it later today.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.