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.
- Open a text editor, such as Notepad.
- 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
- 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.
@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 (...)
: Thefor /f
loop reads through each line ininput.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 thecount
variable.set /a count+=1
: This increments the counter for the next iteration.
endlocal
: Ends the local environment changes made bysetlocal
.
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
Post a Comment