0

We currently have to import around 200 students into our active directory. We have a BAT file which creates the users and folders on the servers.

I've written a script which scans our MSSQL database for the new students, saves them into an array, and then uses system() to call cmd.exe and run the BAT file to create the students.

The BAT file is located on my Y drive (which is on another server). We currently also use DFS paths so I can access the bat file that way as well.

Its worth noting: 1) Both paths to the BAT file have spaces in them 2) I need to pass arguments to the BAT file as well

At the moment, This is the state of my system() argument in PHP:

$bat = escapeshellcmd("cmd /c Y:\SharedRes\Path To Createstudent\createstudent.bat {$stuArray['forename']} {$stuArray['surname']} {$stuArray['username']} {$stuArray['year']} {$stuArray['sf']}");
system($bat);

And with the DFS Path:

$bat = escapeshellcmd("cmd /c '\\organisation.local\Path To Createstudent\createstudent.bat {$stuArray['forename']} {$stuArray['surname']} {$stuArray['username']} {$stuArray['year']} {$stuArray['sf']}'");
system($bat);

Each time I try to run this, I get the following error:

The filename, directory name, or volume label syntax is incorrect

Any ideas how to get PHP to actually run the BAT file? I'm currently stumped.

Many thanks

Phil

P.S I am running the PHP script via command line:

php.exe -f "C:\import.php"
Phil Cross
  • 9,017
  • 12
  • 50
  • 84
  • 2
    Does the path not need to be in double quotes if it has spaces? Might be a dumb question, never tried this personally – Daniel Casserly Aug 29 '12 at 08:09
  • I've tried encapsulating the whole statement in single quotes then the path in doubles and that doesn't seem to work either. It sees the first space in the path as the end of the path. I'll give it another go just to triple check though! – Phil Cross Aug 29 '12 at 08:10
  • @Daniel Casserly Actually, I've jut tried it, and noticed my error! I can get the Bat file running now! – Phil Cross Aug 29 '12 at 08:13
  • Related, [How do you run a .bat file from PHP?](http://stackoverflow.com/q/835941) – jww Nov 15 '16 at 04:04

1 Answers1

2

if the batch file is in a path with spaces you need to quote the argument:

cmd /c "Y:\SharedRes\Path To Createstudent\createstudent.cmd" ...

Also if the parameters contain spaces you need to quote them too. Single quotes are not recognised by cmd, it has to be double quotes.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • Just 1 small query, If one of the arguments I pass has a single quote (IE O'Brien) will the escapeshellcmd function escape this? In other words will I need encapsulate this argument in double quotes? – Phil Cross Aug 29 '12 at 08:22
  • 1
    Single quotes are irrelevant for the Windows command processor. But as said, you should probably quote all arguments for safety anyway. – Joey Aug 29 '12 at 08:44