How to handle directories with spaces?

Related searches

I want to use PowerShell with robocopy to move a large number of files. The code below works if there are no spaces in any directory. How do I improve this to handle directories with spaces?

$workingDirectory = Split-Path -Parent $PSCommandPath
$sourceDir = "$workingDirectory\source folder"
$targetDir = "$workingDirectory\target folder"
$logFile = "$workingDirectory\log.txt"
$options = "/copyall /b /is /r:5 /w:5 /log:$logFile"
Start-Process robocopy -args "$sourceDir $targetDir $options" -NoNewWindow -PassThru -Wait

Ok, I see the error when using Start-Process:

ERROR : Invalid Parameter #3 : "...\target"

However, running robocopy directly requires no special quoting. Note that /copyall requires admin privs.

robocopy $sourcedir $targetdir /copyall /b /is /r:5 /w:5 /log:$logFile

How to cd into a directory with space in the name?, When defining DOCS , you do need to escape the space character. You can quote the string (using either single or double quotes) or you can escape just the space character with a backslash. You can't do both. (On most Unix-like systems, you can have a backslash in a file or directory name, though it's not a good idea. To to use files with spaces you can either use the escape character or youse the double quotes. example: cd new\ dir/ \ is called escape character, used to not expansion of space, so now bash read the space as part of file name. Or you can use: cd "new dir"

Without knowing the specific error it's tough to say exactly. You can add escaped quotes around $sourceDir and $targetDir so that PowerShell doesn't strip the quotes out when starting a new process to execute robocopy.

Start-Process robocopy -args `"$sourceDir`", `"$targetDir`", $options -NoNewWindow -PassThru -Wait

command line, You can enclose the whole path by double-quotes ("), single-quote (') or escape the space character using a backslash (\) : Long filenames or paths with spaces are supported by NTFS in Windows NT. However, these filenames or directory names require quotation marks around them when they are specified in a command prompt operation. Failure to use the quotation marks results in the error message.

Based on the comment from Ansgar Wiechers, I was able to figure it out. Thanks :-)

$workingDirectory = Split-Path -parent $PSCommandPath
$SourceDir = "$workingDirectory\source folder"
$targetDir = "$workingDirectory\target folder"
$logFile = "$workingDirectory\log.txt"
$options = "/copyall /b /is /r:5 /w:5 /log:$logFile"
& robocopy "$sourceDir" "$targetDir" /copyall /b /is /r:5 /w:5 "/log:$logFile"

How to cd to a directory that contains a space in its name?, 5 Answers. Either you put quotes around the directory name ( cd "/Users/niho/Desktop/Reader 0.5" ) or you escape the directory name ( /Users/niho/Desktop/Reader\ 0.5 ). As others have mentioned, quoting the path or backslash-escaping the spaces will work. If a path contains two or more file names with spaces … You can do the same. You can add apostrophes separately to each name, or you can put that particular path which contains names with spaces, between two apostrophes, as a whole, which is much easier.

How To Deal With Spaces in Paths on Mac and Linux, If you've ever had to use paths with spaces on the macOS or Linux command line , you might wonder why it's such a hassle, and what's the best� find . -type d -exec tar cf {}.tar {} \; to compress all directories into individual tarballs (one tar per directory, since \; makes find run the -exec command with one result at a time) find . -type d -exec tar cf all-directories.tar {} + to compress all directories into a single tarball ( + makes find run the -exec command with as many

Spaces in file and directory names, There are three ways that I know of to handle this. They are: Drag-and-drop the file or directory onto the terminal; this will preserve the spaces. I am trying to use robocopy but am unable to make it work because of spaces in the directory names. I am trying to copy 3 directories: My Documents, My Music and My Pictures to 'C:\test-backup' but want the end result to be 'C:\test-backup\My Documents' 'C:\test-backup\My Music' 'C:\test-backup\My Pictures' My command does not work:

In your particular situation, you need to leave the tilde and the following slash unquoted while quoting the space in the directory name. Your cd "~/Library/Application Support/" trial ends up quoting too much (the tilde and its slash), while your cd ~/Library/Application Support/ trial quotes too little (it omits quoting the space in the directory name).

  • Start-Proces robocopy ... -> & robocopy "$sourceDir" "$targetDir" $options
  • @js2010 The error message comes from robocopy and says 'ERROR : Invalid Parameter #3 : "C:\temp\target"' due to lack of quotes around the path strings.