Page 1 of 1

Run .AWK File To Format Data In All Files In A Location

Posted: 19 Jun 2012 13:50
by Anonimoouse
Hi,
First of all, I am a newbie to batch programming so please be easy on me. I have to apply a format.awk file on all the .DAT files located in a folder and generate the formatted files in a different location, but with word "awked" in the file name. Please help me.....

Re: Run .AWK File To Format Data In All Files In A Location

Posted: 19 Jun 2012 15:34
by Squashman
Are you saying that you are using a version of AWK that works on Windows and that Format.awk is an awk script file?

Re: Run .AWK File To Format Data In All Files In A Location

Posted: 19 Jun 2012 18:05
by Anonimoouse
I have an awk file named Format.AWK and it is supposed to format each of the source files[ex: File(1).DAT] and generate formatted file [FileAwked(1).DAT]

The usage of the Format.AWK to apply on the DAT file would be like below, but that is for one file and I have to do the same for a bunch of files in the source location.

awk -f Format.awk File(1).dat > FileAwked(1).dat

Re: Run .AWK File To Format Data In All Files In A Location

Posted: 19 Jun 2012 19:57
by foxidrive
Anonimoouse wrote:Hi,
I have to apply a format.awk file on all the .DAT files located in a folder and generate the formatted files in a different location, but with word "awked" in the file name.
Source Location has files:
File(1).DAT
File(2).DAT
File(3).DAT
I have to apply the awked file (Format.AWK) on each of the file above and generate formatted files as:
FileAwked(1).DAT
FileAwked(2).DAT
FileAwked(3).DAT


This is clear, and is probably related to the other thread you posted.

Do you still need to check if the file exists or is zero bytes?

Re: Run .AWK File To Format Data In All Files In A Location

Posted: 19 Jun 2012 21:26
by Anonimoouse
Yes, it is related to the other thread. I need to check if the file exists and if yes, whether it has any data.

Re: Run .AWK File To Format Data In All Files In A Location

Posted: 19 Jun 2012 21:49
by Aacini
Batch file below solve this topic:

Code: Select all

@echo off
for %%a in ("File(*).DAT") do (
   for /F "tokens=1,2 delims=(" %%b in ("%%a") do (
      awk -f Format.awk %%a > %%bAwked(%%c
   )
)

Batch file below process just not empty files:

Code: Select all

@echo off
for %%a in ("File(*).DAT") do (
   if %%~Za gtr 0 (
      for /F "tokens=1,2 delims=(" %%b in ("%%a") do (
         awk -f Format.awk %%a > %%bAwked(%%c
      )
   )
)

Re: Run .AWK File To Format Data In All Files In A Location

Posted: 20 Jun 2012 00:41
by foxidrive
This is based on Aacini's code but it will handle the destination folder and will only create the target file if it does not exist or is zero bytes. (untested):

Code: Select all

@echo off
set "dest=d:\target folder\here\"
for %%a in ("File(*).DAT") do (
 for /f "tokens=1,2 delims=(" %%b in ("%%a") do (
   if NOT exist "%dest%\%%bAwked(%%c" awk -f Format.awk "%%a" > "%dest%\%%bAwked(%%c"   
    for /F "delims=" %%z in ("%dest%\%%bAwked(%%c") do (
       if %%~zz EQU 0 awk -f Format.awk "%%a" > "%dest%\%%bAwked(%%c"
       )
    )
)