2

This line of code fines all folders in a directory that have a certain bit of text in common and copies all the contents of those folders to a new destination.

dir /b /s /a:d "\\SERVER\Path\Directory\*FolderTag" | for /f "delims=\; tokens=3,4,5*" %%a in ('findstr FolderTag') do @xcopy /i /s /y "\\SERVER\Path\Directory\%%b\%%c" "E:\%%b\"

It works like a charm. But I'd like to be able to move the files instead of just copying them so that they are removed from their original location. I can't simply replace xcopy with move but I can't figure out how to convert the structure of this loop into one that will work with move. Would it be easier just to write another loop that deletes the files? I doubt rm instead of xcopy would work and I'm always a little scared to touch rm.

I tried putting together a working example batch script but because the loop is so dependent on the path structure, i wasn't able to get it working. So basically my question revolves around my implementation of xcopy in this loop and how it could be changed to let move work in its place.

Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
Neal
  • 199
  • 3
  • 16
  • `move` does not exactly behave the same way when it comes to directories, so it might be more reliable to keep `xcopy` and just to append `&& rm /S /Q "\\SERVER\Path\Directory\%%b\%%c"`... – aschipfl Dec 03 '16 at 14:21
  • @aschipfl So would that be added directly after the rest of the loop? `dir /b /s /a:d "\\SERVER\Path\Directory\*FolderTag" | for /f "delims=\; tokens=3,4,5*" %%a in ('findstr FolderTag') do @xcopy /i /s /y "\\SERVER\Path\Directory\%%b\%%c" "E:\%%b\" && rm /S /Q "\\SERVER\Path\Directory\%%b\%%c\%%d"`? I ask because I'm getting some errors with that. – Neal Dec 05 '16 at 17:22
  • Also `rm` isn't a windows command. Its `del`. – Neal Dec 05 '16 at 17:23
  • Sorry, actually I meant [`rd` = `rmdir`](http://ss64.com/nt/rd.html) rather than `rm`... the command [`del` = `erase`](http://ss64.com/nt/del.html) deletes files only but no directories... – aschipfl Dec 05 '16 at 18:49
  • @aschipfl but I only want to delete the files inside the directory and not the directory itself. – Neal Dec 05 '16 at 19:10
  • You were talking about using `move` instead of `xcopy /S`, so I was assuming you want to move the entire source directory tree with all its contents (both files and directories); if you do not want that, you need to be more specific in your question (please [edit](http://stackoverflow.com/posts/40939986/edit) accordingly)... – aschipfl Dec 05 '16 at 21:10

1 Answers1

2

Move should work, but you need to create the folder you're moving to first

Also, you can have multiple commands executing on each iteration of a for loop by using brackets

So the following should work

dir /b /s /a:d "\\SERVER\Path\Directory\*FolderTag" | for /f "delims=\; tokens=3,4,5*" 
%%a in ('findstr FolderTag') do (
mkdir "E:\%%b"
@move /Y "\\SERVER\Path\Directory\%%b\%%c" "E:\%%b\"
)
Richard
  • 1,121
  • 1
  • 9
  • 15