Solved

Batch Script Modification Needed

Posted on 2012-12-25
4
479 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 54

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 54

Expert Comment

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

~bp
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

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…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 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