Spaces in files names in command within batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
vinnie2k
Posts: 12
Joined: 18 Apr 2014 04:37

Spaces in files names in command within batch file

#1 Post by vinnie2k » 18 Apr 2014 04:43

Hi guys,

Thank you to those who already posted advice on how to build these scripts 8)

Here is mine.
It works perfectly well for all the files retrieved by the dir command which have no space in their name.
For filenames with spaces, DOS returns an error because it can't find the file specified - it most likely stops at the space.

How do I tell DOS that the filename includes a space? I tried putting double quotes around %%i but that doesn't work.

Code: Select all

FOR /F "delims=" %%i IN ('dir P: /B') DO (
   echo Opening %%i
   echo Replacing location
   echo off
   c:\Users\vmuso_000\Vincent\BatchReplace.bat "D:\Public " "/volume1/" %%i
   echo Replacing slash
   echo off
   c:\Users\vmuso_000\Vincent\BatchReplace.bat "\" "/" %%i
   echo Done!
)


Help! :mrgreen:

Thank you!
V.
Last edited by vinnie2k on 20 Apr 2014 09:19, edited 2 times in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Spaces in files names in command within batch file

#2 Post by foxidrive » 18 Apr 2014 05:43

Double quotes around the term works for spaces.

vinnie2k
Posts: 12
Joined: 18 Apr 2014 04:37

Re: Spaces in files names in command within batch file

#3 Post by vinnie2k » 18 Apr 2014 07:37

Do you mean like this?

Code: Select all

FOR /F "delims=" %%i IN ('dir P: /B') DO (
   echo Opening %%i
   echo Replacing location
   echo off
   c:\Users\vmuso_000\Vincent\BatchReplace.bat "D:\Public " "/volume1/" "%%i"
   echo Replacing slash
   echo off
   c:\Users\vmuso_000\Vincent\BatchReplace.bat "\" "/" "%%i"
   echo Done!
)


It doesn't.
Last edited by vinnie2k on 20 Apr 2014 09:19, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Spaces in files names in command within batch file

#4 Post by foxidrive » 18 Apr 2014 08:20

I don't know what you are doing in your code, but try this:

Code: Select all

@echo off
echo.@echo testing inside "a long filename.bat"^& pause >"a long filename.bat"
call "a long filename.bat"
echo Now back in the original batch file
pause

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Spaces in files names in command within batch file

#5 Post by Squashman » 18 Apr 2014 09:03

Quotes are always needed for Spaces in Filenames and it ALWAYS a good IDEA to put Quotes around all your command line arguments you are passing to another batchfile.

And as Foxidrive just pointed out you really need to use the CALL command as well if you want control to return the original batch file when you try to execute a batch file from within another batch file.

But it would help to see the code in your 2nd batch file.

vinnie2k
Posts: 12
Joined: 18 Apr 2014 04:37

Re: Spaces in files names in command within batch file

#6 Post by vinnie2k » 20 Apr 2014 09:18

Here's what I do:

Code: Select all

@echo off

REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

if /I "%~1"=="/h" goto:help
if "%3"=="" goto:help

if "%~1"=="" findstr "^::" "%~f0"&goto:help
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""')  do %%~X >> %3_new
    ) ELSE echo.
)
move /Y %3_new %3 >nul

goto:eof

:help
echo BatchSubstitute - parses a File line by line and replaces a substring
echo.
echo Usage: %0 [oldstr] [newstr] [filename]
echo.          oldstr  - string to be replaced
echo.          newstr  - string to replace with
echo.          filname - file to be parsed
echo.
goto:eof

:eof

Took it from this forum.

vinnie2k
Posts: 12
Joined: 18 Apr 2014 04:37

Re: Spaces in files names in command within batch file

#7 Post by vinnie2k » 20 Apr 2014 09:27

OK, let's use call:

Code: Select all

call "c:\Users\vmuso_000\Vincent\BatchReplace.bat "D:\Public " "/volume1/" %%i"


Issue is that call is going to execute

Code: Select all

"c:\Users\vmuso_000\Vincent\BatchReplace.bat "


and forget about the rest.
I need to tell call that the double quote before D: is not a quote like the one before c:.

How do I do that? Escape the double quote?

Code: Select all

\"


:?:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Spaces in files names in command within batch file

#8 Post by foxidrive » 20 Apr 2014 09:39

vinnie2k wrote:OK, let's use call:

Code: Select all

call "c:\Users\vmuso_000\Vincent\BatchReplace.bat "D:\Public " "/volume1/" %%i"



The question is: why are you using quoting in that way?

vinnie2k
Posts: 12
Joined: 18 Apr 2014 04:37

Re: Spaces in files names in command within batch file

#9 Post by vinnie2k » 20 Apr 2014 09:41

Because I have no idea what I'm doing? :mrgreen:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Spaces in files names in command within batch file

#10 Post by foxidrive » 20 Apr 2014 09:43

Well, yeah, that's explains it. :D

What is the task?

vinnie2k
Posts: 12
Joined: 18 Apr 2014 04:37

Re: Spaces in files names in command within batch file

#11 Post by vinnie2k » 20 Apr 2014 09:54

List files from directory.
In each file, find some characters and exchange them with others.
Done 8)

(this is useful for making m3u playlists generated on Windows compatible with Linux-type Synology NAS systems)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Spaces in files names in command within batch file

#12 Post by Squashman » 20 Apr 2014 10:09

Might want to take a look at our threads about REPL.bat and FINDREPL.bat.

Regardless of that yes your syntax is all screwed up.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Spaces in files names in command within batch file

#13 Post by foxidrive » 20 Apr 2014 10:32

I agree with aGerman: Here's an example of literal text replacement.

dir /b /s /a-d "D:\public\*.mp3" |repl "D:\Public" "/volume1/" L >file.txt



This uses a helper batch file called `repl.bat` - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
The Dostips link is inside the file.

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Spaces in files names in command within batch file

#14 Post by Squashman » 20 Apr 2014 12:07

vinnie2k wrote:OK, let's use call:

Code: Select all

call "c:\Users\vmuso_000\Vincent\BatchReplace.bat "D:\Public " "/volume1/" %%i"



Should be

Code: Select all

call "c:\Users\vmuso_000\Vincent\BatchReplace.bat" "D:\Public" "/volume1/" "%%i"

vinnie2k
Posts: 12
Joined: 18 Apr 2014 04:37

Re: Spaces in files names in command within batch file

#15 Post by vinnie2k » 20 Apr 2014 13:11

C:\Users\vmuso_000\Vincent>PlaylistWindows2SynologyConverter.bat
Opening Best Of.m3u
Replacing location
Of.m3u""=="" was unexpected at this time.

:?

Post Reply