?
Solved

Batch Script Modification Needed

Posted on 2012-12-25
4
Medium Priority
?
498 Views
Last Modified: 2013-01-03
I have a script working great.  However the script and the 'filelist.txt' file will reside in:

C:\Scripts

While the data files in the 'filelist.txt' file will reside in

C:\Data

So the resulting files will also reside in:

C:\Data

Do I just put the whole path in the 'filelist.txt' file or do I need to change script?


@echo off
setlocal EnableDelayedExpansion

REM get a file name and the YEAR column
for /F "tokens=1,2 delims=|" %%A in (filelist.txt) do call :Extract %%A %%B

exit /b

:Extract FileName YearColNo
set filename=%1
set colno=%2

@echo Processing file %filename%

REM Get the header line
set head=
for /F "usebackq delims=" %%F in ("%filename%") do if not defined head set head=%%F

REM Construct RegEx for matching the correct year column
set pat=
for /L %%L in (2,1,%colno%) do set "pat=!pat![^^|]*|"
set "pat=^^!pat!2004^|"

> 2004_%filename%     echo,!head!
>>2004_%filename%     findstr /R "%pat%" %filename%
REM Head will be added automatically
REM > Non_2004_%filename% findstr /V /R "%pat%" %filename%

exit /b
0
Comment
Question by:elwayisgod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 38720635
You will run into problems near the end of the script if you put full pathnames into the 'filelist.txt' file.

The script appears to create new files prefixed with "2004_", and appends data to them. This will mess up if you use a full path because it will try to create a file that looks like this:   "2004_C:\Data\somefile.txt", which isn't what you want.

The script will look in the "current directory" for any file operations that use a relative path, so it is easier if you CD to C:\Data inside your script, and then use relative file names inside of the 'filelist.txt'. If you do this, you'll need to make sure the FOR loop accesses "filelist.txt" using an absolute path, since it does not reside in C:\Data.

Example:

@echo off
setlocal EnableDelayedExpansion

cd C:\DATA

REM get a file name and the YEAR column
for /F "tokens=1,2 delims=|" %%A in (C:\Scripts\filelist.txt) do call :Extract %%A %%B

exit /b

..... rest of your script ....
0
 
LVL 57

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 38726919
Assuming the files listed in filelist.txt do not have the full path specified, but are just the file names, then these changes should take care of things.

@echo off
setlocal EnableDelayedExpansion

REM Define file and directory to work with
set FileList=C:\Scripts\filelist.txt
set DataDir=C:\Data

REM get a file name and the YEAR column
for /F "usebackq tokens=1,2 delims=|" %%A in ("%FileList") do call :Extract %%A %%B

exit /b

:Extract [FileName] [YearColNo]
set filename=%~1
set colno=%~2

@echo Processing file "%DataDir%\%filename%"

REM Get the header line
set head=
for /F "usebackq delims=" %%F in ("%DataDir%\%filename%") do if not defined head set head=%%F

REM Construct RegEx for matching the correct year column
set pat=
for /L %%L in (2,1,%colno%) do set "pat=!pat![^^|]*|"
set "pat=^^!pat!2004^|"

> "%DataDir%\2004_%filename%"     echo,!head!
>>"%DataDir%\2004_%filename%"     findstr /R "%pat%" "%DataDir%\%filename%"
REM Head will be added automatically
REM > "%DataDir%\Non_2004_%filename%" findstr /V /R "%pat%" "%DataDir%\%filename%"

exit /b

Open in new window

~bp
0
 

Author Comment

by:elwayisgod
ID: 38730884
Back tomorrow to try and resolve.
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 38742640
Glad that helped, thanks.

~bp
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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 …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

719 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