From Segfault
Jump to: navigation, search


while loop

@echo off
setlocal enableextensions enabledelayedexpansion
set /a "x = 0"
    if %x% leq 5 (
        echo %x%
        set /a "x = x + 1"
        goto :while1

for loop

for /l %a in (1, 1, 100) do echo %a

Or, when operating on files:

for /f %a in ('dir /b /d') do move %a dir\%a


There's also forfiles available for Windows Vista and later versions (or via the Windows Resource Kit[1]), that can do things like delete files older than N days[2]:

forfiles /p "c:\what\ever" /s /m *.* /d <number of days> /c "cmd /c del @path"

To expand on that, find recent files greater than 200 MB:[3][4]

$ forfiles /p c:\    /s /d +"10/01/2017" /c "cmd /c if @fsize gtr 209715200 echo @path @fsize @fdate @ftime"
"C:\pagefile.sys" 1073741824 10/19/2017 5:03:32 PM
"C:\Users\root\AppData\Local\VirtualStore\Windows\SoftwareDistribution\DataStore\DataStore.edb" 604045312 10/19/2017 5:11:51 PM
"C:\Windows\SoftwareDistribution\DataStore\DataStore.edb" 604045312 10/19/2017 5:19:22 PM
"C:\Windows\winsxs\ManifestCache\702349c5b78f9a04_blobs.bin" 211158729 10/19/2017 5:44:16 PM


echo "hello" > NUL


$ echo %date%%time%
27.07.2011 4:44:21,50


This really just redirects stdout to a file and then[5] displays it:

dir > a.txt && type a.txt


Gather version information with wmic[6]:

$ wmic datafile where name='c:\\Program Files\\7-Zip\\7z.exe' get version
  • The double backslashes are important.
  • Only single quotes will work, double quotes will produce an error.

Gather installed software packages:[7]

$ wmic softwarefeature get productname,version | find /i "zip"
7-Zip 9.34            


After PowerShell is started, find out where our profile[8] is stored, as this varies[9] between Windows versions:

% powershell
PS C:\temp> $profile
C:\Documents and Settings\alice\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

We can now create scripts in Microsoft.PowerShell_profile.ps1 that will get loaded the next time PowerShell starts. E.g., setting a prompt[10] that will carry the current directory:

function prompt
   "PS " + $(get-location) + "> "


A very simple netcat substitute, for UDP and TCP:

PS> (New-Object Net.Sockets.UdpClient).Connect("some-host",1234)
PS> (New-Object Net.Sockets.TcpClient).Connect("some-host",1234)


Following a (log) file[11]:

PS> Get-Content file.log -Wait
PS> Get-Content file.log -Wait -Tail 10             # print the last 10 lines, then follow (PowerShell 3 only)

Tail, and search for something:

PS> Get-Content file.log -wait | where { $_ -match "foo" }