0

I am using this code to move files from subfolders of a dir and then delete the subfolders and the dir itself. then create an empty directory with the same name for the next time, problem is if any of dir, subfolder or any file contains spaces in name they the code doesn't work else it wokrs fine for me. help running windows 7

for /R "G:\test\NEW" %%f in (*.mov) do move %%f "G:\test"
pause
@echo off
set /p a=Delete Empty folders(Y/N)
IF /I "%a%"=="y" goto first
IF /I "%a%"!="y" goto second
:first
rmdir G:\test\NEW /s /q
md "G:\test\NEW"
:second
Exit 
Ryan
  • 37
  • 3

3 Answers3

0

If the filename can have a space in it, then you need to quote it.

for /R "G:\test\NEW" %%f in (*.mov) do move "%%f" "G:\test"
lit
  • 14,456
  • 10
  • 65
  • 119
0

By adding double quotes should solve.

for /R "G:\test\NEW" %%f in (*.mov) do move "%%~f" "G:\test"
pause

@echo off
set /p "a=Delete Empty folders (Y/N) "
IF /I "%a%"=="y" goto first
IF /I "%a%" neq "y" goto second

:first
rmdir /s /q "G:\test\NEW"
md "G:\test\NEW"

:second
Exit 
Paul
  • 2,620
  • 2
  • 17
  • 27
0

There are several issues in your code:

  • there are double-quotes missing around most path specifications;
  • the comparison operator != (in your second if) is invalid; I simplified the logic a bit;
  • within the for /R loop you are modifying the directory tree contents which you are iterating through, which is not a good idea, because this might lead to unexpected results -- see this post concerning that topic; therefore it is better to use dir /B /S to list the directory tree contents and let for /F parse the returned list;

The following snippet should work as expected:

for /F "eol=| delims=" %%F in ('dir /B /S "G:\test\NEW\*.mov"') do move "%%~fF" "G:\test\"
pause
@echo off
set /P A=Delete empty folders (Y/N)? 
if /I not "%A%"=="y" exit /B
rmdir /S /Q "G:\test\NEW"
mkdir "G:\test\NEW"
Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99