Solved split filename over batch on Windows 8 client

February 5, 2018 at 07:20:15
Specs: Linux x86_64
split filename over batch on Windows 8 client

I have files in the form: (Datum)-<Verwendung>.txt and I need the part after the date in one seperate txt file written for all txt files in the folder.
I use batch skript with extention *.bat
There are different count of the files
sample: (2018-02-08)-Straßenabschnitt.txt


See More: split filename over batch on Windows 8 client

Reply ↓  Report •

#1
February 6, 2018 at 01:20:10
If all your files are in the same form like this : (2018-02-08)-Straßenabschnitt.txt; you can give a try for this batch script :

@echo off
Title Split Text files
set /a "count=0"
set "CurrentDirectory=%~dp0"
Set "OutPutFile=%~n0.txt"
If exist "%OutPutFile%" Del "%OutPutFile%"
setlocal enabledelayedexpansion
for /f "delims=" %%f in ('Dir /a-d /b "%CurrentDirectory%*.txt" ^| find "-"') do (
	set /a count+=1
	for /f "tokens=1-4 delims=-" %%a in ("%%f") do (
		set "file[!count!]=%%d"
	)
)
for /L %%i in (1,1,%count%) do (
	If "!file[%%i]!" NEQ "" echo !file[%%i]! && (echo !file[%%i]!)>>"%OutPutFile%"
)
pause & Start "" "%OutPutFile%" & exit

message edited by Hackoo


Reply ↓  Report •

#2
February 6, 2018 at 06:47:06
thanks for the help.

there are 2 forms: (2018-02-08) Kerstin.txt and that above. Can it be written without user interaction , only call the batch script and write into txt file without the extention txt ?


Reply ↓  Report •

#3
February 6, 2018 at 07:10:00
✔ Best Answer
Give a try for this modification :

@echo off
Title Split Text files
set /a "count=0"
set "CurrentDirectory=%~dp0"
Set "OutPutFile=%~n0.txt"
If exist "%OutPutFile%" Del "%OutPutFile%"
setlocal enabledelayedexpansion
for /f "delims=" %%f in ('Dir /a-d /b "%CurrentDirectory%*.txt" ^| find "-"') do (
	set /a count+=1
	for /f "tokens=1-4 delims=- " %%a in ("%%f") do (
		set "file[!count!]=%%~nd"
	)
)
for /L %%i in (1,1,%count%) do (
	If "!file[%%i]!" NEQ "" (echo !file[%%i]!)>>"%OutPutFile%"
)
Start "" "%OutPutFile%" & exit

And what did you mean by without user interaction ?


Reply ↓  Report •

Related Solutions

#4
February 6, 2018 at 23:52:18
Thanks for the solution . I did remove the exit to keep the DOS window open. I tried with set ß=á that it is displayed as Straßen, but it is still displayed Straáen in the txt file. Do you have a tip?

Reply ↓  Report •

#5
February 7, 2018 at 03:51:44

Reply ↓  Report •

#6
February 7, 2018 at 04:09:33
Thanks for the info.

Reply ↓  Report •

#7
February 9, 2018 at 02:25:55
Give a try for this modification inspired from this link : Can I get access to an ascii character not in the standard set ?

@echo off
setlocal enableExtensions enableDelayedExpansion
::ÿþ
set "cp="
for /F "tokens=2 delims=:." %%a in ('chcp') do set "cp=%%~a"
if not defined cp set "cp=850"
>nul chcp 65001

set /a "count=0"
set "CurrentDirectory=%~dp0"
Set "OutPutFile=%~n0.txt"
If exist "%OutPutFile%" Del "%OutPutFile%"
for /f "delims=" %%f in ('Dir /a-d /b "%CurrentDirectory%*.txt" ^| find "-"') do (
	set /a count+=1
	for /f "tokens=1-4 delims=- " %%a in ("%%f") do (
		set "file[!count!]=%%~nd"
	)
)
for /L %%i in (1,1,%count%) do (
	If "!file[%%i]!" NEQ "" (echo !file[%%i]!)>>"%OutPutFile%"
)
>nul chcp %cp%
Start "" "%OutPutFile%" & exit


Reply ↓  Report •

#8
February 9, 2018 at 02:27:48
I have already solution.

Reply ↓  Report •

Ask Question