简体   繁体   中英

Batch file moving files with spaces in their name.

My hope is that some can help me out I've spent the last few days searching Google for answers and not getting anywhere. I have a two part code fist pulls everything out of sub-folders and then the second part moves those files by type into other folders located else where. I can get the code to work in a test set up, but it wont on the files I'm trying to move. I think it has to do with the names of the files having spaces in them, but I am not sure. Here are the codes. thanks guys!

Part one

FOR /R C:\Users\Laptop02\Desktop\Folder 1 Test  %%i IN (*.*) DO MOVE %%i C:\Users\Laptop02\Desktop\Folder 1 Test

Second Part

@echooff
set media=C:\Users\Laptop02\Desktop\Foder 2 Test
set jpg=C:\Users\Laptop02\Desktop\Foder 2 Test\mediadata
set xml=C:\Users\Laptop02\Desktop\Foder 2 Test\mediadata

move %dlDir%*.avi %media%
move %dlDir%*/*.avi %meia%
move %dlDir%*.mp4 %media%
move %dlDir%*/*.mp4 %media%
move %dlDir%*.mkv %media%
move %dlDir%*/*.mkv %media%
move %dlDir%*.jpg %jpg%
move %dlDir%**.xml %xml%

Thanks again.

You need to place quotes around files that might have spaces in the name, eg

move "%dlDir%*.avi" "%media%"

UPDATE

For the for portion, add

"delims=" 

like this:

FOR /F "delims=" IN (dir /b /s "C:\Users\Laptop02\Desktop\Folder 1 Test")

While the move command is good and the method above works, it should be faster copying or moving many files using the following robocopy command with multi-thread support:

@echo off
set "source=C:\Users\Laptop02\Desktop\Folder 1 Test"
set "media=C:\Users\Laptop02\Desktop\Foder 2 Test"
set "jpg=C:\Users\Laptop02\Desktop\Foder 2 Test\mediadata"
set "xml=C:\Users\Laptop02\Desktop\Foder 2 Test\mediadata"
robocopy /s /mov /mt "%source%" "%media%" *.avi
robocopy /s /mov /mt "%source%" "%media%" *.mp4
robocopy /s /mov /mt "%source%" "%media%" *.mkv
robocopy /s /mov /mt "%source%" "%jpg%" *.jpg
robocopy /s /mov /mt "%source%" "%xml%" *.xml
pause

Alternatively, it would also be faster to move the files only once:

@echo off
set "source=C:\Users\Laptop02\Desktop\Folder 1 Test"
set "media=C:\Users\Laptop02\Desktop\Foder 2 Test"
set "jpg=C:\Users\Laptop02\Desktop\Foder 2 Test\mediadata"
set "xml=C:\Users\Laptop02\Desktop\Foder 2 Test\mediadata"

cd "%source%"
for /r "%~dp0" %%A in (*) do (
    if "%%~xA"==".avi" move "%%~A" "%media%\"
    if "%%~xA"==".mp4" move "%%~A" "%media%\"
    if "%%~xA"==".mkv" move "%%~A" "%media%\"
    if "%%~xA"==".jpg" move "%%~A" "%jpg%\"
    if "%%~xA"==".xml" move "%%~A" "%xml%\"
)
pause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM