Page 1 of 3 123 LastLast
Results 1 to 15 of 41
  1. #1
    New Lounger
    Join Date
    Nov 2015
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Batch File: search list of names, copy any file containing one of those names?

    SO I've got a directory with a long list of files, each with a naming structure that relates to what's inside the file. I need to find and pull a specific file, copying it to a transfer folder, so I can work on it, while preserving the original file. This is all fine when I have a small number of files to search for, but is there a way to do this with a list of 100's of files?

    I'm thinking have the list of search names saved as a .txt or .csv file on the transfer folder, then run a batch script that searches through and searches the data file directory and when it finds a match, copies the data file, and moves on, until the end.

    Any help?


    REM @ECHO OFF

    :creates backup folder
    IF NOT EXIST C:\TRANSFER\SEARCH_RESULTS MD C:\TRANSFER\BACKUP_LOTS

    :grabs the date and time and makes a folder name
    for /f "tokens=2-8 delims=:-. " %%A in ('NOW') do set BACKUPDIR=%%A%%B%%C-%%D%%E

    :Makes the timestamped directory.
    MD C:\TRANSFER\BACKUP_LOTS\%BACKUPDIR%

    REM SETLOCAL

    :defines variable name
    SET LOCAL_LOTS=C:\TRANSFER\BACKUP_LOTS\%BACKUPDIR%

    :search name list
    _____?

    :copies from the data folder to the timestamped transfer folder
    COPY C:\DATA_FOLDER\*.DAT %LOCAL_RECIPES%

    :repeat until end of name list
    _______?

    REM PAUSE
    Last edited by surfer349; 2015-11-12 at 17:03. Reason: added code

  2. #2
    New Lounger
    Join Date
    Nov 2015
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    wierd, it looks like my OP disappeared.


    REM @ECHO OFF

    :creates backup folder
    IF NOT EXIST C:\TRANSFERBACKUP_LOTS MD C:\TRANSFER\BACKUP_LOTS

    :grabs the date and time and makes a folder name
    for /f "tokens=2-8 delims=:-. " %%A in ('NOW') do set BACKUPDIR=%%A%%B%%C-%%D%%E

    :Makes the timestamped directory.
    MD C:\TRANSFER\BACKUP_LOTS\%BACKUPDIR%

    REM SETLOCAL

    :defines variable name
    SET LOCAL_LOTS=C:\TRANSFER\BACKUP_LOTS\%BACKUPDIR%

    :search name list
    _____?

    :copies from the data folder to the timestamped transfer folder
    COPY C:\DATA_FOLDER\*.DAT %LOCAL_RECIPES%

    :repeat until end of name list
    _______?

    REM PAUSE

  3. #3
    Silver Lounger
    Join Date
    Mar 2014
    Location
    Forever West
    Posts
    2,078
    Thanks
    0
    Thanked 259 Times in 248 Posts
    Nothing showing here to read.

  4. #4
    Super Moderator satrow's Avatar
    Join Date
    Dec 2009
    Location
    Cardiff, UK
    Posts
    4,492
    Thanks
    284
    Thanked 577 Times in 480 Posts
    Auto moderated - try reading it now.

  5. #5
    jwoods
    Guest
    You could use dir /s/b *.file extension or the for command to recursively search through the main directory and all subdirectories.

    Example: for /R %%f in (*.file extension) do echo "%%f"

    You can replace *.file extension with any wildcard combination i.e. *Part of file name*

  6. #6
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,202
    Thanks
    49
    Thanked 987 Times in 917 Posts
    What's wrong with using the search in Windows Explorer and then copying the file?

    cheers, Paul

  7. #7
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    Please tell me if I've understood this correctly.

    • You have a folder with lots of .DAT files in it.
    • You have a list of some of the .DAT files in that folder that you wish to copy to a separate backup folder, whose name includes the date and time (in some format which you haven't specified clearly).

    It would be a good idea if you could give examples of the folder names and file names which you wish to copy. I DO hope that the filenames contain only alphameric characters.

    Given good will on the part of the filenames, this is a fairly simple task, which I will begin when you can give me the requested example names!

    BTW - where did %local_recipes% come from?!
    Last edited by BATcher; 2015-11-13 at 09:36.
    BATcher

    Time prevents everything happening all at once...

  8. #8
    New Lounger
    Join Date
    Nov 2015
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Paul T View Post
    What's wrong with using the search in Windows Explorer and then copying the file?
    cheers, Paul
    why do things manually when you can automate it? Doing one search at a time for 100+ files? really?

  9. #9
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    Here's my first draft, in my style of BATch file writing!
    It makes assumptions...
    No testing has yet been done...

    Code:
    @echo off
    
    :: Copy all the .dat files whose names appear in the text file  filelist.txt
    ::    (which is assumed to be in the source folder)   
    ::  from the source folder  c:\data\folder
    ::  to   the target folder  c:\transferbackup_lots\yyyymmdd-hhmm
    
    :: filelist.txt entries are expected to look rather like:
    ::   file1.dat
    ::   file number two.dat
    ::   file_3.dat
    ::   file number-4.dat
    ::     ... and so on
    
    :: obtain the current date and time regardless of locale/region/country
    ::   %yy% will contain four digits, 20nn;
    ::   %mm%, %dd% and time variables %hr%, %mn%, %sc% and %th% contain two digits
    if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
    for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
       for /f "tokens=%toks% delims=.-/ " %%e in ('date /t')     do (
          for /f "tokens=5-8 delims=:., " %%i in ('echo.^|time') do (
            (set %%a=%%e) & (set %%b=%%f) & (set %%c=%%g) & rem create yy, mm, dd
            (set hr=%%i)  & (set mn=%%j)  & (set sc=%%k)  & (set th=%%l)
            rem  note the variable names for Time: hr, mn, sc, th
    )))
    if 1%hr% LSS 20  (set hr=0%hr%)  & :: ensure 2-digit hours in %hr%
    
    :: create the full target directory path 
    ::   with the final directory name in the form  yyyymmdd-hrmn  (eg 20151114-1523)
    set target=c:\transfer\backup_lots\%yy%%mm%%dd%-%hr%%mn%
    
    :: create the full target directory path, ignoring any error message
    md %target% 2>nul
    
    :: set up the full source path
    set source=c:\data_folder
    
    :: the clever bit: read filelist.txt and, for each filename, 
    ::   copy it from the source path to the target path
    ::   it's easier to do the processing in a subroutine
    
    for /f "tokens=*" %%a in (filelist.txt) do call :process "%%a"
    
    :: that's it, folks!  (maybe a directory ist would be useful...)
    
    pause
    
    endlocal
    
    exit /b 0
    
    ::-----------------------------------------------------------------------------
    :process  the file name in double-quotes passed to this subroutine
    
    :; remove the initial and final double-quotes
    set fn=%~1
    
    :: test that the file extension is .dat
    if /i not "%~x0"==".dat" (
      echo "%fn%" does not have a .DAT extension, so is not copied
      goto :eof
    )
    
    :: perform the file copy
    if exist "%source%\%fn%" (
      copy "%source%\%fn%" "%target%"
    )
    
    goto :eof
    
    ::-----------------------------------------------------------------------------
    BATcher

    Time prevents everything happening all at once...

  10. #10
    New Lounger
    Join Date
    Nov 2015
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BATcher View Post
    Please tell me if I've understood this correctly.

    • You have a folder with lots of .DAT files in it.
    • You have a list of some of the .DAT files in that folder that you wish to copy to a separate backup folder, whose name includes the date and time (in some format which you haven't specified clearly).

    It would be a good idea if you could give examples of the folder names and file names which you wish to copy. I DO hope that the filenames contain only alphameric characters.

    Given good will on the part of the filenames, this is a fairly simple task, which I will begin when you can give me the requested example names!

    BTW - where did %local_recipes% come from?!
    yes, very close. I've got a directory folder on a network drive that saves a bunch of .DAT files using "LOT_TYPE_PRODUCT.dat"
    "C:\DATA_FOLDER\LOT_TYPE_PRODUCT.DAT" (example "Lot1_Type1_Product1.dat", "Lot2_Type1_Product1.dat", "Lotn+1_Typen+1_Productn+1.dat")

    I have a list of "LOTs" that I need to retrieve the .dat files for. I can save this list in any way necessary (excel, .txt, .csv, whatever). (ex: Lot1, Lot2, Lot6, Lot7, Lot818, etc)

    I want to run some sort of script or code that will search the given list, then grab and copy all of those .dat files containing that "LOT" name over to a transfer folder for easy access.

    *extra credit* I want to be able to run this script inside the network space, remote desktop, and have it search through half a dozen IP-addresses, network drives, each with their own "DATA_FOLDER", crawling through and grabbing a matching .dat file from the list, rather than go to each network drive one at a time.

  11. #11
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    I think your final paragraph can be put down as "future objective" for the time being!

    Running the risk of me posting just as you are replying, I would ask:
    is there only a single file which starts with "Lot27"
    or could there be multiple files which start "Lot27", for example
    Lot27_Type3_Product2.dat
    Lot27_Type3_Product7.dat
    Lot27_Type16_Product12.dat
    Lot27_Type16_Product227.dat

    The first option is good, the second would require the entire file list to be read multiple times...
    BATcher

    Time prevents everything happening all at once...

  12. #12
    New Lounger
    Join Date
    Nov 2015
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BATcher View Post
    is there only a single file which starts with "Lot27"
    or could there be multiple files which start "Lot27", for example
    Yes, multiple files for each lot. I need to grab all files for a given lot name. For example, a single lot has 4 different measurement types, so it will have 4 files related to that lot. Is there a way to search/copy a wildcard? search/copy Lot27* and grabs all the files that match that name?

  13. #13
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    I will have to do a bit of thinking about that one (I can see a few different approaches) and I don't have any more free time until later...
    BATcher

    Time prevents everything happening all at once...

  14. #14
    New Lounger
    Join Date
    Nov 2015
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    is it possible/better to copy/paste the entire original folder to the transfer folder then go through and delete a .dat file if it doesn't match the input search list?

  15. #15
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,202
    Thanks
    49
    Thanked 987 Times in 917 Posts
    Quote Originally Posted by surfer349 View Post
    why do things manually when you can automate it?
    Didn't realize you wanted to move 100s - not reading properly, again.

    cheers, Paul

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •