尝试在批处理文件中设置路径

Trying to set the path in batch file

我正在尝试从一个文件夹复制一些图片,然后使用批处理将其移动到另一个文件夹。我在设置路径时遇到问题,因为路径在 it.If 中包含空格 我从文件夹中删除空格它工作正常但有空格它给出错误 找不到路径 。 . 这是代码。

@echo off 

SET odrive=%odrive:~0,2%
setlocal enabledelayedexpansion

set backupcmd=echo
set backupcmd=xcopy /s /c /d /e /h /i /r /y

set "filesw=C:\Users\%USERNAME%\Numerical Analysis\*.png"

for /f "delims=" %%i in ('dir /s /b %filesw%') do (
  if "%%~xi"==".pdf" set "dest=D"
  if "%%~xi"==".docx" set "dest=P"
  if "%%~xi"==".zip" set "dest=Z"
  if "%%~xi"==".rar" set "dest=Z"
  if "%%~di"=="C:" if "!dest!"=="Z" set "dest=!dest!3"
  %backupcmd% "%%i" "%drive%\Personal\PICS\Wedding\Barat\MOVIE!dest!\"
)

@echo off 
cls

如果你们帮我解决这个路径问题,那将非常有帮助。

我就是这样做的。使用 FOR /R 命令遍历目录树以查找所需的文件类型。但我只是在猜测你想做什么。

@echo off 

setlocal enabledelayedexpansion

set backupcmd=xcopy /s /c /d /e /h /i /r /y

set "filep=C:\Users\%USERNAME%\Numerical Analysis"

for /R "%filep%" %%i in (.) do (
    if "%%~xi"==".pdf" set "dest=D"
    if "%%~xi"==".docx" set "dest=P"
    if "%%~xi"==".zip" set "dest=Z"
    if "%%~xi"==".rar" set "dest=Z"
    if "%%~di"=="C:" if "!dest!"=="Z" set "dest=!dest!3"
    %backupcmd% "%%i" "%drive%\Personal\PICS\Wedding\Barat\MOVIE!dest!\"
)

你的问题的解决方案,特别是 "setting the path since the path contains spaces in it" 导致 "cannot find the path" 是 "quote the filespec" 因此:

for /f "delims=" %%i in ('dir /s /b "%filesw%"') do (

这样,double-quotes之间的字符串按字面意义使用(虽然某些特殊字符有批处理的意思,如&^)!需要转义;即在前面加上插入符^).如您所见, dir 命令将使用多个参数执行,因为空格是分隔符,并且变量 filesw 将在执行前逐字替换为 dir 命令 - 与大多数命令一样, dir 使用空格(逗号、制表符、分号)作为分隔符。