Link to home
Start Free TrialLog in
Avatar of mtrussell
mtrussell

asked on

Write a batch command in DOS

Hi Experts -

I need to write a batch command for the below DOS commands:

pushd \\jassrv02\jas-fp$\documents
DIR /B /O:N > filenames.txt



I am not a DOS expert at all so could someone show me how to do this?  I know that I open Notepad and save it as an .exe file and I know if I type in the above lines in a command prompt I get the information I need but I am not sure how to write the batch command.  Any assistance provided would be appreciated.

Thanks
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

A batch file is basically a collection of commands - and you DO NOT save it as a .EXE - you save it as a .CMD or .BAT.  

For example, save those two commands in text file called "mybatch.cmd" and then simply type mybatch to run it.
To create a batch file, open notepad, type the commands you want, then save as a ".bat" file.

Now, specifically, what are you attempting to do with the above commands?

It appears you're attempting to get a list of all the filenames in "\\jassrv02\jas-fp$\documents" in a file called "Filenames.txt"

If this is what you're attempting to accomplish, how about trying this command:

Dir /B /O:N "\\jassrv02\jas-fp$\documents" >>filenames.txt




Hope this helps!

:o)

Bartender_1
Avatar of mtrussell
mtrussell

ASKER

Thanks!  The reason I can't do the second suggested command is I have to force DOS to find the network... ok last question what line do I put in this so the command window doesn't pop-up, is there a way to run this in the background?
Just to add a bit more.  You may not even need the PUSHD, since the following should work:

DIR /B /O:N \\jassrv02\jas-fp$\documents > \\jassrv02\jas-fp$\documents\filenames.txt 

Open in new window

If you do need to do the PUSHD for some reason then it is good practice to "undo" the push when you are done working in that directory with a POPD.  So that would look like:

pushd \\jassrv02\jas-fp$\documents
DIR /B /O:N > filenames.txt 
popd

Open in new window

Most people like to suppress the display of each command in a BAT file from displaying, and that can be done with an ECHO OFF command.  Adding an @ at the front of that line will even suppress it from displaying.  So the script could then look like:

@echo off
pushd \\jassrv02\jas-fp$\documents
DIR /B /O:N > filenames.txt 
popd

Open in new window

Lastly, if you wanted for clarity sake, you can assign paths of filenames to variables in the script to make it more readable, more maintainable, or save some typing.  So something like this will work (notice it's a good idea to enclose filenames in double quotes just in case they ever contain spaces):

@echo off
set ServerFolder=\\jassrv02\jas-fp$\documents
set ListingFile=filenames.txt
pushd "%ServerFolder%"
dir /B /O:N > "%ListingFile%"
popd

Open in new window

Hope this helps a little, feel free to ask questions.

~bp
==> ok last question what line do I put in this so the command window doesn't pop-up, is there a way to run this in the background?

How are you initiating the script, is it going to be a scheduled thing from the Windows Scheduler, or just clicked on via Windows Explorer when needed to run, etc?

~bp
I am going to kick off the command when the end-user needs to look at the file.  It will be an ad-hoc routine.  It is more an administrative 'needle in the haystack' routine I am trying to solve for them.
==> I am going to kick off the command when the end-user needs to look at the file.  It will be an ad-hoc
==> routine.  It is more an administrative 'needle in the haystack' routine I am trying to solve for them.

When you say "kick off" what exactly do you mean, how will you invoke the script?

~bp
sorry about that... the code is a procedure behind a button in an Access database.  When the end-user clicks the button it runs the code we have discussed.

One question for you - how do you hide the command prompt entirely from showing up?  the Echooff hides the step by step commands on the screen but is there a way of minimizing the screen?
Can you share the VBA code you are using in the Access database to invoke the BAT script?

It's likely the easiest way to do this is by adjusting the VBA code where you launch the BAT to do it without a window.  Much harder for the BAT file to "hide" its window after it starts.

~bp

Here you go.

Call Shell("\\jassrv02\jas-fp$\Documents\filename.bat", 1)

Call DelayTest




DoCmd.OpenQuery "Qry_NotAttachedFiles", acViewNormal, acEdit
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!