Batch file moving files with spaces in their name.
Solution 1
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
Solution 2
You need to place quotes around files that might have spaces in the name, e.g.
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")
Idea Bak
Updated on June 04, 2022Comments
-
Idea Bak almost 2 years
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.
-
Idea Bak over 11 yearsStill need help with the first part though. Still wont pull files with spaces in there names out of sub-folders.
-
Eric J. over 11 years@IdeaBak: Did you quote your variable
DO MOVE "%%i"
? -
David Ruhmann over 11 yearsDoes your Part 1 look like this:
FOR /R "C:\Users\Laptop02\Desktop\Folder 1 Test" %%i IN (*.*) DO MOVE "%%i" "C:\Users\Laptop02\Desktop\Folder 1 Test"
? Anytime you are using a literal string containing spaces, the spaces need to be encapsulated by using surrounding quotations. -
Idea Bak over 11 years@David it did not, how ever changing it to what you have has not worked either. I thank you for your help though.
-
David Ruhmann over 11 years@IdeaBak is it displaying any error messages when running the batch script? That can usually narrow the search for the issue. Are there any other special characters in the filenames?
"()!&
-
Idea Bak over 11 yearsIt looks as though nothing happens, a quick fast then nothing.
-
David Ruhmann over 11 yearsAre you running the script from an already open cmd prompt? If not the script will immediately close when an error occurs and if a
pause
is not specified at the end of the script. Also, I tested my snippet and it seems to work just fine on filenames containing spaces. -
Idea Bak over 11 yearsNo I running them as .Bat files because I use a scheduler to make the moves every morning while I am at work.
-
Eric J. over 11 years@IdeaBak: Updated my answer. You need to add something to the for command.
-
David Ruhmann over 11 years@IdeaBak A good method for testing bat files when developing them is to open a cmd console and run the bat file within the console, that way all the errors are printed out to the screen of the console.
-
David Ruhmann over 11 years@EricJ. Unfortunately, I do not believe the
delims
option is supported by the/r
recursive mode. The default for the recursive mode is to place then entire file path into the variable specified. See technet.microsoft.com/en-us/library/bb490909.aspxParsingKeywords
is only supported for the/f
mode. -
Eric J. over 11 years@DavidRuhmann: You can ditch /r and instead use (dir /b /r). Will update my answer.