在名称批处理脚本中移动带空格的文件

Moving files with spaces in name batch scripting

所以我正在尝试模仿 Apple 的某些功能

我目前正在开发的一个功能是能够 select 一个或多个文件,然后通过 "context menu" 创建一个 "new folder" (定向到一个批处理文件传递文件通过 FOR 循环)然后将文件移动到 "new folder"

我遇到的问题是带有空格的文件名,我使用 "robocopy" 进行了研究,但发现它有点棘手

到目前为止我的代码

@echo off
set cDir=%~dp1
set newFolder="%cDir%NewFolder"
md %newFolder%
echo.
:: get each selected file and echo
for %%I in (%*) do (
    echo %%I
    echo.
    echo %newFolder%
    move "%%I" "%newFolder%"
    echo.
)
pause

您修改后的代码应该也可以移动文件和文件夹。为了安全起见,这是我的代码变体。请注意 setmdmove 语句中的引用更改,但我重复:您的(修改后的)引用变体也应该有效:

@echo off
set cDir=%~dp1
set "newFolder=%cDir%NewFolder"
md "%newFolder%"
echo.
:: get each selected file and echo
for %%I in (%*) do (
    echo %%I
    echo.
    echo %newFolder%
    move "%%~I" "%newFolder%\"
    echo.
)
pause

尽管 move /? 移动文件并重命名文件和目录,但源和目标都可以是 文件夹 或单个文件(resource,在我的 Win-8 上验证)。

证明。

==>move "D:\Path\COCL\bu bu bu" "D:\Path\content\"
        1 dir(s) moved.

==>move "D:\Path\content\bu bu bu" "D:\Path\COCL\"
        1 dir(s) moved.

==>

下一个资源:Command Line arguments (Parameters).