问题
I am hoping the wonderful experts here can assist me with my problem. I have found this site extremely useful in the past in solving other issues I have had but I have searched this site and tried all the answers similar to my problem but cant seem to get any of them to work for this particular problem. I am not a coder but have dabbled with other code and got them to work for other issues.
I need to split a text file containing keywords(1 kw per line, no blank lines) into multiple text files within the same directory, each with 300 lines (except last text file if total input lines not exactly divisible by 300). The input file will NEVER be larger than 100MB.
Ideally, I then need the input file deleted once it has been split and all split text files moved to another directory (there are no other text files to worry about in the original directory) I need it to be a bat file or vbs script called via bat file.
input file:
- keyword-file.txt
output files:
- keyword-file_1.txt (300 lines)
- keyword-file_2.txt (300 lines)
- keyword-file_3.txt (300 lines)
- etc
To clarify requirements the above:
- Split input text file (<100MB) into smaller text files, each with 300 lines
- Delete input text file
- Move all split text files to another specified directory
回答1:
For the splitting, perhaps something like this will do:
@Echo Off
Setlocal EnableExtensions DisableDelayedExpansion
Set "inFile=keyword-file.txt"
Set "nLines=300"
If Not Exist "%inFile%" GoTo :EOF
For %%A In ("%inFile%") Do Set "fName=%%~nA"&Set "fExt=%%~xA"
Set "count=0"
For /F %%A In ('Find /C /V ""^<"%inFile%"') Do Set "fLines=%%A"
(For /L %%A In (1 1 %fLines%) Do (Set/P "data="
Set/A "file=(count/%nLines%)+1", "count+=1"
SetLocal EnableDelayedExpansion
(Echo=!data!)>>"%fName%_!file!%fExt%"
EndLocal))<"%inFile%"
EndLocal
GoTo :EOF
I'll leave the deletion and move commands to you…
来源:https://stackoverflow.com/questions/44389673/batch-file-to-split-text-file-into-multiple-files-in-same-directory-on-win-svr-2