EzDevInfo.com

batch-file interview questions

Top batch-file frequently asked interview questions

How to pass command line parameters to a batch file?

I need to pass id and password to a cmd (or bat) file at the time of running rather than hardcoding them into the file.

Here's what the command line looks like:

test.cmd admin P@55w0rd > test-log.txt

Source: (StackOverflow)

What does %~dp0 mean, and how does it work?

I find %~dp0 very useful, and I use it a lot to make my batch files more portable.

But the label itself seems very cryptic to me... what is the ~ doing? does dp mean drive and path? does the 0 refer to %0, the path to the batch file that includes the file name?

Or is it just a weird label?

I'd also like to know if it is a documented feature, or something prone to be deprecated.


Source: (StackOverflow)

Advertisements

Get list of passed arguments in Windows batch script (.bat)

I'd like to find a Windows batch counterpart to Bash's $@ that holds a list of all arguments passed into a script.

Or I have to bother with shift?


Source: (StackOverflow)

Stop and Start a service via batch or cmd file?

How can I script a bat or cmd to stop and start a service reliably with error checking (or let me know that it wasn't successful for whatever reason)?


Source: (StackOverflow)

What does %~d0 mean in a Windows batch file?

I'm looking at a batch file which defines the following variables:

set _SCRIPT_DRIVE=%~d0 set
set _SCRIPT_PATH=%~p0

What do %~d0 or %~p0 actually mean? Is there a set of well-known values for things like current directory, drive, parameters to a script? Are there any other similar shortcuts I could use?


Source: (StackOverflow)

Declaring and using a variable in DOS/Windows batch file (.BAT)

I'm trying to declare and use a variable in my batch file. It looks like it should be simple.

@ECHO OFF

SET location = "bob"
ECHO We're working with "%location%"

The output I get is:

We're working with ""

What's going on here? Why is my variable not being echo'd?


Source: (StackOverflow)

What is the current directory in a batch file?

I want to create a couple of batch files to automate some thing.

My question is when I create a batch file. What is the current directory? Is it the directory where the file is located or is it the same directory that appears in cmd?


Source: (StackOverflow)

How to check if a file exists from inside a batch file [duplicate]

This question already has an answer here:

I need to run a utility only if a certain file exists. How do I do this in Windows batch?


Source: (StackOverflow)

How to prevent auto-closing of console after the execution of batch file.

Can someone tell me, what command to put at the end of a batch file to prevent auto-closing of console after the execution of file?


Source: (StackOverflow)

Logical operators ("and", "or") in DOS batch

How would you implement logical operators in DOS Batch files?


Source: (StackOverflow)

In Windows cmd, how do I prompt for user input and use the result in another command?

I have a Windows .bat file which I would like to accept user input and then use the results of that input as part of the call to additional commands.

For example, I'd like to accept a process ID from the user, and then run jstack against that ID, putting the results of the jstack call into a file. However, when I try this, it doesn't work.

Here's my sample bat file contents:

@echo off
set /p id=Enter ID: 
echo %id%
jstack > jstack.txt

and here's what shows up in jstack.txt:

Enter ID: Terminate batch job (Y/N)? 

Source: (StackOverflow)

How can you find and replace text in a file using the Windows command-line environment?

I am writing a batch file script using Windows command-line environment and want to change each occurrence of some text in a file (ex. "FOO") with another (ex. "BAR"). What is the simplest way to do that? Any built in functions?


Source: (StackOverflow)

Create folder with batch but only if it doesn't already exist

Can anybody tell me how to do the following in in a DOS batch script? (*.bat):

  • Create a folder only if it doesn't already exist

In more detail, I want to create a folder named VTS on the C:\ drive, but only if that folder doesn't already exist. I don't want to overwrite the contents of the folder if it already exists and the batch is executed...


Source: (StackOverflow)

Displaying Windows command prompt output and redirecting it to a file

How can I run a command-line application in the Windows command prompt and have the output both displayed and redirected to a file at the same time?

If, for example, I were to run the command dir > test.txt, this would redirect output to a file called test.txt without displaying the results.

How could I write a command to display the output and redirect output to a file in the Windows command prompt, similar to the tee command on Unix?


Source: (StackOverflow)

How to create empty text file from a batch file?

Can somebody remember what was the command to create an empty file in MSDOS using BAT file?


Source: (StackOverflow)