Page 1 of 1

Trying to adapt Batch file to work on folder/file names with spaces

Posted: 10 Jan 2017 04:20
by gearer
Hi.

I am trying to adapt this batch file(made by adrian from wdtvforum.com) to work on folder/file names with spaces:

FOR /F "delims=*" %%A IN ('dir /b /s *.mkv') DO CALL:WDTVFIX "%%A"
START "" logging.txt
GOTO :eof
:WDTVFIX
mkclean --optimize %~dpnx1 %~dpn1_fixed.mkv
if not errorlevel 0 (
ECHO Failed on %~nx1 remux! >> logging.txt
goto :eof)
ECHO OFF
if exist "%~dpn1_fixed.mkv" (
del "%~dpnx1"
ECHO Processing of %~nx1 successful! >> logging.txt
goto :eof


When using it, it works perfectly with D:\Test\test1.mkv, but does not work on D:\Test\test 1.mkv.

I am not in any way skilled in programming, so I hope someone can help me.

Thanks!
Gearer

Re: Trying to adapt Batch file to work on folder/file names with spaces

Posted: 10 Jan 2017 11:29
by penpen
You only have to encapsulate any file (or directory) path in doublequotes and use command arguments/for variables with tilde (to avoid multiple encapsulation) to work with them without errors:

So the first lines should be changed to:

Code: Select all

FOR /F "delims=*" %%A IN ('dir /b /s "*.mkv"') DO CALL:WDTVFIX "%%~A"
START "" "logging.txt"
GOTO :eof
The other lines should be adjusted in the same way.

penpen.

Re: Trying to adapt Batch file to work on folder/file names with spaces

Posted: 10 Jan 2017 12:40
by Compo
This is your problem:

Code: Select all

mkclean --optimize %~dpnx1 %~dpn1_fixed.mkv
fixed as:

Code: Select all

mkclean --optimize %1 "%~dpn1_fixed.mkv"

Re: Trying to adapt Batch file to work on folder/file names with spaces

Posted: 10 Jan 2017 13:37
by gearer
Compo wrote:This is your problem:

Code: Select all

mkclean --optimize %~dpnx1 %~dpn1_fixed.mkv
fixed as:

Code: Select all

mkclean --optimize %1 "%~dpn1_fixed.mkv"


This fixed the issue with spaces, but now the batch file does not continue to the next file. Also, the "delete old file" stopped working. Any ideas?