Batch command to Generate Text Files Based on Lines from a Source File

Due to some specific requirements, I often work with large amounts of data in Excel spreadsheets. Ultimately, I might need to transform each well-organized row of data into a separate text document. This is where Batch commands come in handy. The Batch command shared in this article can generate new text files line by line based on the content of a specified text file, creating one text file per line. The naming convention starts with 1.txt and proceeds sequentially.
  1. Open a text editor, such as Notepad.
  2. Copy and paste the following code into the editor:
@echo off
setlocal enabledelayedexpansion

set count=1

for /f "delims=" %%a in (input.txt) do (
    echo %%a > !count!.txt
    set /a count+=1
)

endlocal
  1. Save the code as a .bat file, for instance, generateFiles.bat.
  2. Ensure your source file, input.txt, is in the same directory as the .bat file.
  3. Double-click on generateFiles.bat to run it.
Save the code as a .bat file, for instance, generateFiles.bat. Ensure your source file, input.txt, is in the same directory as the .bat file. Double-click on generateFiles.bat to run it. Command Explanation:
  • @echo off: This command prevents the batch file from displaying the command prompt during its execution.
  • setlocal enabledelayedexpansion: This enables delayed environment variable expansion. This allows us to use the !count! syntax for dynamic variable handling inside the loop.
  • set count=1: Initializes a counter variable to 1.
  • for /f "delims=" %%a in (input.txt) do (...): The for /f loop reads through each line in input.txt. The "delims=" part ensures the entire line is read, even if it contains spaces.
    • echo %%a > !count!.txt: For each line read, it echoes the line's content into a new file named after the current value of the count variable.
    • set /a count+=1: This increments the counter for the next iteration.
  • endlocal: Ends the local environment changes made by setlocal.
Batch Script – Commands Batch Command Post Lists
Batch command to Generate Text Files Based on Lines from a Source File

Batch command to extract the filenames in the current directory

Batch command to create blank text files

Batch rename files using the parent directory name

Comments

Popular posts from this blog

Installing Python3.10.x Version on Mac and Set the Environment

Adding Specific Applications to Windows 11 Startup Apps

HTTPS(SSL) Blocks WordPress Admin Access