Solved

Modify Text File with Batch Script to Create Additional Files Based on Specific Text String in File

Posted on 2014-04-24
4
324 Views
Last Modified: 2014-05-23
I have a text file where I have a defined set of variables that appear in a fixed position in each text record.

The original file location and output location will always be a fixed location: N:\DownloadedFiles\

The data I am evaluating is in positions 18-23 of each row.

If positions 18-23 contain VA8, copy row into a new text file with the following naming convention: VA8_MI186_yyyymmddhhmm.txt (hhmm in military time)

If positions 18-23 contain OH252, copy row into a new text file with the following naming convention: MI186_UPD_OH252_yyyymmdd.txt

If positions 18-23 contain MI3, copy row into a new text file with the following naming convention: MI186_UPD_MI3_yyyymmdd.txt
0
Comment
Question by:sparker1970
  • 2
  • 2
4 Comments
 

Author Comment

by:sparker1970
ID: 40020212
Here is a HelperFile
HelperFile.txt
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40020382
This is a .cmd approach:
@echo off
setlocal EnableDelayedExpansion

set dt=!date:~-4,4!!date:~-10,2!!date:~-7,2!
set tm=!time:~0,2!!time:~3,2!

pushd N:\DownloadedFiles\
for /F "tokens=* delims=" %%L in (HelperFile.txt) do (
  set token=%%L
  set token=!token:~17,5!
  set target=
  if "!token!" == "  VA8" set target=VA8_MI186_%dt%%tm%.txt
  if "!token!" == "OH252" set target=MI186_UPD_OH252_%dt%.txt
  if "!token!" == "  MI3" set target=MI186_UPD_MI3_%dt%.txt
  if defined target (echo.%%L) >> !target!
)
popd

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40020503
... and a PowerShell one, with a slightly different algorithm:
set-location N:\DownloadedFiles\

$data = get-content HelperFile.txt

$data | ? { $_.Substring(17,5) -eq '  VA8' } | out-file "VA8_MI186_$(      get-date -format 'yyyyMMddhhmm').txt"
$data | ? { $_.Substring(17,5) -eq 'OH252' } | out-file "MI186_UPD_OH252_$(get-date -format 'yyyyMMdd'    ).txt"
$data | ? { $_.Substring(17,5) -eq '  MI3' } | out-file "MI186_UPD_MI3_$(  get-date -format 'yyyyMMdd'    ).txt"

Open in new window

0
 

Author Closing Comment

by:sparker1970
ID: 40087057
This worked perfectly. Sorry for taking so long to respond. I was out of the office for a few weeks and just getting caught up on everything. I was trying to stick with a basic text/batch file so I did not test the PowerShell solution but some time in the future I may as I just picked up the Windows PowerShell Cookbook...to read in my spare time lol.

Thanks again, worked 100%!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

758 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

20 Experts available now in Live!

Get 1:1 Help Now