CMD Script Traversal and Find Files of Certain Type

I am writing a cmd file that converts *.log files to *.csv on a Windows Server 2016 machine.

How do I have the script traverse the folder structure and find just the files of "*.log" type and then run the conversion process from *.log five to *.csv?

Thanks for your help
TessandoIT AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
NVITCommented:
Add this to your  .Bat file.
Adjust sourcedir and yourcommand as needed.
It's in debug mode for visual confirmation. To run it for real, remove the ECHO prefix when satisfied.

set sourcedir=c:\dirname

for %%a in ('dir /bs "%sourcedir%\*.log") do (
  ECHO yourcommand "%%a"
  pause
)

Open in new window

0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
NVIT script has a few bugs:

set sourcedir=c:\dirname

for %%a in ('dir /b /s "%sourcedir%\*.log"') do (
  ECHO yourcommand "%%a"
  pause
)

Open in new window

0
 
TessandoIT AdministratorAuthor Commented:
Thank you for the script and thank you for the correction. I appreciate it.

The original script I was running is a FORFILES command, in order to convert *.log files to *.csv files.

Would either of you have a better suggestion than the following for the conversion script?


Open in new window

forfiles /s /C "cmd /c echo @date, @time, @s-ip, @cs-method, @cs-uri-stem, @cs-uri-query, @s-port, @cs-username, @c-ip, @cs(User-Agent), @cs(Referer),@sc-status, @sc-substatus, @sc-win32-status, @time-taken" > %Timestamp%_Log.csv "%%a"

It generates a file, but unlike running just the raw FORFILES command, there is no data in the file.

Thank you again for your attention. I greatly appreciate it.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
NVITCommented:
Whoops!
Thanks, Lee.
0
 
NVITCommented:
Currently, your forfiles line echos echos the @date, @time, @s-ip, @cs-method, @cs-uri-stem, @cs-uri-query, @s-port, @cs-username, @c-ip, @cs(User-Agent), @cs(Referer),@sc-status, @sc-substatus, @sc-win32-status, @time-taken"
output to a CSV file.

The "%%a" seems incorrectly placed after the output redirection.

Is there another line to run the conversion?
Is there more code to show?
0
 
TessandoIT AdministratorAuthor Commented:
No more code to show, NVIT.

I removed the "%%a" and it did generate a file, but the file was only filled with the headers (e.g. @date, @time, etc).

I paired down what needs to be converted (what I believe to be essential) and I'm getting no real data to speak of:

 log-file-results.png
Looks like I'm going to go back to the drawing board. Any suggestions of ways to convert .Log files to .CSV files for Windows?

Thanks again.
0
 
NVITCommented:
Is the log file a text file?   If so, just rename it to a csv file.

What generates the log file?
0
 
TessandoIT AdministratorAuthor Commented:
The log file is a text file, but doesn't have commas in it. I'm seeking to do this with IIS Log Files.

Thanks
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
So if this is space delimited, then you could try this (I done limited testing):

For /f "tokens=*" %%s in ('dir /s /b *.log') do (
	for /f "tokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15*" %%a in (%%s) do @echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o >> %%s.csv
)

Open in new window


The above is based on the default log file config I have on my IIS server.
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
TessandoIT AdministratorAuthor Commented:
This is close to something that I will write for Production. Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.