Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

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
0
mtrussell
Asked:
mtrussell
1 Solution
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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
 
Christopher McKayCommented:
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
 
mtrussellAuthor Commented:
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
Bill PrewCommented:
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
 
Bill PrewCommented:
==> 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
 
mtrussellAuthor Commented:
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
 
Bill PrewCommented:
==> 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
 
mtrussellAuthor Commented:
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
 
Bill PrewCommented:
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
 
mtrussellAuthor Commented:

Here you go.

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

Call DelayTest




DoCmd.OpenQuery "Qry_NotAttachedFiles", acViewNormal, acEdit
0
 
Bill PrewCommented:
Try this change:

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

~bp
0
 
mtrussellAuthor Commented:
Thanks!
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Tackle projects and never again get stuck behind a technical roadblock.
Join Now