Windows Bat File Potpourri


The purpose of this article is to display a set of commonly used Windows Bat Scripts

Check if a folder exists and create if it doesn't
if not exist "C:\Temp\InstallationLogs\" mkdir C:\Temp\InstallationLogs
Iterate through a .csv file
for /f "delims=," %%a in (Computers.csv) do (
Run a .exe from a share on each computer using psexec, Store results to a text file. Location for psexec from microsoft
C:\Temp\psexec.exe \\%%a -h -c \\ServerName\Share\HelloWorld.exe >> C:\Temp\InstallationLogs\%%a_InstallResults.txt)
Create temp Drive Letter mapped to your UNC root location and essentialy Change Directory to that location.
pushd \\servername\share
Remove the temp drive and return to orginal Directory
Take Ownership of subfiles and folders from a list of Paths in Folders.CSV. Supress pompt.
REM Takes ownership of all Sub Files and folders read from Path stored is CSV
REM  Can be destructive to Redirected permissions
for /f "delims," %%a in (Folders.csv) do (
if exist %%a takeown /f %%a /r /d Y
if exist %%a icacls %%a /grant Everyone:F /T
echo %%a)