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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gearer
Posts: 2
Joined: 10 Jan 2017 04:08

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

#1 Post by gearer » 10 Jan 2017 04:20

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

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

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

#2 Post by penpen » 10 Jan 2017 11:29

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.

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

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

#3 Post by Compo » 10 Jan 2017 12:40

This is your problem:

Code: Select all

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

Code: Select all

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

gearer
Posts: 2
Joined: 10 Jan 2017 04:08

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

#4 Post by gearer » 10 Jan 2017 13:37

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?

Post Reply