Log directory structure to output file Batch file (.bat)

Batch file and Windows PowerShell
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

Log directory structure to output file Batch file (.bat)

Post by dzcadii » Sun Mar 01, 2015 10:55 pm

Simple way to iterate through a directory structure and log the output.
Options:
  1. Files only
  2. Directories only
  3. Directories and files
*Note: Place this file in the root of where you want the scanning to begin
Deliminator is "::.::"
Save file as yourfilename.bat (just make sure the extension is ".bat"

Code: Select all

@echo off
setLocal EnableDelayedExpansion

set hr=%TIME: =0%
set hr=%hr:~0,2%
::echo %hr%

set min=%TIME:~3,2%
::echo %min%

set sec=%TIME:~6,2%
::echo %sec%



set log_file=%date:~10,4%%date:~4,2%%date:~7,2%_%hr%%min%%sec%_scan_results.txt

set /P RESULT=[Please select an option(1-Files/2-Directories/3-All)]

IF %RESULT% == 1 GOTO Option1
IF %RESULT% == 2 GOTO Option2
IF %RESULT% == 3 GOTO Option3

:Option1
echo "I am logging all files ONLY"
for /r %%a in (*) do echo %%a::.::%%~za >> %log_file%
goto DONE


:Option2
echo "I am logging all directories ONLY"
for /d %%a in (*) do dir /ad /on /s /b "%%a" >> %log_file%
goto DONE

:Option3
echo "I am logging all directories and files"
for /d %%a in (*) do dir /ad /on /s /b "%%a" >> %log_file%
for /r %%a in (*) do echo %%a::.::%%~za >> %log_file%
goto DONE

:DONE
echo I have written the results to %log_file%
PAUSE


Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests