Saved Bookmarks
| 1. |
Solve : Is there a way to copy or move files containing a certain word between folders?? |
|
Answer» I would be grateful for any help or suggestions you might give. I would be grateful for any help or suggestions you might give.So what you really want is a script that will parse multiple directories all at once and not just your folder called OLD.assuming you have all your files just in "old" and want them all moved to just "new" this should work. You will have to fill in the pathway yourself though. Code: [Select]@echo off setlocal EnableDelayedExpantion dir | find /v "<DIR>" >>mylist.txt for /f %%G in (mylist.txt) do ( title %%G if exist str.txt del /f str.txt echo %%G >>str.txt findstr /I "match" str.txt >nul if %errorlevel% EQU 0 COPY FILLINPATHWAYHERE\old\%%G FILLINPATHWAYHERE\new\%%G /Y >nul ) If in fact you do have multiple folders in "old" then you would have to use something else.Do you want to just do one folder at a time? This should recurse from the current folder and below, and copy all files with match somewhere in the filename to the target folder (make sure the target folder exists). Code: [Select]@echo off for /f "delims=" %%a in ('dir /b /s /a-d ^|find /i "match"') copy "%%a" "c:\new folder\here" I edited this to add the find command - because the DIR command will match using both long and short filenames so it can have a false positive when it matches on a short filename. The find command should restrict the search to long filenames only. If you have hundreds of thousands of files in the tree then let us know as this will slow down dramatically with so many files.Quote from: Lemonilla on July 11, 2012, 10:03:55 AM assuming you have all your files just in "old" and want them all moved to just "new" this should work. You will have to fill in the pathway yourself though.Do you realize that the DIR command has a switch to exclude Directories? alot of your code is redundant and could be shortened as you can see from Foxidrive's post.Quote from: foxidrive on July 11, 2012, 10:04:24 AM Do you want to just do one folder at a time?Hi Foxidrive. I see you found a new place to hang out. I think this might work as well. Code: [Select]xcopy *match* c:\temp /SYes Squashman, I noted you blokes talking about it. The more batch files the merrier! Note that I edited my post above - and it will work with the move command too.Quote from: foxidrive on July 11, 2012, 10:04:24 AM Do you want to just do one folder at a time? As a test I made the directory c:\new folder\here. At the command prompt in DOS I then changed directory to the directory I wanted (containing my test file with the word match in it). I then pasted in the code you supplied and hit enter. I got the message '%%a was unexpected at this time'. Any ideas what I did wrong ?My mistake - it was missing the do keyword. Make sure you create a batch file - call it matchA.bat for example. Code: [Select]@echo off for /f "delims=" %%a in ('dir /b /s /a-d ^|find /i "match"') do copy "%%a" "c:\new folder\here"Quote from: No_hope_without_snacks on July 12, 2012, 03:00:25 AM You cannot copy and paste the code directly into the command prompt for it to work. As Foxidrive has already said, you need to create a batch file and execute the batch file. Quote H:\>for /? If you are going to start getting into batch files to automate processes you better start reading the help files for the CODE you are executing so that you understand the code. This falls under the Teach A Person to Fish instead of giving them the fish. |
|