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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

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

#16 Post by foxidrive » 26 May 2012 04:52

insatiable11 wrote: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 :/

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"


You can download GnuSED and use this batchfile on your txt files.

Code: Select all

@echo off
sed "s/^[0-9]*[-. ]*//" "%~1" > "%~1.txt"



If you give it a text file called "abc.txt" (you can drop it on the batch file) and it gives you an output file called "abc.txt.txt" with the leading numbers and text removed from your file.

I gave it this text file and it gave me the result that is below it:


Code: Select all

1. example1.doc
2. example2.doc
3. example3.doc

1 - example1.doc
2 - example2.doc
3 - example3.doc

1 example1.doc
2 example2.doc
3 example3.doc

1- example1.doc
2- example2.doc
3- example3.doc

1.example1.doc
2.example2.doc
3.example3.doc



example1.doc
example2.doc
example3.doc

example1.doc
example2.doc
example3.doc

example1.doc
example2.doc
example3.doc

example1.doc
example2.doc
example3.doc

example1.doc
example2.doc
example3.doc

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

#17 Post by Aacini » 29 May 2012 22:21

Below is the new version; these are the changes:

1- The problem with names that begin with numbers is solved. Remember that this version is just the second one on the number problem, so it is no time to give up yet! The trick is to extract the string placed between the number and the name; the new version use space, dash and dot characters to assemble this separator. If you find a new case with another character, just insert it between quotes in the FOR command at line #18. The only case that can not be managed this way is when there is NOT a separator between the number and the name, like in "1example.doc". However, if you have many cases like this one, an additional small program can solve it.

2- The problem with names that include exclamation marks is solved.

3- I devised an entirely new method to create the list of file names that should solve all your problems. The new method uses a two-phases approach: in the first phase each word of the filename is extracted and the rest is used as wild-card for filenames search; this should find the right file if the original name have errors in one word, it has one additional word (like "The" at beginning) or several missed words. If this phase don't found the desired file, the second phase use each word of the given filename as wild-card for the search. This guarantee that the right file will be found if at least one word of its name is correctly written!

In the new version all the names of not found files are reported in the log file with the name of the copied file (or "FILE NOT COPIED" message). If is there just one file found in the list, it is not automatically copied like before (this feature can be reinserted later, if you wish).

I think this new version will solve all your problems so the two-stage execution you requested is no longer needed. Test the program and report the results!

Code: Select all

@echo off
if "%~1" equ "While" goto %2
setlocal EnableDelayedExpansion
set While=for /L %%a in () do if
set Do=(
set WEnd=) else exit
set RunWhile=cmd /V:ON /Q /C "%~F0" While
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%" neq "1" goto start
   set n=0
   :nextChar
   set /A n+=1
   for %%a in (" " "-" ".") do if "!firstLine:~%n%,1!" equ %%a goto nextChar
   set /A n-=1
   set cutNumber=!firstLine:~1,%n%!
:start
setlocal DisableDelayedExpansion
for /F "usebackq delims=" %%a in ("%~NX1") do (
   set "fileName=%%~Na"
   setlocal EnableDelayedExpansion
   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"
      echo/>> "%newFolder%.log"
      echo File not found: "!fileName!.doc">> "%newFolder%.log"
      %RunWhile% ChooseName Phase1
      if errorlevel 1 (
         set /P name=< nameChoosen
         ECHO copy "!name!" "%newFolder%"
         echo File "!name!" copied
         echo File "!name!" copied>> "%newFolder%.log"
      ) else (
         %RunWhile% ChooseName Phase2
         if errorlevel 1 (
            set /P name=< nameChoosen
            ECHO copy "!name!" "%newFolder%"
            echo File "!name!" copied
            echo File "!name!" copied>> "%newFolder%.log"
         ) else (
            echo FILE NOT COPIED
            echo FILE NOT COPIED>> "%newFolder%.log"
         )
      )
   )
   endlocal
)
if exist "%newFolder%.log" (
   echo/
   echo/
   echo CONTENTS OF LOG FILE:
   type "%newFolder%.log"
   del nameChoosen
)
PAUSE
goto :EOF


:ChooseName
set nWord=0
for %%a in (!fileName!) do (
   set /A nWord+=1
   set word[!nWord!]=%%a
)
set "originalName= !fileName! "
set iWord=0
set option=
%While% not defined option %Do%
   if !iWord! lss !nWord! (
      set /A iWord+=1
      for %%A in (!iWord!) do (
         if %3 equ Phase1 (
            for %%B in (!word[%%A]!) do set "wildCard=!originalName: %%B =*!"
         ) else (
            set "wildCard=  !word[%%A]!  "
         )
      )
      set i=0
      for %%f in ("*!wildCard:~1,-1!*.doc") do (
         set /A i+=1
         echo     !i!- %%f
         set "name[!i!]=%%f"
      )
      if !i! gtr 0 (
         set /P "option=Enter number (0 cancel this phase), or press Enter to continue searching: "
         if defined option (
            for %%A in (!option!) do echo !name[%%A]!> nameChoosen
         ) else (
            echo/
            echo CONTINUE SEARCHING: "!fileName!.doc"
         )
      )
   ) else (
      set option=0
   )
