Reputation: 600
I am trying to copy some pictures from one folder and move it to another folder using batch. I am having trouble in setting the path since the path contains spaces in it.If I remove the spaces from folder it works fine but with spaces it gives error that cannot find the path.. here is the code.
@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
It would be really helpful if you guys help me fix this path problem.
Upvotes: 0
Views: 101
Reputation: 79947
The solution to your problem, specifically "setting the path since the path contains spaces in it" leading to "cannot find the path" is to "quote the filespec" Thus:
for /f "delims=" %%i in ('dir /s /b "%filesw%"') do (
In this way, the string between the double-quotes is used literally (although certain special characters with a meaning to batch like &^)!
need to be escaped; that is, preceded by a caret ^
). As you have it, the dir
command will be executed with multiple arguments since spaces are separators and the variable filesw
will be substituted literally into the dir
command before execution - and like most commands, dir
uses spaces (commas, tabs, semicolons) as separators.
Upvotes: 1
Reputation: 14290
This is how I would do it. Use the FOR /R command to walk the directory tree looking for the file type you want. But I am just guessing at what you are trying to do.
@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!\"
)
Upvotes: 1