search files,copy+paste to new folder (batch file software?)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
insatiable11
Posts: 8
Joined: 03 May 2012 10:38

search files,copy+paste to new folder (batch file software?)

#1 Post by insatiable11 » 03 May 2012 10:46

I am a complete newbie when it comes to batch files and batch file software, so apologies in advance.

I've done some research on Google etc but I seem to just be getting more and more confused trying to use various different scripts and programs and reading different info on various websites.

This is what I need...

I have a folder with around 2000 files in it. I want to be able to copy certain files from this folder (normally around 40) and place them in a brand new folder, I want to be able to do this with nothing more than a txt file that has all the file names I need copying in it with every file name on a separate line.

I receive around 10 new txt files each day so you can imagine how tedious it is searching through 2000 files, picking out the 40 I need, 10 times a day everyday.

To make things even more complicated, the txt files I receive don't always have the correct file names in them, maybe there's a misspelling or a word or two missing for some of the names. That's ok when I'm going through picking out the files I need to copy myself because I can normally tell which file is being requested, but I can't see it being so easy for a program or whatever to do, I'm guessing it'll need to have a search feature where it picks out the most likely file.

I'd like it to be as automated as possible. I receive the txt file, make sure each file name is on a separate line then a program or script or whatever goes out and picks out the files (or most likely files for the name's that aren't exact matches) and copies them into a brand new folder (which I'd like to be the same name as the text file it's using to choose the files).

I hope this makes sense, I've probably made it sound a lot more complicated than I needed to, I've been looking for a solution for weeks now so my brains gone a bit foggy :/

I'd appreciate any help you can offer!

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: search files,copy+paste to new folder (batch file softwa

#2 Post by Aacini » 03 May 2012 11:03

The Batch file below is a preliminary version. Please, test it and report any desired change or error.

EDIT: this is the final version after 3 last modifications. Remember to eliminate the ECHO parts!

Code: Select all

@echo off
setlocal EnableDelayedExpansion
cd /D "%~DP1"
set newFolder=%~N1
if not exist "%newFolder%" ECHO md "%newFolder%"
if exist "%newFolder%.log" del "%newFolder%.log"
set cutNumber=
set /P firstLine=< "%~NX1"
if "%firstLine:~0,1%" equ "1" (
   if "%firstLine:~1,1%" equ "." (
      set "cutNumber=. "
   ) else (
      set "cutNumber=- "
   )
)
for /F "usebackq delims=" %%a in ("%~NX1") do (
   set "fileName=%%~Na"
   if defined cutNumber (
      set "fileName=!fileName:*%cutNumber%=!"
   )
   if exist "!fileName!.doc" (
      ECHO copy "!fileName!.doc" "%newFolder%"
      echo File "!fileName!.doc" copied
   ) else (
      echo/
      echo File not found: "!fileName!.doc"
      call :getFileList "!fileName!"
      if !n! gtr 0 (
         if !n! equ 1 (
            set i=1
         ) else (
            set i=0
            set /P "i=Enter the desired file number: "
         )
         if !i! gtr 0 (
            call :setElem name=name[!i!]
            ECHO copy "!name!" "%newFolder%"
            echo File "!name!" copied
         ) else (
            echo FILE NOT COPIED
            echo File not found: "!fileName!.doc">> "%newFolder%.log"
         )
      ) else (
         echo I can't find any similar file...
      )
   )
)
if exist "%newFolder%.log" (
   echo/
   echo/
   echo THESE FILES WERE NOT COPIED:
   type "%newFolder%.log"
)
PAUSE
goto :EOF

:getFileList
set n=0
set "name=%~N1"
:cutName
   set "name=%name:~0,-1%"
   if not defined name exit /B
   if not exist "%name%*.doc" goto cutName
for %%a in ("%name%*.doc") do (
   set /A n+=1
   set "name[!n!]=%%a"
   echo     !n!- %%a
)
exit /B

:setElem
set %1=!%2!
exit /B
Previous file require you place the name of the .txt file in the first parameter. For example, if previous Batch file is named PROCESS.BAT and the text file is NAMES.TXT:

Code: Select all

PROCESS NAMES.TXT
The upcase ECHO commands just show the commands that would be executed; when you are ready, remove ECHO part. An error checking at several places may also be added...

Welcome to this forum! :mrgreen:
Last edited by Aacini on 17 May 2012 10:50, edited 4 times in total.

insatiable11
Posts: 8
Joined: 03 May 2012 10:38

Re: search files,copy+paste to new folder (batch file softwa

#3 Post by insatiable11 » 03 May 2012 12:49

Hi Aacini, thank you very much for your help and warm welcome.

Here is an example of what the list of 2000 file names look like (I created it with a word generator):

Transport Factory Dropping.doc
Roundabout Desert Judge.doc
Reveal Internal Amaze.doc
Case Balance Brand Flip.doc
Luggage Stay Supporter.doc
Reign Classification Recall.doc
Needle Bounce Want Road.doc
Replacement Slogan Pardon.doc
Gesture Factor Crying Bending.doc
Gender Criterion Perfection Plea.doc

Here is an example txt file I would receive, also indicating whether file names are correct/incorrect:

Code: Select all

lugage stay support - incorrect, misspelling
transport factory dropping - correct (except no .doc)
replacement slogan pardon - correct (except no .doc)
needle want road - incorrect, missing word


I must point out that the txt files I receive rarely have the file extension on the end (so in the above example .doc).

Another thing that often happens is I'll receive a txt file like the one below where the file names are all correct but they're ordered numerically which may also cause problems:

Code: Select all

1. Transport Factory Dropping
2. Roundabout Desert Judge
3. Reveal Internal Amaze
4. Case Balance Brand Flip
5. Luggage Stay Supporter
6. Reign Classification Recall
7. Needle Bounce Want Road
8. Replacement Slogan Pardon
9. Gesture Factor Crying Bending
10. Gender Criterion Perfection Plea


I've saved your preliminary version as PROCESS.BAT but I don't know how to go any further? What parts do I need to edit? (sorry again for being a complete noob)

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: search files,copy+paste to new folder (batch file softwa

#4 Post by Aacini » 03 May 2012 13:17

I modified my program above to manage the .doc extension. Copy that program again in a Batch file (i.e. PROCESS.BAT) and place it in the same folder that contain both the 2000 .doc files and a .txt file and test it:

Code: Select all

PROCESS filename.txt
After that, report what happened...

NOTE: Is important that there are NOT spaces at the end of the lines with file names!

insatiable11
Posts: 8
Joined: 03 May 2012 10:38

Re: search files,copy+paste to new folder (batch file softwa

#5 Post by insatiable11 » 03 May 2012 13:48

Ok I've saved the new PROCESS.BAT file and placed it in the folder with all the .doc files and put a .txt file in there as well but how do I test it?

I've tried right clicking on PROCESS.BAT and clicking "Run as administrator" but all that happens is command prompt pops up for half a second and then disappears again??

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: search files,copy+paste to new folder (batch file softwa

#6 Post by Fawers » 03 May 2012 14:24

insatiable11 wrote:I've tried right clicking on PROCESS.BAT and clicking "Run as administrator" but all that happens is command prompt pops up for half a second and then disappears again??


Let's say these files are located in "C:\DOC Files".

Open your command prompt, drag and drop PROCESS.BAT and type in the name of the .txt file.
For instance, if the .txt file name is docfiles.txt, your command line should look like this:

Code: Select all

"C:\DOC Files\PROCESS.BAT" "C:\DOC Files\docfiles.txt"

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: search files,copy+paste to new folder (batch file softwa

#7 Post by Aacini » 03 May 2012 15:55

@insatiable11: In this case, insert PAUSE command just before goto :EOF!

insatiable11
Posts: 8
Joined: 03 May 2012 10:38

Re: search files,copy+paste to new folder (batch file softwa

#8 Post by insatiable11 » 03 May 2012 16:13

I've tested it in the actual folder I need it for and I've also created the folder "C:\DOC Files" with the example doc files in there and the txt file "docfiles.txt" to see if it would work then but both times all I got was...

md "docfiles"

File not found: "docfiles.doc"
I can't find any similar file...


I don't understand why it's trying to find docfiles.doc?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search files,copy+paste to new folder (batch file softwa

#9 Post by foxidrive » 03 May 2012 17:38

You can continue with Aacini's help - I just wanted to point out that with misspelt names is it difficult to implement fuzzy logic and trying to guess names, when two similar names could match and you get the wrong file.

How about if the batch file gives you a list of names that do not match, and you can then check and correct the list, and let the batch file then do the copy?

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: search files,copy+paste to new folder (batch file softwa

#10 Post by Aacini » 04 May 2012 08:39

Oops! I assumed you would use a DOS Window (command line) to execute the Batch file! :? My mistake... :cry:

I modified the Batch file above. To test it:

1- Copy again the Batch file into the folder that have the *.doc files and the names.txt file
2- DRAG the names.txt file and DROP IT on the batch.bat file icon.

This is the result of a test I made with your example data:

Code: Select all

md "names"

File not found: "lugage stay support.doc"
    1- Luggage Stay Supporter.doc
Enter the desired file number: 1
copy "Luggage Stay Supporter.doc" "names"
File "Luggage Stay Supporter.doc" copied
copy "transport factory dropping.doc" "names"
File "transport factory dropping.doc" copied
copy "replacement slogan pardon.doc" "names"
File "replacement slogan pardon.doc" copied

File not found: "needle want road.doc"
    1- Needle Bounce Want Road.doc
Enter the desired file number: 1
copy "Needle Bounce Want Road.doc" "names"
File "Needle Bounce Want Road.doc" copied
Press any key to continue . . .


Please, report what happened! :wink:

insatiable11
Posts: 8
Joined: 03 May 2012 10:38

Re: search files,copy+paste to new folder (batch file softwa

#11 Post by insatiable11 » 10 May 2012 22:04

Sorry for my late reply, I've not had the time to sit down and test out your script until now.

It worked near perfectly in terms of finding the correct files, I loved the idea of giving search result options to choose from; genius!

I tested it with the folder of 2000+ files and a previous text file I'd received and there was only one file it failed to find. It searched for "Cranival Funfair Map.doc" and it came up with 8 search results all of which started "Cra". However the file that it should've found was "Carnival Map.doc".

The rest of the files it found and copied (31 files in this particular txt file) but then it didn't paste the files to anywhere? Maybe you haven't set that part up yet and were waiting for me to respond with any flaws or things I'd like changing?

Sorry for only replying now, I really should've found the time to reply earlier.

One thing that maybe could be added:
Though it did find all the files that it could possibly find, there was one file that could not be found because it was not available (sometimes happens) so the search result gave 10 possible files all of which were obviously wrong because the file requested was not in the folder. I then typed "0" and I think it copied nothing which is what I was hoping it would do. What I'd also like is when I type "0" indicating that the file has not been found or is not available, that somehow a new txt file or whatever is created showing these files so I can then see what files I was unable to get.

Thank you for all your help, it looks like this script is going to be the answer to all my problems! Like i said it copied 29 out of 31 files perfectly (one of which didn't exist anyway) all I need now is to be able to paste them into a new folder (I'd also like the name of this folder to be the same name as the txt file it's grabbing the file names from - if possible).

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: search files,copy+paste to new folder (batch file softwa

#12 Post by Aacini » 11 May 2012 13:26

As I said before, the Batch file is a preliminary version; however, it supposedly create a new folder with same name of the .txt file and copy the files to it. Note that there are several ECHO commands written in upcase letters; these ECHO commands show the commands that would execute the real things (create the folder and copy the files); when you are satisfied with program's preliminary results, remove the ECHO part. For example, ECHO md "%newFolder%" (in the IF line) should be md "%newFolder%" to really create the new folder (the same thing in two COPY commands).

I will do the modification that if you type 0, the name of the not found file be stored in a log file. However, before I do that I would like to agree in other points so I will not post many versions with small changes each.

- Do you like that if the list of possible names for a not found file be exactly 1 name the program copy it with no further confirmation?

- About file names with numbers: how to identify that names have numbers? If the first line start precisely with "1. "? Any other possible option?

- Any other request? This is the right moment to ask for they! 8)

Antonio

insatiable11
Posts: 8
Joined: 03 May 2012 10:38

Re: search files,copy+paste to new folder (batch file softwa

#13 Post by insatiable11 » 16 May 2012 21:03

Hi Antonio,

Thank you for adding the "0" log file.

Yes I think if only 1 result is given it would be better if the program copy it without me needing to confirm it.

On the few occassions I receive file names beginning with numbers they are nearly always "1." "2." "3." etc. Maybe sometimes "1 -" "2 -" "3 -" etc? I think 99 out of 100 times it will be one of these two ways.

My only other request is that maybe you could get the program to find the one file it failed to find in my test? It searched for "Cranival Funfair Map.doc" and it came up with 8 search results all of which started "Cra". However the file that it should've found was "Carnival Map.doc".

Thank you again. Once you've made these changes I'll test it with a few hundred text files I have previously received and will report back with the results. Hopefully there shouldn't be many (if any) problems because the script seems to work so well already! :D

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: search files,copy+paste to new folder (batch file softwa

#14 Post by Aacini » 17 May 2012 10:56

Ok. I modified the Batch file above so it include now these changes:

1- If you type 0 or just Enter, the name of the not found file is copied to a log file that have the same name of the input text file but .LOG extension. When program ends, it show this file if it exists.

2- If the result list have 1 name, it is copied with no confirmation.

3- About names that begin with numbers:

Code: Select all

   IF the first char of the first line is "1" THEN
      Numbers will be eliminated with these rules.
      IF second char is "." THEN
         SET cutNumber=". "
      ELSE
         SET cutNumber="- "
      ENDIF
   ENDIF
   Eliminate first characters in each name until cutNumber.

Note that this method require an space after the "." or "-".

4- About your last request, I thought at first to generate a new file list with names that match the beginning of the not found file instead the end. However, this list could be very large, so I thought to generate several lists and show the shorter/last one, but in your example this list will not include the name you wish neither. This problem is complex to solve; the modification is not simple because it should include a method to pause the list each time the screen is full. How many names exists that start with "C"? :? I suggest you to solve this case manually: after the program ends and the names of the not found files be reported, you may modify the names file and run the program again or just manually copy the missing files.

Test the program and report the result! :D

insatiable11
Posts: 8
Joined: 03 May 2012 10:38

Re: search files,copy+paste to new folder (batch file softwa

#15 Post by insatiable11 » 26 May 2012 03:55

I've tested the batch file over the last few days with many txt files.

The main recurring problem I'm having is with the txt files where the names begin with numbers :/

I think I've got a solution though!

Can you create several very slightly different batch files? For example...

A batch file for txt files with names that DON'T start with numbers

A batch file for txt files with names that start "1. example.doc"

A batch file for txt files with names that start "1 - example.doc"

A batch file for txt files with names that start "1 example.doc" (notice that this example is the number then just one space then the name, I didn't realise how many of these I actually got!)

A batch file for txt files with names that start "1- example.doc"

A batch file for txt files with names that start "1.example.doc"

etc etc.

This way I can simply choose which batch file to use depending on the txt file I receive.

Also, it means that I should be able to create my own slightly different batch files if I need to. For example if I receive a txt file with names that start "1-example.doc" couldn't I just copy the batch file you created for txt files with names that start "1.example.doc" and change the "." to "-" ?

If this is true you would only need to make different batch files for one character e.g. "-" and then I would just copy and edit it to "." or ":" or whatever, as I go.

So maybe have these slight deviations: "no numbers", "1example.doc", "1 example.doc", "1-example.doc", "1- example.doc", "1 -example.doc", "1 - example.doc"

I know this means creating 7 batch files but they really would be a lifesaver!


One glitch that I have found is that the batch file doesn't seem to recognise exclamation marks. For example the file name is "Do It Yourself!.doc" and the txt file told the batch file to search for "Do It Yourself.doc". This is what happened: -

Code: Select all

File not found: "Do It Yourself.doc"
      1- Do It Yourself.doc
The system cannot find the file specified.
File "Do It Yourself.doc" copied

As you can see it has given one search result but you'll notice that there is no "!" in the file name. Then it can't find the file (because it's missing the "!") but still says it's copied it even though it hasn't. It also doesn't note anything in the log file (which is why it took me a while to realise). I could just remove all the exclamation marks from the file names? Do you know if there are any other characters the batch file doesn't recognise?


The only other issue I've had is with it not finding the correct file if the first word has a mistake or slight difference, for example it searched for "Bathroom cream or white" and should've found "Bathrooms cream or white" but instead it gave 7 results that all started "Bathroom". In another example it searched for "Bathroom Do It Yourself" and should've found "Do It Yourself Bathroom" but instead it gave the same 7 results that all started "Bathroom". A few times I've had it where because the file name begins "The" and in the txt files "The" is often missed out, the file is never found. Is there no way you can tell the batch file to just show the files that have the most identical sequence of characters with some sort of points system? E.g...

Comparing "Bathroom Do It Yourself" to "Do It Yourself Bathroom" = B+a+t+h+r+o+o+m D+o+ i+t+ Y+o+u+r+s+e+l+f
18 points (18 +'s) out of a possible 19 (there are 19 spaces between the characters of "Bathroom Do It Yourself"). Each + denotes that the two filenames share the same sequence of characters (B+a), (a+t), (t+h) etc.

I know I'm probably completely over-simplifying this and if I am, I'm sorry - I understand that it's very difficult to "implement fuzzy logic and trying to guess names" as foxidrive put it. Plus I knew absolutely nothing about batch files just a few weeks ago (and still don't really), so if I sound ignorant please accept my apologies.

I'll just have to use the log files to manually copy the files it can't find - it's still a hundred times faster than what I was doing before! Thank you again, you have saved me A LOT of time! :D


I've just thought; is there any chance you can get the batch file to first go through all of the names without it copying but it "remembers" the files it finds and the files you choose as well as creates a log file, then once it's gone through all the files hit "go" or Enter and then it copies eveything all at once? This way if you need to go back and edit the txt file before it starts copying; you can. I suppose I could use the batch file with ECHO, but that would mean having to always go through the whole process of picking and choosing the files I want twice everytime even when the txt file doesn't need changing.

Post Reply