%WEnd% !option!
exit /B


Antonio

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

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

#18 Post by insatiable11 » 24 Sep 2012 19:53

Hi Antonio,

Firstly I just wanted to thank you again for the batch file you created for me. It sometimes struggles to find the correct file but because of the log file it creates, I can easily go back and grab them manually.

For me the main plus is the fact that I no longer have to be concentrating and staring at the computer screen for hours on end searching through files. I just let the batch file do all the work and press a number whenever it asks me a question. It probably isn't an awful lot faster than before but it's definitely much much easier. So thanks again!

I've been given the task recently of inputting data onto a website and while doing it I remembered about the batch file you created for me and wondered if a batch file could do the trick again...

Basically all I'm doing at the minute is going down a txt file and manually copying one line at a time and pasting it onto the site.

So all I would need is a batch file to automatically do the copying for me, so as I pasted each line onto the site - the batch file would then copy the next line in the txt file automatically. Meaning I could then just continue to paste onto the site, without constantly going back to the txt file to copy the next line.

Is this something a batch file could do? Maybe there's a piece of software out there already that does this?

I really hope you can help me out again! :D

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

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

#19 Post by foxidrive » 24 Sep 2012 23:38

A keyboard and mouse macro recording program will work for that.

You record yourself doing it once (macro) and then a hotkey will repeat what you have just done (play back the macro). All you do is keep hitting the hotkey and watching.

jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

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

#20 Post by jjlyon » 11 Oct 2012 21:03

Hi, i have a very smiler problem. I'm trying to do a very similar thing however my names.txt file contains a simple list such as:

Tony.ptpx
Mark.ptpx
Tony.ptpx
Tony.ptpx
Josh.ptpx
Josh.ptpx

with lots of Repetitions.

i need the documents to be Searched, copy+pasted over to the names folder but then to be re-named if there is already a file with the same name with a number at the end. such as Tony.ptpx, mark.ptpx, Tony1.ptpx, Tony2.ptpx, .. ect also a great help would be for the files to be copied over with an additional number at the front of the file names so i can keep the order they ware in on the list. for example, overall in the names folder i would like to see:

1Tony.ptpx
2Mark.ptpx
3Tony1.ptpx
4Tony2.ptpx
5Josh.ptpx
6Josh1.ptpx


my lists are never normally more then 60 lines in length. I'm also a complete newbie when it comes to batch files, the code by Aacini was the closest yet to what i have been looking for.

any help would be amazing & a real life saver! the lists are of disk files i use for my disk burning business, and i always need to burn them in order with copy's if listed, a batch file to do this would remove a lot of the daily repetitiveness. please help :)

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

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

#21 Post by foxidrive » 12 Oct 2012 00:04

This seems to work here - note that with a leading number on each filename then the trailing number on duplicates aren't really needed.

1) Change the target folder in set "target=d:\data folder" to where you want the files copied.
2) the batch file will search from the folder it is in, and below, for the files.
3) it gives a two digit number at the start of each filename, so any lists over 99 lines will be upset.



Code: Select all

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "target=d:\data folder"
set num=0
for /f "delims=" %%a in (names.txt) do (
set /a num=num+1
set c=00!num!
set c=!c:~-2!
for /f "delims=" %%b in ('dir "%%a" /a:-d /o:n /b /s') do call :next "%%~fa"
)
pause
GOTO:EOF

:next
echo processing %1
pushd "%target%"
set fnum=
:loop
if exist "??%~n1!fnum!%~x1" (
set /a fnum=fnum+1
goto :loop
)
copy /b /y "%~1" "!c!%~n1!fnum!%~x1" >nul
popd



With this names.txt file

repalce.bat
replace.txt
replace.txt
a.bat
repalce.bat
replace.txt
a.bat



it produced this list of files.

01repalce.bat
02replace.txt
03replace1.txt
04A.BAT
05repalce1.bat
06replace2.txt
07A1.BAT

jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

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

#22 Post by jjlyon » 12 Oct 2012 18:07

this is fantastic! thank you so much foxidrive :D .

insatiable11, i use a program called MouseRobot, its not free but its the best automated keyboard and mouse macro recording program iv used. with this tool and the batch file foxidrive made me i can save hours a day of work! the only job i need to do now is deal with customer messages,package and post my disks!. everything else is completely automated!

i cant thank you enough foxidrive for your help! this batch file was the last link i needed for complete automation and a dream i have been trying to reach for nearly a year.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

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

