Solved

Batch Script Modification Needed

Posted on 2012-12-25
4
475 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
  • 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 53

Accepted Solution

by:
Bill Prew earned 500 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 53

Expert Comment

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

~bp
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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