Question:
how to operate on folders which contain a space in its name using cmd?
khaja m
2007-09-25 10:15:46 UTC
for example to copy a file from desktop to a folder in D: drive which has a space embeded in it , for example to copy a file
"abc.txt" to folder "new folder".
Six answers:
connie111
2007-09-25 10:39:56 UTC
Hello There!



Use the Subst (subsitute) command and assign a drive letter.. . maybe G: or K: to the path to the directory where you have "new folder" (something with a space in the name)



I just made a directory called test. Than a sub directory called "new folder" . I use the command md "new folder" (there is a space between the md and "new folder") to create the directory with the space in the name. YOU MUST PUT THE QUOTES on either end of the name with a space in it.



Now, for you to work with these 'folders' that have spaces in them you must put them in quotes.



Then I traveled the path using the cd command to get to the "new folder" using quotes around the directory name with a space in, from the one directly above it.



Once there, I used the SUBST command and typed in at the prompt Subst K: . That is Subst K:(space period) to assign the Drive Letter K: to that current directory.. .which is "new folder"



Now I can copy files or what ever by simply typing



copy *.doc K: to copy files to "new folder" or



copy K:*.* c:\test to copy files from "new folder" to the directory called test.



The Subst command is very helpfull in these mixed name space environments.



When you have finished using the Subst command to assign the "new folder" a drive letter, you can actually type subst again and it will display the drive letter you have assigned and the directory path that you assigned the drive letter to. Then you will be able to see that you, in fact, have assigned a Letter to a directory or folder path.





And this to Wyatt:



It really depends on what you are doing as to which answer is best. And renaming the folder to remove a 'space' in the name could cause problems with something else. ..Like changing "My Documents" or "My Pictures" or "My Videos" might cause other programs who call these places internally to NOT WORK!



And using quotes and underscores can be a pain. Just assigning a drive letter with the subst command is really easy and you can even make it permanent and never have to worry about it again. Before you judge it as too complicated, why don't you just try it.
2007-09-25 17:25:12 UTC
Someone asked a question similar to this a while back, and the answer is, try putting an underscore ( _ ) where the spaces are in the folder names. If this doesn't work, try renaming the folder by removing the space, and adding an underscore where the space was. As a general rule, when dealing with folders, files, etc, that you want to access programmatically, it is best to NEVER include spaces in the names. Instead, use an underscore. It will save you a lot of headache in the future. Good luck.
Eric V
2007-09-25 18:21:34 UTC
Yes, manually putting quotes around it will work.



However, here is a VERY useful shortcut that I guarantee you will find super nifty:



Start typing the name of the folder or filename, and once you have a couple letters in, simply hit the 'tab' button. This is called 'tab completion' and the command window automatically fills in the rest of the folder or file name for you.



If the folder name or filename has spaces in them, tab completion will automatically put quotation marks around the whole thing.



If you have multiple folders starting with the same letters, (i.e. My Music, My Documents, My Pictures...) just keep hitting the tab button until the folder/file you want is displayed.



For example: try hitting this sequence of keys:



cd C:\Pr\Mi



On my machine, it gets expanded to:

cd "C:\Program Files\Microsoft Games" automatically (it depends what folders you have starting with Mi...)



On linux terminals this works pretty much the same. The tab completion is my best friend when it comes to command line file manipulation.
Wyatt
2007-09-25 17:57:50 UTC
holy cr@p connie111, could you make the answer any more complicated? the first two guys are perfectly correct and their solution is perfectly easy to understand and use. sometimes the best answer really is the shortest one.



to connie111:

sorry you took offense, but dude simply wants to copy a file at the command prompt, not save the computer world from typing extra quotes.



"My Documents" wow you're right, those two extra key strokes have caused me a repetitive motion injury...
2007-09-25 17:22:47 UTC
You should use quotemarks before and after the path like this: copy "C:\Documents and Settings\*.*" "D:\Documents and Settings"
Barkley Hound
2007-09-25 17:22:33 UTC
Putting quotes around it might work.

*


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...