Home‎ > ‎Scripting‎ > ‎

DOS Scripts

Create a large empty file

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/fsutil.mspx?mfr=true

fsutil file [createnew] PathName length

fsutil file createnew c:\DeleteMe1GB.txt 1073741824

For loops

for each directory in .\Export\ delete directoryname 

for /D %%d IN (Export) DO rmdir %%d

Random Password Generator

 

wapg.exe -x 8 -n 1 -E "|?&<%\/!@#$^*',`~+=_;:()[]{}." >foo.txt

WSFTP

 

iftpaddu.exe -h hostname -mod -u username -p newpass 

 

Query RDP Sessions

http://www.howtogeek.com/howto/windows/command-line-hack-for-terminal-server-has-exceeded-the-maximum-number-of-allowed-connections/

query session /server:servername
reset session [ID] /server:servername

query session /server:localhost | findstr console

query user


DateStamp

echo %date:~-4%%date:~4,2%%date:~7,2%
returns YYYYMMDD
echo %date:~4,2%/%date:~7,2%/%date:~-4%

MM/DD/YYYY

echo %date:~0,3%

returns Mon (day of the week)

SET CurrDay=%DATE:~7,2%&::
SET CurrMonth=%DATE:~4,2%&::
SET CurrYear=%DATE:~10,4%&::
SET CurrDate=%CurrMonth%_%Curr<wbr>Day%_%Curr<wbr>Year%</wbr></wbr>

http://ings.ca/jim/2008/02/10/adding-timestamps-in-a-dos-batch-file/

@echo off
cls

rem Timestamp Generator
rem Parse the date (e.g., Fri 02/08/2008)
set cur_yyyy=%date:~10,4%
set cur_mm=%date:~4,2%
set cur_dd=%date:~7,2%

rem Parse the time (e.g., 11:17:13.49)
set cur_hh=%time:~0,2%
if %cur_hh% lss 10 (set cur_hh=0%time:~1,1%)
set cur_nn=%time:~3,2%
set cur_ss=%time:~6,2%
set cur_ms=%time:~9,2%

rem Set the timestamp format
set timestamp=%cur_yyyy%%cur_mm%%cur_dd%-%cur_hh%%cur_nn%%cur_ss%%cur_ms%

rem Do something with it
echo Timestamp: %timestamp%

rem Clear the environment variables
set cur_yyyy=
set cur_mm=
set cur_dd=
set cur_hh=
set cur_nn=
set cur_ss=
set cur_ms=
set timestamp=
systeminfo > "H:\DC Patching\%computername%_%date:~-4%%date:~4,2%%date:~7,2%.txt" 

 

Aggregate.bat

concatenate files together. Create a batch file with the content bellow, drop it into the directory of files you want to aggregate and run it.
for %%f in (*.log) do type "%%f" >> aggregate.txt 

http://www.howtogeek.com/howto/keyboard-ninja/keyboard-ninja-concatenate-multiple-text-files-in-windows/

Get the logged on user for a remote machine

nbtstat -a computername
nbtstat -a computername | FIND "<03>" | FIND /I /V "computername"
WMIC /Node:computername ComputerSystem Get UserName 

  

ComputerNames that start with ABC

http://www.dostips.com/forum/viewtopic.php?f=3&t=1353

IF %COMPUTERNAME:~0,3%==ABC echo foo



What service is using a port

XXXX is the port in use
NNNN is the PID returned by netstat -ano

netstat -ano | findstr XXXX
tasklist /svc /FI "PID eq NNNN"
net stop "Service Name"



Calculate File sizes

get a list of all files then create a new list of their file sizes 

dir /s/b *.zip > c:\temp\test.txt
#return file size and file path
for /f %I in (c:\temp\test.txt) do @echo %~zpI
#Return file size only
for /f %I in (c:\temp\test.txt) do @echo %~zI


Testing port access from a batch script

Had to call powershell to get this done
powershell -noprofile -ExecutionPolicy bypass -command "& {$(try { $socket = new-object Net.Sockets.TcpClient; $socket.Connect('google.com',80); $socket.Connected} catch [system.exception] {'False'})}" 






Comments