Pergunta

I have a problem. I have many text file (.txt). I want to write the name of the corresponding text files inside them and finally merge them. I have lots of them. I cant do it manually, its too time consuming.

Suppose, i have a text file named "windows.txt" and its contents are

Windows XP
Windows 7
Windows 8

I want their contents to be

windows
Windows XP
Windows 7
Windows 8

After merging similar files they should look like

windows
Windows XP
Windows 7
Windows 8

continents
Europe
Asia
Africa

languages
English
Chinese

Please HELP !!

Foi útil?

Solução

@echo off
echo. >result
for /f %%i in ('dir /b /a-d *.txt') do (
 echo.
 echo %%~ni
 type %%i
)>>result
ren result result.txt

Pseude-Code for better understanding:

turn echo off
delete result file
for all txt files in current directory do:
 print empty line
 print name of textfile without extension
 print textfile
and put it into new file "result"
if done, rename the resulting file

Outras dicas

May be too easy but I think it's valid answer:

type *.txt >result 2>&1
ren result result.txt

The trick is when type is used with list of files or wild cards it prints file names in error stream.And its not redirected to file with txt extension to avoid its print too but is renamed at the end.

concerning names of the files written into them (better test it first)

@echo off
for %%a in (*.txt) do ( 
 type %%~na.txt* >%%~na 2>&1
 del  /q /f %%~na.txt
 ren %%~na %%~na.txt
 )

EDIT

Probably will work if 8.3 notation is enabled:

@echo off
ren ????????.txt ???????? >nul 2>&1
for /f "tokens=* delims=" %%# in ('for %%a in (????????^) do @echo %%a') do ( 

 echo %%#>%%~#_
 rem echo %%~n#_
 type %%~n# >>"%%~#_" 2>nul
 del  /q /f "%%~n#"
 ren "%%~n#_" "%%~n#.txt"
)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top