Windows file names with spaces

How do I use spaces in the Command Prompt?

How can I use spaces in the Windows Command Line?

11 Answers 11

Single quotation marks won’t do in that case. You have to add quotation marks around each path and also enclose the whole command in quotation marks:

I just figured out that for a case where the path involves the use of white space characters, for example, when I need to access the app xyz which location is :

To run this from windows cmd prompt, you need to use

If double quotes do not solve the issue then try e.g.

to get a list of alternative file or directory names. Example output:

Now use the short 8 character file or folder name in the 5th column, e.g. PROGRA

1.XML, in your commands. For instance:

Enclose the paths containing spaces with double quotes.

I prefer to enclose the command in () which is valid batch which makes it a bit easier to read:

Try to provide complex pathnames in double-quotes (and include file extensions at the end for files.)

CMD interprets text with double quotes («xyz») as one string and text within single quotes (‘xyz’) as a command. For example:

FOR %%A in (‘dir /b /s *.txt’) do (echo «%%A»)

And one good thing, cmd is not* case sensitive like bash. So «New fiLE.txt» and «new file.TXT» is alike to it.

*Note: The %%A variables in above case is case-sensitive (%%A not equal to %%a).

this worked for me in a batch file

Just add Quotation Mark

Example:«C:\Users\User Name»

Hope it got Solved!

You should try using quotes.

Spaces in the Commend Prompt (in a VBA Shell command code line)

I had a very similar problem which ended up being a space in the command prompt when automating via VBA to get the contents from the command window into a text file. This Thread was one of many I caught along the way that didn’t quite get me the solution.

So this may help others with a similar problem: Since the syntax with quotes is always difficult to get right , I think showing some specific examples is always useful. The additional problem you get using the command prompt in VBA via the Shell thing, is that the code line often won’t error when something goes wrong: in fact a blink of the black commend window misleads into thinking something was done.

As example… say I have a Folder, with a text file in it like at

The space there in the folder name gives the problem.

Something like this would work, assuming the Folder, AlansFolder, exists

This won’t work. (It won’t error).

Including quote pairs around the path will make it work

( By the way, if the text file does not exist, then it will be made).

With the benefit of hindsight, we can see that my solution does tie up approximately with some already given..

Converting that code line to a manual given command we would have

That seems to work

This works also

This final form also works and ties up with the solution from sacra ….” You have to add quotation marks around each path and also enclose the whole command in quotation marks “ …..

Windows file names with spaces

This forum is closed. Thank you for your contributions.

Answered by:

Question

This is something I have been meaning to figure out for a while, but never quite got around to it.

Most of my scripts process some kind of file (and/or folders), and by far the simplest method I have used is to pass the file(s) to a script using the right click -> Send To short cut. Back in the day when creating Batch files, this was very common, but with PowerShell it seems a little more awkward, or perhaps I just haven’t figured out the simple solution. Why use PS then? Well, it’s just far cleaner and more powerful than a Batch file.

Читайте также:  Удалить сервер лицензирования windows

The Send To short cut simply invoke the Powershell Command Command interpreter with my script, and the files are passed in as argument into $args:

By and large this works just fine. as long as the file names do not have spaces within them, otherwise each part of the file name with a space gets passed in as separate parameter in the $args array.

At the moment I cheat, and run a little test that breaks out of the ForEach loop that checks the parameters in $args, to see if they are valid file names, thus:

But I suspect there is a far cleaner way of doing this, or should I just somehow recursively loop, concatenating the parameters until we have a valid file name?

Windows file names with spaces

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Answered by:

Question

I have a file share where they aren’t supposed to put spaces in the file names, because it causes problems in another one of our systems. Is there an easy way to search my file share to find any files with a .jpg extension that have a space in the file name anywhere?

I tried doing a search for * *.jpg but that didn’t work, it returned everything (i’m guessing because of the space). I can’t search through the command prompt either, so it has to be through the search gui.

Answers

I looked into this some more.

If the Indexing service is running, and your users can open the Indexing Service console, they can use the query form there. This GUI is the MMC console ciadv.msc. In it, you can double click a catalog to expose a link to the query form. The advanced query #FileName «* *.jpg» works.

I created a customized console by opening mmc and selecting F ile\add/remove snap-in. I added the Indexing service snap-in. I drilled down to the query form and select V iew\customize and unchecked both Console Tree and Action Pane. Then I saved the console with a name of my chosing (.msc). You could create a shortcut to such a file. The users would still have to enter the query.

If the Indexing service is enabled, the Windows search GUI, aka Search companion or Search assistant has a link to the the Indexing service GUI. At least it did on my XP Pro x64 machine where the Indexing service is enabled.

On my Server 2003 R2 x64 machine, it was harder to get to but I finally did it. I selected «Change preferences», then «Without Indexing service» and then «Change Indexing Service settings (advanced)». Then the Indexing service console opened.

Can GNU make handle filenames with spaces?

I have a directory containing several files, some of which have spaces in their names:

I use GNU’s $(wildcard) command on this directory, and then iterate over the result using $(foreach) , printing everything out. Here’s the code:

Here’s what I would expect to see printed out:

Here’s what I would actually get:

The latter is obviously of no use to me. The documentation for $(wildcard) flat-out states that it returns a «space-separated list of names» but completely fails to acknowledge the huge problems this raises. Nor does the documentation for $(foreach) .

Is it possible to work around this? If so, how? Renaming every file and directory to remove the spaces is not an option.

5 Answers 5

The bug #712 suggests that make does not handle names with spaces. Nowhere, never.

Читайте также:  Настройки вида проводника windows 10

