Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have this batch script that allows me to copy folders and its content from a text file, could someone help me adjust it so it move/cut the folders+content and not just copy?

  set file_list=ready.txt
    set dest_folder=c:\small\final
    for /f "delims=" %%a in (%file_list%) do (
    xcopy /Y "%%a*" "%dest_folder%%%~pa"
    )
share|improve this question
    
All you need is to change xcopy ... to move .... Look at move /? for specifics. –  nerdwaller Dec 3 '13 at 13:01
    
I tried that, it gives me an error "Cannot move multiple files to a single file." –  user2827759 Dec 3 '13 at 13:08
    
That is one of the things you should have posted in your original question. In the future, be sure to include all relevant information (i.e. the problem, things you tried, what errors you saw, etc.) and update your question with things like that. Glad you got this answered, welcome to Super User! –  nerdwaller Dec 3 '13 at 15:38
add comment

1 Answer

up vote 1 down vote accepted

Please note: you can't use wildcards with move.

(and please make file_list -> dir_list for clarity.)

@echo off
Setlocal EnableDelayedExpansion
set dir_list=ready.txt
set dest_folder=c:\small\final\ok
mkdir %dest_folder%
for /f "delims=" %%a IN (%dir_list%) do (
   set dir=%%a
   if "!dir:~-1!"=="\" set dir=!dir:~0,-1!
   move "!dir!" "%dest_folder%\"
)

For directories in ready.txt with trailing \ the \ will be removed before the move.


Edit:

Since you are moving the directories from one drive to another you can't use move. Here is a solution with robocopy:

@echo off
Setlocal EnableDelayedExpansion
set dir_list=ready.txt
set dest_folder=d:\small\final\ok\
if "%dest_folder:~-1%"=="\" set dest_folder=%dest_folder:~0,-1%
for /f "delims=" %%a in (%dir_list%) do (
  set dir1=%%a
  if "!dir1:~-1!"=="\" set dir1=!dir1:~0,-1!
  for %%f in (!dir1!) do set dir2=%%~nxf
  robocopy "!dir1!" "%dest_folder%\!dir2!" /MOVE /E
)

Please note robocopy can only copy and move entire directories and not single files. So your ready.txt should only consists of directories. Trailing \ are taken care of and can be mixed.

Robocopy needs the complete directory name for the new directory, that's why we need the
for %%f-line (to get te last directory-part of the source-directory.) So it needs
robocopy c:\sub\meme d:\sub\meme. You can't do robocopy c:\sub\meme d:\sub.

You can test this batchfile by putting echo before the robocopy-file
(and before the move in the previous example).

share|improve this answer
    
Was your first sentence intended to be can't use wildcards? Also, the safer thing to do is mkdir the destination first, and add a backslash (`) to the end of the folder name (e.g. %dest_folder%`). The mkdir will create it if it doesn't exist, and the backslash means it will either move into the directory or throw an error (if not exist) - rather than renaming anything. –  Bob Dec 3 '13 at 13:39
    
Thanks for the attempt however That didn't work, i get The system cannot find the file specified. –  user2827759 Dec 3 '13 at 13:48
    
@Bob Yes, tnx. I forgot 2 characters. (Also added the mkdir.) About the \ . It depends... if the directory is successfully created a \ wouldn't be necessary. And if the %dest_folder% is changed to include the \ all goes wrong. (I'll add it with a warning) –  Rik Dec 3 '13 at 13:51
    
@user2827759 Could you change the line move xxx to echo move xxx and post the result in your answer (via edit). We don't have all the files (e.g. we don't know what you have in ready.txt). –  Rik Dec 3 '13 at 13:52
    
@Rik Doubled up backslashes shouldn't matter - the system will treat any number of consecutive backslashes the same as a single one. Yea, it's not strictly necessary after mkdir, but it also doesn't hurt ;) –  Bob Dec 3 '13 at 13:54
show 12 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.