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.....
Run .AWK File To Format Data In All Files In A Location
Moderator: DosItHelp
-
- Posts: 10
- Joined: 19 Jun 2012 13:45
Run .AWK File To Format Data In All Files In A Location
Last edited by Anonimoouse on 22 Jun 2012 11:23, edited 1 time in total.
Re: Run .AWK File To Format Data In All Files In A Location
Are you saying that you are using a version of AWK that works on Windows and that Format.awk is an awk script file?
-
- Posts: 10
- Joined: 19 Jun 2012 13:45
Re: Run .AWK File To Format Data In All Files In A Location
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
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
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?
-
- Posts: 10
- Joined: 19 Jun 2012 13:45
Re: Run .AWK File To Format Data In All Files In A Location
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
Batch file below solve this topic:
Batch file below process just not empty files:
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
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"
)
)
)