如何通过Windows CMD按指定行数拆分文件并命名为序列文件
Hey there, let's walk through how to split files in Windows CMD—since CMD doesn't have a dedicated built-in split command, we'll use native tools and batch scripts to handle both of your scenarios cleanly.
First, let's cover the general case where you want to split any file into chunks with a fixed number of lines (say, 50 lines per file). We'll use a batch script that counts total lines, calculates how many chunks we need, then splits the file accordingly.
Step-by-step script
Save this as split_equal_lines.bat, then adjust the input_file and lines_per_chunk variables to match your needs:
@echo off setlocal enabledelayedexpansion :: Set your parameters here set "input_file=your_input_file.txt" set "lines_per_chunk=50" :: Change this to your desired line count per file set "output_prefix=part" :: Output files will be named part1.txt, part2.txt, etc. :: Count total lines in the input file for /f %%a in ('find /c /v "" "%input_file%"') do set "total_lines=%%a" :: Calculate number of chunks (add 1 if there's a remainder) set /a "num_chunks=total_lines / lines_per_chunk" set /a "remainder=total_lines %% lines_per_chunk" if !remainder! gtr 0 set /a "num_chunks+=1" :: Initialize counters set "current_chunk=1" set "start_line=1" :split_loop :: Extract lines from start_line to start_line + lines_per_chunk -1 for /f "skip=!start_line! tokens=* delims=:" %%b in ('findstr /n "^" "%input_file%"') do ( echo %%b >> "%output_prefix%!current_chunk!.txt" set /a "line_count+=1" if !line_count! equ !lines_per_chunk! goto next_chunk ) :next_chunk if !current_chunk! equ !num_chunks! goto done set /a "current_chunk+=1" set /a "start_line+=lines_per_chunk" set "line_count=0" goto split_loop :done echo Successfully split into !num_chunks! files. endlocal
How it works:
find /c /v ""counts all lines in the input file (including empty lines).findstr /n "^"adds line numbers to every line, so we can skip lines and track our position.- The loop extracts lines in batches, writing each batch to a new file until all lines are processed.
For this specific case, we can use a simpler script that directly processes story.txt and outputs to the chapter-named files. This script doesn't require pre-counting lines—it just processes each line and switches to a new file every 10 lines.
Ready-to-use script
Save this as split_story.bat in the same folder as story.txt, then run it:
@echo off setlocal enabledelayedexpansion set "input_file=story.txt" set "lines_per_chapter=10" set "chapter_num=1" :: Add line numbers to each line, then process them for /f "tokens=1* delims=:" %%a in ('findstr /n "^" "%input_file%"') do ( :: Write the line content (%%b) to the current chapter file echo %%b >> "chapter.!chapter_num!.txt" :: Check if we've reached the line limit for this chapter set /a "line_mod=%%a %% lines_per_chapter" if !line_mod! equ 0 set /a "chapter_num+=1" ) echo Done splitting! Check your chapter files. endlocal
How it works:
findstr /n "^"prefixes every line instory.txtwith a line number (e.g.,1:Once upon a time...).- The
for /floop splits each line into the line number (%%a) and the actual content (%%b). - We write each content line to the current chapter file. When the line number is a multiple of 10, we increment the chapter number to start writing to the next file.
- If the last chapter has fewer than 10 lines, it will still be saved with whatever lines are left.
内容的提问来源于stack exchange,提问作者Mike

