EzDevInfo.com

cmd interview questions

Top cmd frequently asked interview questions

RM -rf equivalent for Windows?

I need a way to recursively delete a folder and its children, is there a prebuilt tool for this, or do I need to write one?

DEL /S doesn't delete directories.

DELTREE was removed from Windows 2000+


Source: (StackOverflow)

Batch file: Find if substring is in string (not in a file)

In a batch file, I have a string abcdefg. I want to check if bcd is in the string.

Unfortunately it seems all of the solutions I'm finding search a file for a substring, not a string for a substring.

Is there an easy solution for this?


Source: (StackOverflow)

Advertisements

How to run two commands in one line in Windows CMD?

I want to run two commands in a Windows CMD console.

In Linux I would do it like this: touch thisfile ; ls -lstrh.

How is it done in windows?


Source: (StackOverflow)

Is there a command to refresh environment variables from the command prompt in Windows?

If I modify or add an environment variable I have to restart the command prompt (minor inconvenience). Is there a command I could execute that would do this without restarting CMD?


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 use ADB Shell when Multiple Devices are connected? Fails with "error: more than one device and emulator"

>adb --help

...

-s <specific device>  - directs command to the device or emulator with the given
                        serial number or qualifier. Overrides ANDROID_SERIAL
                        environment variable.
...

>adb devices
List of devices attached 
emulator-5554   device
7f1c864e        device

...

>adb shell -s 7f1c864e
error: more than one device and emulator

?


Source: (StackOverflow)

How to delete files/subfolders in a specific directory at command prompt in Windows

Say, there is a variable called %pathtofolder%, as it makes clear it is a full path of a folder.

I want to delete every single file and subfolder in this directory, but not the directory itself.

But, there might be an error like 'this file/folder is already in use'... when that happens, it should just continue and skip that file/folder.

Can anyone give me some command for this?


Source: (StackOverflow)

Setting a system environment variable from a Windows batch file?

Is it possible to set a environment variable at the system level from a command prompt in Windows 7 (or even XP for that matter). I am running from an elevated command prompt.

set name=value seems to be only valid for the session of the command prompt..


Source: (StackOverflow)

Executing multiple commands from a Windows cmd script

I'm trying to write a Windows cmd script to perform several tasks in series. However, it always stops after the first command in the script.

The command it stops after is a maven build (not sure if that's relevant).

How do I make it carry on and run each task in turn please?

Installing any software or configuring the registry etc is completely out of the question - it has to work on a vanilla Windows XP installation I'm afraid.

Ideally I'd like the script to abort if any of the commands failed, but that's a "nice to have", not essential.

Thanks.


Source: (StackOverflow)

Windows batch files: .bat vs .cmd?

As I understand it, .bat is the old 16-bit naming convention, and .cmd is for 32-bit Windows, i.e., starting with NT. But I continue to see .bat files everywhere, and they seem to work exactly the same using either suffix. Assuming that my code will never need to run on anything older than NT, does it really matter which way I name my batch files, or is there some gotcha awaiting me by using the wrong suffix?


Source: (StackOverflow)

How do I get the application exit code from a Windows command line?

I am running a program and want to see what its return code is (since it returns different codes based on different errors).

I know in Bash I can do this by running

echo $?

What do I do when using cmd.exe on Windows?


Source: (StackOverflow)

Long commands split over multiple lines in Vista/DOS batch (.bat) file

How do I make long commands go over multiple lines in a Vista/DOS batch file?


Source: (StackOverflow)

How to run multiple .BAT files within a .BAT file

I'm trying to get my commit-build.bat to execute other .BAT files as part of our build process.

Content of commit-build.bat:

"msbuild.bat"
"unit-tests.bat"
"deploy.bat"

This seems simple enough, but commit-build.bat only executes the first item in the list (msbuild.bat).

I have run each of the files separately with no problems.


Source: (StackOverflow)

Why does only the first line of this Windows batch file execute but all three lines execute in a DOS command shell?

I have a batch file that executes three Maven commands, one after the other. Each command can be successfully executed in the script - by itself!. But when I add all three commands to the same file, only the first one executes before the script exits. Any idea why?

mvn install:install-file -DgroupId=gdata -DartifactId=base -Dversion=1.0 -Dfile=gdata-base-1.0.jar  -Dpackaging=jar -DgeneratePom=true
mvn install:install-file -DgroupId=gdata -DartifactId=blogger -Dversion=2.0 -Dfile=gdata-blogger-2.0.jar  -Dpackaging=jar -DgeneratePom=true
mvn install:install-file -DgroupId=gdata -DartifactId=blogger-meta -Dversion=2.0 -Dfile=gdata-blogger-meta-2.0.jar  -Dpackaging=jar -DgeneratePom=true

Also, if I copy all three commands and paste them into a DOS command shell (cmd.exe), they execute one after the other with no problem. So this is apparently some issue with the dos batch file.


Source: (StackOverflow)