Solved

Batch Script Modification Needed

Posted on 2012-12-25
4
489 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 56

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 56

Expert Comment

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

~bp
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

626 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