I found a blog post saying it’s partially implemented by escaping the spaces with \ ( \\ seems to be typo or formatting artefact), but:

  • It does not work in any functions except $(wildcard) .
  • It does not work when expanding lists of names from variables, which includes the special variables $? , $^ and $+ as well as any user-defined variable. Which in turn means that while $(wildcard) will match correct files, you won’t be able to interpret the result anyway.

So with explicit or very simple pattern rules you can get it to work, but beyond that you are out of luck. You’ll have to look for some other build system that does support spaces. I am not sure whether jam/bjam does, scons, waf, ant, nant and msbuild all should work.

GNU Make does very poorly with space-separated filenames.

Spaces are used as delimiters in word list all over the place.

This blog post summarizes the situation well, but WARNING: it incorrectly uses \\ rather than \

You can also use variables, so this would also work

Only the wildcard function recognizes the escaping, so you can’t do anything fancy without lots of pain.

But don’t forget that your shell uses spaces as delimiters too.

If I wanted to change the echo done to touch $@ , I’d have to add slash to escape it for my shell.

or, more likely, use quotes

In the end, if you want to avoid a lot of pain, both in GNU make, and in your shell, don’t put spaces in your filenames. If you do, hopefully the limited capabilities of Make will be sufficient.

This method will also allow use of listed file names such as $? and user variables that are lists of files.

The best way to deal with spaces in Make is to substitute spaces for other characters.

You can then safely manipulate lists of file names using all the GNU Make functions. Just be sure to remove the +’s before using these names in a rule.

This info is all from the blog that everyone else was posting.

Most people seem to be recommending using no spaces in paths or using Windows 8.3 paths, but if you must use spaces, escaping spaces and substitution works.

If you are willing to rely on your shell a bit more, this gives a list which can hold names with spaces just fine:

The original question said that «renaming is not an option», yet many commenters have pointed out that renaming is pretty much the only way Make can handle spaces. I suggest a middle way: Use Make to temporarily rename the files and then rename them back. This gives you all the power of Make with implicit rules and other goodness, but doesn’t mess up your file naming scheme.

You could call these targets by hand with make nospaces and make yesspaces , or you can have other targets depends on them. For example, you might want to have a «push» target which makes sure to put the spaces back in filenames before syncing files back with a server:

[Sidenote: I tried the answer which suggested using +s and s+ but it made my Makefile harder to read and debug. I gave up on it when it gave me guff over implicit rules likes: %.wav : %.ogg ; oggdec «$ .]

how to make batch file handle spaces in file names

I have the following batch file to make git diff invoke spreadsheet compare UI in windows. So I’m trying to pass the git diff’s 2nd (old file) and 5th (new file) arguments to spreadsheet compare in order to make it compare the file using git diff.

So now, this batch file only successfully handles files with NO spaces in the file names, it CANNOT handle files with spaces in the file names.

Читайте также:  Mac os как создать архив rar

What code should I add to this script to make this batch code handles file with spaces:

It currently throw errors like this:

5 instead of %5 . Additionally the recommended syntax for the set command is Set «VariableName=VariableValue» . I would therefore suggest the following in your script above, Set «path2=%

5″ , to remove the surrounding doublequotes, or Set «path2=%5» if you’re happy to keep them. – Compo Jun 28 ’19 at 22:36

2 Answers 2

See the following answers on Stack Overflow:

  1. How to set environment variables with spaces?
    Why is no string output with ‘echo %var%’ after using ‘set var = text’ on command line?
    They explain the recommended syntax set «VariableName=variable value» to define an environment variable and the reasons recommending this syntax.
  2. Why does ECHO command print some extra trailing space into the file?
    It explains why the space character left to redirection operator > on an ECHO command line is also written into the file as trailing space and how to avoid this safely on variable text written into the file.
    See also Microsoft documentation about Using command redirection operators.
    On other command lines than ECHO a space left to > is usually no problem.

It is in general wrong to use multiple times » within an argument string like a file or folder path. There should be just one » at beginning and one » at end. This is explained by help of Windows command processor output on last help page on running in a command prompt window cmd /? .

The Microsoft documentation about Naming Files, Paths, and Namespaces explains that the directory separator on Windows is \ and not / and therefore / should not be used in batch files on Windows in file/folder paths.

The help output on running in a command prompt window call /? explains how the arguments of a batch file can be referenced with which modifiers.

The code rewritten according to information posted above and on the referenced pages:

The first line in tmp.txt contains the second argument as passed to the batch file, i.e. without or with surrounding double quotes.

The following code is necessary to write the second argument safely always without » into file tmp.txt even on second argument passed to the batch file is «Hello & welcome!» :

2 cannot be used as not working for something like «Hello & welcome!» . Windows command processor would interpret the first string separated by normal space, horizontal tab, comma, equal sign, or no-break space (in OEM code pages) delimited string after & as command or application to execute as described by single line with multiple commands using Windows batch file.

«tmp.txt» could be written everywhere in both batch files also with just tmp.txt . But it is never wrong to enclose the complete file/folder argument string in double quotes even on not being really necessary because of the string does not contain a space or one of these characters &()[]<>^=;!’+,`

. So it is good practice to always enclose a complete file/folder argument string in double quotes. For example running a replace on both batch files searching for tmp.txt and using as replace string %TEMP%\%

n0.tmp would result in using instead of tmp.txt in current directory a temporary file with name of batch file as file name and file extension .tmp in directory for temporary files independent on what is the name of the batch file and what is the path of the directory for temporary files.

The last suggestion is reading this answer for details about the commands SETLOCAL and ENDLOCAL.

The temporary file should be also deleted finally before reaching an exit point for batch file execution.

Оцените статью