Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

batch file - determining a file name

Status
Not open for further replies.
Oct 9, 2003
174
US
Hey guys

Not sure if this is the right forum to post this question but I'll give it a shot.

I'm writing a batch file to run on a win2K server, I have a directory (e:\logs) that I am trying to have the batch file read the filename of the log file in the dir ( there will only be one file in there at a time) and then put that string into a variable.

Can anyone help me with that.
 
I would suggest that you not use batch files and instead look at vbscript. Here is the code to give you want you asked for. Check out the vbscript forum for additional help. forum329

Code:
Dim fso 
Dim oFolder
Dim oFile

  Path1 = "C:\Temp"

  Set fso = createobject("Scripting.FileSystemObject")
  Set oFolder = fso.GetFolder(Path1)
  
  For Each oFile In oFolder.files
      Wscript.Echo oFile.Name
  Next

I hope you find this post helpful.

Regards,

Mark
 
thanks mark but i'm worse at vb scripting than i am at batch scripting.

I can use dir /b e:logs *.log to get the name of the log file but how to I pass that to a variable?
 
sorry about that I mean't

dir /b e:\logs\*.log to get the name of the log file to display, but how to I redirect that to a variable?
 
If I understand, I think FOR can help you. Such as:

FOR %F IN (*.log) DO echo %F

This will echo the file name for every file with .log for an extension. Of course, you only have one, but it's now stored in %F.

Do HELP FOR at the command prompt for the full dope.
 
This should do it if you are only using one log file in the directory.

set /a log=dir /b e:logs *.log
echo %log%


Explination: Set log=logfilename.log would set a variable called %log%. This is how you call a variable in a batch %var%. The /A tells it to set it as an expression, so this will cause the variable to do an action instead of just a statement. You can also use the /P to have Set prompt the user for a variable.

set /p name:"Enter username:"

would Ask the user
Enther username:_______

once typed that becomes the variable.


But markdmac is right about the vbs thing. If you wanna learn how to script study VBS. I am just now starting to read more about vbs and loving more and more each day. VBS can do so much more with alot fewer headaches.
 
What is it that you are looking to DO with the file once you have the name? I've already provided you witht he code to enumerate all of the file names.

You should really re-evaluate your position on this. From the sound of it you are not strong in batch files and frankly that is for the most part a dead technology. Scripting is both the present and future. (yes there is still a place for batch files, but complexity is not the place for it).

Vbscripting is an essential skill at this point. The language is incredibly easy to follow so there is a very small learning curve and Microsoft provides HUNDREDS of sample scripts for free to use as a starting point.

Give some more details of what you are trying to do, I may already have a script that does it or can help you.

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top