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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Anonimoouse
Posts: 10
Joined: 19 Jun 2012 13:45

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

#1 Post by Anonimoouse » 19 Jun 2012 13:50

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.....
Last edited by Anonimoouse on 22 Jun 2012 11:23, edited 1 time in total.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

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

#2 Post by Squashman » 19 Jun 2012 15:34

Are you saying that you are using a version of AWK that works on Windows and that Format.awk is an awk script file?

Anonimoouse
Posts: 10
Joined: 19 Jun 2012 13:45

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

#3 Post by Anonimoouse » 19 Jun 2012 18:05

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#4 Post by foxidrive » 19 Jun 2012 19:57

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?

Anonimoouse
Posts: 10
Joined: 19 Jun 2012 13:45

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

#5 Post by Anonimoouse » 19 Jun 2012 21:26

Yes, it is related to the other thread. I need to check if the file exists and if yes, whether it has any data.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#6 Post by Aacini » 19 Jun 2012 21:49

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
      )
   )
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#7 Post by foxidrive » 20 Jun 2012 00:41

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"
       )
    )
)

Post Reply