Situation:
Numerous sub-folders with random folder names containing several files with random names. Some I want to retain, others can be deleted.
Goal:
Move the files with extensions: *.exe *.pdf *.avi or other specified extentions out of the subfolders and into a common separate folder without recreating the previous folder structure. So that I have one folder with just the files I want, leaving behind any other file types. Then log the subfolder directory names (results-log.log) and delete the subfolders and everything left in them.
Example; starting:
- parent1
- sub1
- random1.exe
- random2.pdf
- random3.txt
- sub2
- random4.exe
- random5.avi
- random6.log
- random7.jpg
- sub1
End result:
- parent2
- results-log.log
- random1.exe
- random2.pdf
- random4.exe
- random5.avi
So I've searched around for other answers and it looks like I need some sort of FOR loop, but I don't understand enough of how that works with parameters to do what I need. Moving pdf files to parent folder from subfolder with variable names is one example I looked at, and I looked over the suggested reading, but I totally don't get how the parameters and variables work. Especially where I need to cover several file types.
So far I'm looking at something like this:
@ECHO OFF
SETLOCAL EnableExtensions
set "_parent=c:\logs\cache\"
set "_dest=c:\archive\"
for /D /r %%G in ("%_parent%\*") do (
if exist "%%~G\*.pdf" (
echo move /B "%%~G\*.pdf" "%_dest%\"
) else (
echo nothing to copy "%%~G\*.pdf" "%_parent%\"
)
)
- Line 3 I believe that sets "c:\logs\cache\" as the parent folder, so in my example about that would equal "parent1"
- Line 4 should set the destination for example folder "parent2" as "c:\archive"
- Line 5 is
%%Gcorrect? Does it matter what the letter is? - Line 6 how do I specify the other file extensions I want to look for?
- Line 7 do I want to use
moveor something else, and what's the/Bparameter?/yis the only one I know of formove.
Hopefully that's enough info, can edit as needed for more info.
andand notorfor if conditions. One more reason to use powershell :) – mrfreester Oct 19 '15 at 22:18IFstatement, but I still keep the first 4 lines as they are, correct? Then what do I do about theelseportion? Add it after all of myIFstatements with anechoto the log file?p.s. I'm open to doing this with PowerShell instead, but I know even less about using PS, it's on my list of things I need to learn soon.
– WraythOsu Oct 20 '15 at 15:07rd /s /q %%Gbetween themoveline and thesetline in each statement? My only concern here is that I don't want to delete a folder that might contain a file type I haven't factored in the script. So say a folder shows up as a .msi instead of .exe, I want that folder to be left alone for now. So I only want it to delete a folder if a file has been moved out. – WraythOsu Oct 20 '15 at 19:50rd /q %%Gline the last if block where you write to the log file since you should get into that only if you deleted something in the previous if blocks. I would probably leave out the/soption as I think that will delete the directory even if there are still files in it. If you leave out/sthen I'm pretty sure it'll do what you want, which is delete the folder only if it's empty. – mrfreester Oct 20 '15 at 22:43/s. But I don't want to delete everything from the _parent on down. In case a new file type comes in that doesn't get moved, I want that folder to stay in place so I can evaluate it. So I just want to delete the folders where I've moved a file from. – WraythOsu Oct 21 '15 at 13:22rd /s /q %%Gand it did what I wanted. Only problem was it didn't delete a directory with a space in it. So can I just put" "around the%%Glike this:rd /s /q "%%G"? I know that quotes can do weird things if not done right. So all I need now is the correct formatting for that line and this is answered. woohoo! – WraythOsu Oct 21 '15 at 15:33f1/f2both with files in them, and the loop goes throughf1first, if you delete that folder you might accidentally remove everything inf2. You might want to throw in some echo statements to see the order. If the loop always goes through the top folder first consider yourself lucky, and all you'll need to do is userdin that last if statement. If not so lucky, you'll have to work through some more logic. – mrfreester Oct 21 '15 at 15:35RDcommand to handle folder names with spaces and we are all set. – WraythOsu Oct 21 '15 at 15:39