Solved

Batch Script Modification Needed

Posted on 2012-12-25
4
470 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 51

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 51

Expert Comment

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

~bp
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

26 Experts available now in Live!

Get 1:1 Help Now