Log File and Folder Information Using Command Prompt

If you’re looking to catalog the files and folders in a specific directory and its subfolders, a handy Windows Command Prompt script can help you accomplish this task. This tutorial will walk you through the steps to create and run a batch script that logs detailed information about files and folders.

Prerequisites

Before we get started, you’ll need:

  1. Windows Command Prompt Knowledge: Basic familiarity with the Command Prompt is beneficial.
  2. Directory Location: Know the directory path from which you want to log information.
  3. Text Editor: A text editor like Notepad for creating and editing batch files.

Step-by-Step Guide

Step 1: Open Notepad

  • Press Win + R, type “notepad,” and press Enter to open Notepad, where we’ll create our batch script.

Step 2: Copy the Script

  • Copy the following script and paste it into Notepad:

@echo off
setlocal enabledelayedexpansion

REM 1. Define variables for folder location and log file
set "folderLocation=C:\YourFolderPath"
set "logFile=C:\YourLogFolderPath\log.txt"

REM 2. Initialize the log file
echo File Name,Date Created,Last Modified>%logFile%

REM 3. Recursively traverse the folder and subfolders
for /r "%folderLocation%" %%a in (*) do (
    set "path=%%~dpa"
    set "file=%%~nxa"
    set "created=%%~ta"
    set "modified=%%~ta"

    REM Check if the item is a file or folder
    if exist "%%a\" (
        set "type=Folder"
    ) else (
        set "type=File"
    )

    REM 4. Append file information to the log file
    echo !type!,"!path!,!file!,!created!,!modified!">>%logFile%
)

echo Process completed. Log file created at %logFile%
endlocal

Step 3: Customize Folder and Log File Paths

  • Replace C:\YourFolderPath with the actual path of the folder you want to log information from.
  • Replace C:\YourLogFolderPath\log.txt with the desired path where you want to save the log file.

Step 4: Save the File

  • Click on “File” in Notepad then Choose “Save As.”
  • Set “Save as type” to “All Files.”
  • Name the file with an .bat extension, for example, LogInfo.bat.
  • Save the file to a location of your choice.

Step 5: Run the Script

  • Open the folder where you saved LogInfo.bat using Windows Explorer.
  • Hold down the Shift key and right-click in the folder.
  • Select “Open command window here” from the context menu. This opens Command Prompt with the current folder as the working directory.

Step 6: Execute the Script

  • In the Command Prompt window, type LogInfo.bat and press Enter.
  • The script will run, traversing through the specified folder and its subfolders, logging file and folder information into a log file.

Step 7: View the Log File

  • Once the script finishes running, you can open the log file (specified in the script) using a text editor to view the collected information.

Important Notes

  • Ensure you have permission to access the specified folder and create files in the specified log file location.
  • Back up important data before running scripts that modify or create files.
  • Always validate and understand scripts from untrusted sources to ensure they don’t contain harmful code.

That’s it! You’ve successfully created and used a Command Prompt script to log file and folder information from a specific directory. This can be particularly useful for keeping track of your files or creating a record of folder contents for various purposes.

********************************************************

If you liked what you read Please Share.
I’d love it if you followed me on YouTube and Facebook.

Also, feel free to subscribe to my posts by email.
Donations for the site can be made here.
Thanks for reading.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *