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 have some files with the word 'match' in the file name; let's say they are in a FOLDER called 'old'. I want to COPY (later move) all the files with 'match' in the filename to a folder called 'new'.

Do you know how to do this in DOS?


I have a lot of folders to go in and out of so I am looking for an easier way.Quote from: No_hope_without_snacks on July 11, 2012, 09:32:19 AM

I would be grateful for any help or suggestions you might give.

I have some files with the word 'match' in the file name; let's say they are in a folder called 'old'. I want to copy (later move) all the files with 'match' in the filename to a folder called 'new'.

Do you know how to do this in DOS?


I have a lot of folders to go in and out of so I am looking for an easier way.
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.

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 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?

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 "*match*" /b /s') copy "%%a" "c:\new folder\here"
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?

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.



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


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 ?
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 /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable.

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.


Discussion

No Comment Found