Tag Archives: Batch

Batch – Switch Proxy Auto-Config URL

This is a batch file I wrote designed for a user in a contracting environment to be able to easily switch between the corporate PAC and their customer’s PAC.  If the current PAC is set to the customer’s (or is blank), it will switch it to the corporate PAC; otherwise it will set the PAC to the customer’s.  Only the two variables at the top need be edited.

For reference, this is the setting in question:

@echo OFF
rem -- Set proxy values in following two lines --
set OUR_COMPANY=http://proxy.example.com:8080/
set THEIR_COMPANY=http://proxy.customer.com:8080/
set KEY_NAME="HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set VALUE_NAME=AutoConfigURL
FOR /F "usebackq skip=1 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO ( 
@echo Current proxy is %PROXY_VALUE%
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v AutoConfigURL /t REG_SZ /d "%THEIR_COMPANY%" /f
echo Proxy switched to %THEIR_COMPANY%
) else (
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v AutoConfigURL /t REG_SZ /d "%OUR_COMPANY%" /f
echo Proxy switched to %OUR_COMPANY%

(Batch) Check if Batch File is Running as Administrator (Windows 7/2008+ Compatible)

This code snippet will allow you to check if you are running with elevated permissions or not. Original credit goes to http://www.robvanderwoude.com/clevertricks.php.  However, the script there is not Windows 7 compatible as the “at” command is deprecated.  Here I use netstat with the “-b” parameter, which requires administrator privileges.

@echo off
netstat -a -n -b > nul
if %ERRORLEVEL% equ 0 (
	echo You are Administrator.
) else (
	echo You are NOT Administrator.

Batch File to Share all Folders in a Given Directory

This batch file will share all of the folders in a given directory.  I chose batch over PowerShell as it does not have the ability to natively share folders (in Server 2008), though you can do it via WMI.  I would prefer using shell commands.  It simply uses the folder name as the share name and grants everyone full permissions (share permissions, not NTFS permissions).  Simply pass the desired directory containing the directories you wish to share as the first parameter.  Otherwise, it will use the current directory by default.  You must not end the parameter with “\”, or net share will refuse to accept the syntax; thus if you want to share everything in D:\, pass the parameter as D: .

@echo off
set "folder=%~1"
if not defined folder set "folder=%cd%"

for /d %%a in ("%folder%\*") do (
  for %%* in ("%%a\.") do net share "%%~n*"="%%a" /grant:everyone,FULL /unlimited