How to separate single text file into multiple text file by lines using batch

batchbatch filecmd.exetext editing

I have running the following program,

@echo off
cls
set /p "filename=type file name>"
setlocal enabledelayedexpansion
for /r E:\ %%a in (*) do if "%%~nxa"=="%filename%" (
echo %%~dpnxa >>path.txt
)

I have got a output file path.txt which contains,

E:\new.txt 
E:\Redmi\new folder\new.txt
E:\windows\new folder\new folder\new.txt

I like to have those in separate files like,

E:\new.txt in path1.txt

E:\Redmi\new folder\new.txt in path2.txt

E:\windows\new folder\new folder\new.txt in path3.txt

Best Answer

How can I separate single text file into multiple text file by lines?

Building on the comment by Seth, you can use the following batch file:

@echo off
cls
setlocal enabledelayedexpansion
set /p "filename=type file name>"
rem initialise counter to 1
set _count=1
for /r E:\ %%a in (*) do if "%%~nxa"=="%filename%" (
  echo %%~dpnxa >>path!_count!.txt
  rem increment the counter
  set /a _count+=1
  )
endlocal

Notes:

  • I have moved the setlocal line in order to stop the variable filename leaking into the calling cmd shell.

Further Reading

Related Question