1

I'm trying to run a sequence of commands using 1 line command. The sequence in this example is simple

'cd\;mkdir C:\Users\Admin\Documents\test'

Individually, in command prompt, these commands work just fine, but when I try to execute the entire script, it gives me an error The filename, directory name, or volume label syntax is incorrect

The same sequence of commands runs just fine in powershell, but not in command prompt

What could possibly be wrong here?

thepurpleowl
  • 147
  • 4
  • 15
Mi Po
  • 1,123
  • 2
  • 12
  • 21

3 Answers3

1

Using && will only continue executing commands if the previous command was successful. If the directory already exists MKDIR returns a value of 1, not 0.

Use & if you want the subsequent command to run regardless of the success or failure of the previous command.

lit
  • 14,456
  • 10
  • 65
  • 119
1

PowerShell and cmd are different and thus have different syntax.

In cmd, & and && are conditional processing symbols.

Character: &

Syntax: command1 & command2

Definition: Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.

Character: &&

Syntax: command1 && command2

Definition: Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.

And ; is a special character used to separate parameters

Character: ; or ,

Syntax: command1 parameter1;parameter2

Definition: Use to separate command parameters.

In PowerShell, ; is a statement terminator. This is frequently used to run two commands on the same line as if there was a new line in between.

BenH
  • 9,766
  • 1
  • 22
  • 35
0

I suggest first to read Single line with multiple commands using Windows batch file explaining the operators & and && and || which can be used in a batch file or on Windows command prompt.

The right command line with multiple commands for this example is:

cd \ & mkdir "C:\Users\Admin\Documents\test" 2>nul

The first command cd \ sets current directory to root of current drive although this is not really necessary.

The second command "C:\Users\Admin\Documents\test" 2>nul creates the entire directory tree (with enabled command extensions as by default). If an error occurs like this directory already existing, the exit code of command MKDIR is 1 for failure instead of 0 for success. The error message is redirected with 2>nul from handle STDERR to device NUL to suppress it.

It is of course also possible that the directory could not be created because the used user account does not have permissions to create this directory or there is in directory C:\Users\Admin\Documents already a file with name test. For that reason it is perhaps better to use:

cd \ & ( if not exist "C:\Users\Admin\Documents\test\" mkdir "C:\Users\Admin\Documents\test" 2>nul ) & echo OK

More commands can be appended with &, && or || as demonstrated here with echo OK being always executed independent on IF condition and result of creation of the directory.

Mofi
  • 46,139
  • 17
  • 80
  • 143