0

I'm helping a bud fix an application that has recently been changed over to using a UNC path. Before he could use a bat file to run

@echo off
echo %~z1

to get a file's size.

Now the bat file won't work because CMD does not support UNC paths as current directories. I thought about using pushd command to temporarily create a drive letter that points to the network resource but I'm thinking there has to be a more direct, cleaner way to do this I'm probably just not experience enough with CMD to know it yet.

Any suggestions or assistance would be greatly, appreciated!

Thanks.

Update

To clarify when the bat file is called (through a PHP file using exec() function) I get nothing in response. I tried a few ways of debugging (it's been a few days so I don't remember exactly what) but the most I could get was "Echo is off" or "The system cannot find the file specified." errors. I can copy/paste the file address into my Windows Explorer and I can find the file fine though.

Update II

It has been noted that the code shouldn't have a problem despite UNC not being supported. If this is true then what else could be the issue? Like I said before I can copy and paste the file paths that are given to the bat file and they open fine in windows explorer.

Update III

I tried timing how long the bat file took to execute and it seems to randomly either take almost no time or a little over a minute. So I'm guessing that might be my problem area. However when I run it via the ajax call its response time is about 550-650. I have no idea what would cause a bat file's execution time to vary by so much. Any ideas would be welcome!

Thanks in advance for any input!

mario
  • 1,503
  • 4
  • 22
  • 42

1 Answers1

1

cmd is not compatible with an UNC active directory, but the code in your file will not have any problem with it. You can invoke as

\\server\share\folder\file.bat \\server\share\folder\file.txt
d:\folder\file.bat "\\server\share\folder with spaces\file.txt"
"\\server\share\folder with spaces\file.bat" d:\file.txt
....

and in every case your posted code will work as long as both the batch file and the file to be processed exist

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Okay cool, could you explain to me why the code in this case would work specifically? It seems to directly contradict what Microsoft's documentation said. – mario Mar 30 '15 at 16:31
  • And if the UNC path isn't the problem then what do you think it could be since the path is pretty much the only thing that's been changed since it broke? – mario Mar 30 '15 at 16:34
  • 1
    @gv0000, 1) if the file name/path contains spaces you need to include the apropiated quotes (both in batch and file being referenced if necessary). 2) How are you executing the PHP code, directly calling a script or from a web server? In the second case, does the account under the web server is running have access to the remote resources? – MC ND Mar 30 '15 at 16:41
  • I am calling this bat file using `exec("batfile_getFileSize \"$file\"",$out);` in a php file. batfile_getFileSize is a bat file in the same directory. $file is the file path such as $file = "\\\\server\\share\\path_to_file\file.zip" And the user running the command has admin access. – mario Mar 30 '15 at 17:02
  • 1
    @gv0000, Is the last single backslash a typo error? – MC ND Mar 30 '15 at 18:46
  • Sorry, its just a typo. – mario Mar 30 '15 at 18:48
  • 1
    @gv0000, i've been testing with your code with both a local and remote bach files, local and remote data files, calling the php script from command line (`php -f test.php`) and everything works as long as the user calling the script has access to the remote folder. – MC ND Mar 31 '15 at 06:56
  • Cool. You're awesome and I appreciate it. I timed the bat file's execution running manually and it will randomly take either almost no time or a little over a minute to execute. I'm thinking that might be my problem area. – mario Mar 31 '15 at 14:11
  • And when I running via the ajax request it is usually around 550-650 response time. – mario Mar 31 '15 at 14:12