Windows batch files (.bat) can be scripted, which means you can do all sorts of things like setting variables. But if you’re having a hard time getting those variables to set, this might be why.
SET var=Holy crap, it sets the variable!
Will work fine, but…
SET var = Holy crap, the space screws it up!
… will not. If you’re used to putting spaces after equals signs like I am, you can make this mistake without even knowing it. If you do it wrong, it won’t throw any errors either. Just silently fails.
So you want to do something on a mapped network drive using PHP, but it simply tells you it couldn’t find the drive? Worry not.
What’s going wrong?
The problem is that PHP runs under the SYSTEM account, and the SYSTEM account can’t access mapped drives.
So, we need to map the drive from the SYSTEM account. You can do this from your PHP script, but if you need persistent access, we can do this:
1. Download the Windows Sysinternals Suite and unzip it somewhere you can easily get to from a command prompt.
2. Open an elevated command prompt. (Search for “cmd.exe” from the Start ball, and then right-click on it, and choose Run as Administrator.)
3. Using the command prompt, navigate to wherever you put Sysinternals.
4. Elevate yourself once again to supreme power by using:
psexec -i -s cmd.exe
A new command prompt window will open that is running as the SYSTEM account.
5. Map the network drive:
net use z: \\[IP ADDRESS HERE]\[FOLDER NAME HERE] /persistent:yes
And you’re done!
You can only remove this mapping the same way you created it, from the SYSTEM account. If you need to remove it, follow steps 1 -4 but change the command on step 5 to: net use z: /delete.
The newly created mapped drive will now appear for ALL users of this system but they will see it displayed as “Disconnected Network Drive (W:)”. Don’t worry though! It displays as disconnected, but will work for any user.
If you use a Mac and Windows together, you’re likely to end up with a bunch of hidden .DS_STORE files all over your Windows drives. You can easily search and destroy them all using this command:
(Open the command line first of course. You can do this by mashing Win + R, then typing “cmd”. Or just type “cmd” in the Start Menu search box.)
del /s /q /f /a .DS_STORE
That will find every instance of this Mac resource file and delete it. Good times. But wait there’s more. In addition to DS_STORE, OSX will also put a bunch of other junk every where starting with “._”. Kill those like this.
del /s /q /f /a ._.*
(Why not use “._*'” instead? Apparently, you can sweep up legit files from other things such as Chrome by doing that. Thanks commenters.)
Important note! This will only search inside the folder you’re in, as well as every folder below that. So, if you wanted to search and clean an entire drive, make sure you’re in the root folder. Get there with this:
You could also put all of this into a .bat file for great automation.
Pro tip: copy-pasting into your command window
Windows 10 will let you CTRL-V paste into the command line. Friggin sweet. But did you know you can do it in other versions of Windows too? Just right-click on the command window and click Paste. That will save you a little work.