Solved

Write a batch command in DOS

Posted on 2010-11-30
12
432 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:mtrussell
12 Comments
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 34241393
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.
0
 
LVL 22

Expert Comment

by:Bartender_1
ID: 34241437
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
0
 

Author Comment

by:mtrussell
ID: 34241503
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?
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 34241510
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
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 34241526
==> 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
0
 

Author Comment

by:mtrussell
ID: 34247207
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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 51

Expert Comment

by:Bill Prew
ID: 34247733
==> 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
0
 

Author Comment

by:mtrussell
ID: 34247855
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?
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 34247937
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
0
 

Author Comment

by:mtrussell
ID: 34248087

Here you go.

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

Call DelayTest




DoCmd.OpenQuery "Qry_NotAttachedFiles", acViewNormal, acEdit
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 34248367
Try this change:

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

~bp
0
 

Author Closing Comment

by:mtrussell
ID: 34248468
Thanks!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now