#23 Post by Ed Dyreen » 12 Oct 2012 18:54

'
Very well, you are aware there are alternatives to mouseRobot that are FREE and BASIC.
They allow to control windows in a multi-tasking secure manner, unlike just recording mouse- and keyboard-Events.

jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

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

#24 Post by jjlyon » 12 Oct 2012 20:44

i know there are alternatives, probably better ones, i have only looked at a few of them but I'm not very good when it comes to coding :( , i rely needed something that was simple and fast to learn. mouserobot isn't the most stable in my opinion but it does get the job done for the day to day tasks of my small business.

I.E copying my sales history to my desktop from ebay, opening it in excel and extracting an printing addresses then extracting the list of disk items i have to post to a txt file, then running the bat file foxidrive made for me to pull all the disk images in the list to 1 folder and then copying all the images into my ptpublisher program which gets the robot to print & burn all the disks off. iv been trying to work it out for a long time :roll:

autoitscript does look like it can do more however it seems a bit to complex for something i need up and running quickly. preferably i would of liked to of created a program to do it all for me in VB, but my skills are sadly low in the programming corner :oops: . however it is something i want to look into in the future! :)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

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

#25 Post by Ed Dyreen » 12 Oct 2012 20:58

jjlyon wrote:but I'm not very good when it comes to coding :( , i rely needed something that was simple and fast to learn.
Which is why I said BASIC :wink:

BASIC is a family of general-purpose, high-level programming languages whose design philosophy emphasizes ease of use; the name is an acronym from Beginner's All-purpose Symbolic Instruction Code.

jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

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

#26 Post by jjlyon » 12 Oct 2012 22:19

Thanks for your advice Ed Dyreen, I'm still in the 30 day trial of mouserobot an to buy its quite expensive. in the mean time ill have a good look into AutoIt and see what i can come up with. :)

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

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

#27 Post by foxidrive » 12 Oct 2012 23:01

There are some free mouse/keyboard recording programs and they are superb for many tasks.
Some are easier to learn than others but are more powerful (when editing the macros or running macros from the command line etc)

Clavier+ V10.6.1  freeware macro program for windows

MacroMaker 2.0.0.4 allows the user to build macros and assign them to hotkeys.
MacroMaker allows the user to record both keyboard and mouse activity.
The user can simulate a series of keystrokes, execute built in functions
or play back a previous recording with just a keystroke. With the copy
and paste functions available you can feasibly have a clipboard, which
can store text only, assigned to every key on the keyboard amongst other
prebuilt functions. The best part is that it is a Macro generation
program that is truly free. Anthony Mathews


Aldo's Macro Recorder 1.3 - July 26, 2001
This version is freeware

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

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

#28 Post by foxidrive » 12 Oct 2012 23:03

jjlyon wrote:i cant thank you enough foxidrive for your help! this batch file was the last link i needed for complete automation and a dream i have been trying to reach for nearly a year.


Thanks, I'm glad it helps you. :)

Bobbo Jones
Posts: 48
Joined: 18 Oct 2012 21:27

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

#29 Post by Bobbo Jones » 18 Oct 2012 22:45

Hi all, I have a question regarding the original problem the aacini and the thread starter were working on.

I have the exact same need, I need to search through folders of thousands of files, and collect 20 odd files at a time, and copy paste them into a new folder.
The files will always be correctly labeled, so I do not actually need a the part of this that suggests possible matches, although if it is already included it is ok to leave it in.

I have only learned of batch scripting this morning, and have been watching beginner youtube vids all day, and a, trying to learn as fast as I can. What I am searching for, is how to personalise the formula in Aacini's first reply, to be suitable for my purposes.

For arguments sake, the text file containing the list of files I need to search for would be called
breaks filenames world.txt
The folder I want to search through would be called
Z:\2011-MASTERS
The folder I would want them to go in, can be a new folder created by the script, that appears on the desktop

Can anyone offer any advice?
thanks all! thanks for this forum.

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

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

#30 Post by foxidrive » 19 Oct 2012 00:46

Bobbo Jones wrote:I have the exact same need, I need to search through folders of thousands of files, and collect 20 odd files at a time, and copy paste them into a new folder.

For arguments sake, the text file containing the list of files I need to search for would be called
breaks filenames world.txt
The folder I want to search through would be called
Z:\2011-MASTERS
The folder I would want them to go in, can be a new folder created by the script, that appears on the desktop


You need to repeat your requirements, so people don't have to re-read the thread. Usually you'd create a new thread for a task that is not the same.

So you have a file. You have a folder which contains subfolders and thousands of files.

What is the task exactly? Does the text file contain a list of filenames? Are they all unique? Do you want to move those filenames into another folder? Or copy them? Or??

Post Reply