need some DOS help

General software, Operating Systems, and Programming discussion.
Everything from software questions, OSes, simple HTML to scripting languages, Perl, PHP, Python, MySQL, VB, C++ etc.
Post Reply
User avatar
grundy
Senior Member
Posts: 1308
Joined: Sat Dec 16, 2000 12:00 am
Location: CA

need some DOS help

Post by grundy »

hello,

I'm trying to set up a batch file which will monitor the number of files in particular directory. I'd like to run this on a scheduled basis, then write the time, date, and number of files into a log file.

This much I have working, with one issue, which is that I can't figure out how to write all of the information onto a single line. Ideally, I'd like to write out in single lines in the format [date],[time],[#file] in order to create a csv.

Does anybody know how to append the results of multiple dos commands to a single line of output?

Alternately, is it possible in dos to set a variable to an evaluated, non-numeric expression? If I could set num_files=(dir "*.net" |find "File(s)") and end up with the variable num_files equal to the result of dir "*.net" |find "File(s)" I'd be in business...

any ideas? thanks for looking!
User avatar
MadDoctor
New Member
Posts: 5
Joined: Fri Apr 27, 2001 12:00 pm
Location: Looks dark

Post by MadDoctor »

Norm know all about this stuff. Hang in there. He'll be around in a bit.
People will forget what you said... and people will forget what you did... but people will never forget how you made them feel.
User avatar
grundy
Senior Member
Posts: 1308
Joined: Sat Dec 16, 2000 12:00 am
Location: CA

Post by grundy »

MadDoctor wrote:Norm know all about this stuff. Hang in there. He'll be around in a bit.
that's funny, I haven't looked at this forum in probably two years. today I spent an hour or so searching through dos documentation and thinking "who the heck knows about this stuff... I remember there was this guy Norm on Speedguide..."

Norm?
User avatar
Norm
SG VIP
Posts: 14195
Joined: Tue Mar 27, 2001 12:00 pm

Post by Norm »

set hour=%time:~0,2%

set folder=%date:~10,4%_%date:~4,2%_%date:~7,2%_%hour%_%time:~3,2%_%time:~6,2%

echo %folder%>onelineoftext.txt



Cramped for time, so here's a quick example to work with.

Notice there are two vatiables HOUR and FOLDER. In the 3rd line FOLDER adds the date and HOUR to become ONE variable named FOLDER
In the last line FOLDER is echoed to a file and prints ONE line.

All you need to do for your batch file is create a third variable, adding the #files as a variable.

I'll keep an eye on the thread to see that you have understood. Working till 10pm or so tonight, on my way to work now.


Even simpler, to help understand what I mean.

set first=norm
set second=earl
set third=%first%_%second%_steadman

echo %third%>onelineoftext.txt

Will output norm_earl_steadman to a file named onelineoftext.txt


Good luck
User avatar
grundy
Senior Member
Posts: 1308
Joined: Sat Dec 16, 2000 12:00 am
Location: CA

Post by grundy »

Thanks Norm,

I'm aware of the ability to concatenate multiple variables onto a single line of output.

What I can't figure out how to do is to set a variable to anything other than a literal string, or an evaluated mathematical expression.

Again, I want to set a variable equal to the number of files in a directory. For example:

C:\Documents and Settings\solomonw\My Documents>dir "*.doc" | find "File(s)"
13 File(s) 1,184,256 bytes

I want to set a variable equal to the output line here. Clearly, I'll need to parse out the '13', but there appears to be adequate documentation to help me with that.

The only thing that I can think of is an if statement, but the number of files may vary between about 0-100, so that would be less than elegant.

Is it possible to do this?

thanks again for the help!
cyberskye
Senior Member
Posts: 4717
Joined: Wed Jan 10, 2001 12:00 am
Location: DC

Post by cyberskye »

Does DOS support arrays?
anything is possible - nothing is free

:wth:
Blisster wrote:It *would* be brokeback bay if I in fact went and hung out with Skye and co (did I mention he is teh hotness?)
:wth:
User avatar
grundy
Senior Member
Posts: 1308
Joined: Sat Dec 16, 2000 12:00 am
Location: CA

Post by grundy »

cyberskye wrote:Does DOS support arrays?
not sure about that. good suggestion though, I'll look into that tomorrow.
User avatar
grundy
Senior Member
Posts: 1308
Joined: Sat Dec 16, 2000 12:00 am
Location: CA

Post by grundy »

hey hey y'all, progress

I cd to the correct directory, count the number of files, and write this out to a file:

dir "*.net" |find "File(s)" > C:\test\LicCount.txt

this writes out in the format '1 File(s) 0 bytes'

then I parse out the number, and write out date, time, number:

for /F "tokens=1" %A in (C:\test\LicCount.txt) do @echo %time%,%date%,%A>>C:\test\LicLog.txt

the problem is that, while the above command succeeds if run from a command prompt, it fails as a batch script. I've tried as both a .bat and .cmd. Any idea why it is failing or how I can fix it?

thanks again for the help!
cyberskye
Senior Member
Posts: 4717
Joined: Wed Jan 10, 2001 12:00 am
Location: DC

Post by cyberskye »

I don't use DOS so can't really help you.

- this is silly-easy on a unix system (shell or perl script)
anything is possible - nothing is free

:wth:
Blisster wrote:It *would* be brokeback bay if I in fact went and hung out with Skye and co (did I mention he is teh hotness?)
:wth:
User avatar
grundy
Senior Member
Posts: 1308
Joined: Sat Dec 16, 2000 12:00 am
Location: CA

Post by grundy »

cyberskye wrote: this is silly-easy on a unix system (shell or perl script)
need this for a windows-only app :(
User avatar
Norm
SG VIP
Posts: 14195
Joined: Tue Mar 27, 2001 12:00 pm

Post by Norm »

Frankly, it was beyond me. I hate that. So the rest is history.


Change

for /F "tokens=1" %A in (C:\test\LicCount.txt) do @echo %time%,%date%,%A>>C:\test\LicLog.txt

to

for /F "tokens=1" %%A in (C:\test\LicCount.txt) do @echo %time%,%date%,%%A>>C:\test\LicLog.txt

For some reason when used in a batch file there needs to be 2 % signs before each variableA

Now we both know, thanks it'll come in handy.
User avatar
Mad_Haggis
Senior Member
Posts: 4128
Joined: Sun Mar 07, 2004 12:00 pm

Post by Mad_Haggis »

grundy wrote:hello,

I'm trying to set up a batch file which will monitor the number of files in particular directory. I'd like to run this on a scheduled basis, then write the time, date, and number of files into a log file.

This much I have working, with one issue, which is that I can't figure out how to write all of the information onto a single line. Ideally, I'd like to write out in single lines in the format [date],[time],[#file] in order to create a csv.

Does anybody know how to append the results of multiple dos commands to a single line of output?

Alternately, is it possible in dos to set a variable to an evaluated, non-numeric expression? If I could set num_files=(dir "*.net" |find "File(s)") and end up with the variable num_files equal to the result of dir "*.net" |find "File(s)" I'd be in business...

any ideas? thanks for looking!
I like dos.

Specs on the run command function of the PC are required? just trying to help.
BEER
User avatar
grundy
Senior Member
Posts: 1308
Joined: Sat Dec 16, 2000 12:00 am
Location: CA

Post by grundy »

Norm wrote: For some reason when used in a batch file there needs to be 2 % signs before each variableA
holy cow, that works! Norm, I sincereley appreciate the help. I don't think I would have figured that out on my own!
User avatar
YARDofSTUF
Posts: 70006
Joined: Sat Nov 11, 2000 12:00 am
Location: USA

Post by YARDofSTUF »

grundy wrote:holy cow, that works! Norm, I sincereley appreciate the help. I don't think I would have figured that out on my own!


Thats why he is the "DOS Master!"
User avatar
Norm
SG VIP
Posts: 14195
Joined: Tue Mar 27, 2001 12:00 pm

Post by Norm »

Glad to be of assistance.
Once I saw the actual line of code you were using I did some playing and testing (to no avail) It worked on the commandline, but not in a batch file.

Then I had a faint memory of someone telling me to RTFM lol (inside joke - me)

In the XP help files, in the commandline reference section under the FOR command.
Deep in the page.......

"To use FOR in a batch file, use the following syntax:

for %%variable in (set) do command [CommandLineOptions]"

All is well and we both learned something. Actually, I've never used the FOR command in all the batch scripts I've written. It's nice to know how it works and I'll probably be using it in future.
Thank you.

Norm
Post Reply