batch file that searches for files from list - multiple copies ???

Page 1 of 2 12 LastLast

  1. Posts : 8
    win10
       #1

    batch file that searches for files from list - multiple copies ???


    HI.

    I use the following batch files to search through folders for images listed in a text filelist and copy them to destination folder.

    set FIILELIST=FileList.txt
    set SEARCHFOLDER=%cd%\Overlaid-Images
    set FILEDESTINATION=%cd%\FilesCategorised\Category01

    IF NOT EXIST %FILEDESTINATION% (mkdir %FILEDESTINATION%)
    for /f "delims=" %%x in (%FIILELIST%) do (forfiles /p %SEARCHFOLDER% /s /m %%x /c "cmd /c copy /y @Path %FILEDESTINATION%\@file" 2>>temporary_log.txt)
    for /f "tokens=5 " %i in (temporary_log.txt) do (echo.%~i)>>temporary_log.txt
    del temporary_log.txt
    pause

    REM %%x.* for files without extensions and %%x for files with.


    Sample FileList.txt:

    ai21091458-ai21ndnl-GS
    ai21091458-ai21ndnl-GS
    ai21091458-ai21ndnl-GS
    ai21092313-ai21tjxq-GS
    ai21092338-ai21skue-GS
    ai21092703-ai21rfcx-GS
    ai21092234-ai21cvjq-GS
    ai21092418-ai21ifxs-GS
    ai21092438-ai21ifxs-GS


    It works superbly for my needs with one exception. It doesn't create multiple copies if there is more than one identical entry.

    So, I was wondering if the following is possible?:

    If the script encounters the same filename entry in the FileList.txt more than once, can a script make as many copies of that file as are in the list rather than just the one, as currently?

    |For example, in the sample FileList.txt list above, rather that create one copy of 'ai21091458-ai21ndnl-GS', can a script create three copies of it for me?

    I don't think this is possible, but better to ask.

    Thanks to anybody who might be able to help me.
      My Computer


  2. Posts : 16,713
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #2

    I think Stack overflow would be a more helpful forum for your current question
    Batch-file Questions - Stack Overflow

    All the best,
    Denis



    Welcome to TenForums.

    It's really worth making time to browse through the Tutorial index - there's a shortcut to it at the top of every page.
    - At the foot of the Tutorial index is a shortcut to download it as a spreadsheet.
    - I download a new copy each month.
    - By downloading it as a spreadsheet I can benefit from Excel's excellent filtering capabilities when I search for topics of interest.
    - Tutorials are also listed by category at Tutorials - there's also a shortcut to that at the top of every page.
    - Both tutorial lists are searchable.
    - You can also search for TenForumsTutorials in many general search engines, such as Google, by adding site:tenforums.com/tutorials after your search term. For example,
    taskbar toolbars site:tenforums.com/tutorials

    You can search TenForums using the search box in the top-right corner of all TenForums webpages or using Advanced Search - TenForums
    - You can also search TenForums threads in many general search engines, such as Google, by adding site:tenforums.com after your search term. For example,
    Search for drivers by HardwareID site:tenforums.com
    - [This is what the search box in the top-right corner of TenForums webpages does automatically]
      My Computer


  3. Posts : 8
    win10
    Thread Starter
       #3

    Thanks Denis
      My Computer


  4. Posts : 16,713
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    I hope you are successful.

    I never really liked ForFiles & might have forgotten much about it in the years since I last experimented with it but it does seem to me that you are sending all your copies to the same folder and that is what is limiting your results.

    Perhaps you need to treat your destination as the highest-level folder and extract your selected files to paths within it that reflect their source paths so that, for example,
    D:\MyDocs\Tom\Dick\TestFile-GS
    is copied to
    %FILEDESTINATION%\MyDocs\Tom\Dick\
    whilst
    D:\MyDocs\Tom\Dick\Harry\TestFile-GS
    is copied to
    %FILEDESTINATION%\MyDocs\Tom\Dick\Harry\

    Personally, since I would not use ForFiles with any confidence if at all, I'd have to do the path-filename processing in a subroutine so I could lay it all out in a way that was amenable to debugging & future script maintenance.
    Something that looks a bit like this [assuming I have got the ForFiles part of the command right],

    Code:
    for /f "delims=" %%x in (%FIILELIST%) do (forfiles /p %SEARCHFOLDER% /s /m "%%x") & (Call :FileProcessing "%%x")
    pause
    
    :FileProcessing
    :: Here's where you would extract and process file paths
    Set "ThisFile=%~1"
    :: and probably where you would write your temporary_log
    echo "%ThisFile%" >>temporary_log.txt
    GoTo :EOF

    The only example of my own that I can find at the moment is based on this line [which does not use ForFiles]
    Code:
    For /f  "usebackq tokens=*" %%X in ("%SEARCHFOLDER%") do Call :FileProcessing "%%X"

    If you do adopt such an approach, you might find these useful
    my ditty and demo for Standard passable variables [post #47] - TenForums
    Manipulating variables in batch files - post #14 - TenForums


    All the best,
    Denis
      My Computer


  5. Posts : 8
    win10
    Thread Starter
       #5

    Thanks Denis for your assistance. It is greatly appreciated and sorry for the slow reply!

    Unfortunately, stackoverflow gave me rather short shrift!

    I think my windows batch file understanding and knowledge leaves a lot to be desired. I shall look at your code and see if I can dissect it.

    What I was hoping for would be a setting where if a windows batch file encounters a 'file with the same name in this location', that it could automatically be set to 'copy, but keep both files' rather than a 'copy and replace' which I believe is what is happening now. Filename copies that have the usual windows Copy naming convention for same images as .....(2).jpg would be ideal for me, as long as it makes those copies according to the identical files that appear in the initial FileList.txt.

    For example, my FileList.txt might have three identical files such as

    Code:
    ai21091458-ai21ndnl-GS.jpg
    
    ai21091458-ai21ndnl-GS.jpg
    
    ai21091458-ai21ndnl-GS.jpg
    but if these could be copied and pasted into the destination folder as

    Code:
    ai21091458-ai21ndnl-GS.jpg
    
    ai21091458-ai21ndnl-GS (2).jpg
    
    ai21091458-ai21ndnl-GS (3).jpg
    I would be one very happy bunny indeed.

    Is this possible?

    Thanks if you, or anybody else, might be able to help me.
      My Computer


  6. Posts : 3,271
    Win10
       #6

    [Sorry, for reposting, but this time the test doesn't involve FORFILES]
    test only:
    As Denis has said, FORFILES seems not only quite difficult to debug, but also it doesn't want to accept any CALL command well.
    Anyway, if I understand your scenario correctly, here is one way you could try and do what you want. BUT, sorry, I would have to leave all the debugging to you as I have only borrowed the code (I wouldn't be able to debug it any further).

    This batch file doesn't use FORFILES as it seems to have a couple of quirks re: specifying a variable %SEARCHFOLDER% name which has spaces in the Top/Main folder. Basically, the first part of the batch file uses the FOR /F command so you can get a NewFileList.txt which shows all the pathnames of the individual files in the recursed sub folders according to the list in your FileList.txt.

    Then, using the code from the following site, it copies over the files from NewFileList.txt - re-numbering the copies as it goes along.

    [Solved] Windows batch file to copy and keep duplicates - Code Redirect

    I hope you can work it out using the test folder.zip attached. Just create a test folder somewhere safe, and copy the test folder structure within it to see how it works.
    Code:
    :: First Part
    :: make full list of all the files existing from the your original FileList.txt with "full paths"
    :: call this new list "NewFileList.txt"
    
    @ECHO ON
    SETLOCAL ENABLEDELAYEDEXPANSION
    set "FILELIST=%cd%\FileList.txt"
    set "SEARCHFOLDER=%cd%\Overlaid-Images"
    set "FILEDESTINATION=%cd%\FilesCategorised\Category01\"
    set "NewFileList=%cd%\NewFileList.txt"
    
    IF NOT EXIST "%FILEDESTINATION%" mkdir "%FILEDESTINATION%"
    IF EXIST "%cd%\NewFileList.txt" del "%cd%\NewFileList.txt"
    
    FOR /F "tokens=*" %%i IN ('TYPE "%FileList%"') DO FOR /F "tokens=*" %%j IN ('DIR /S/B "%%i"') DO ECHO "%%j">>"%cd%\NewFileList.txt"
    
    pause
    
    
    :: Second Part
    :: Do the Copying and re-number duplicates
    
    set "NewFileList=%cd%\NewFileList.txt"
    set "FILEDESTINATION=%cd%\FilesCategorised\Category01\"
    set /a counter=0
    
    FOR /F "tokens=*" %%q IN ('TYPE "%NewFileList%"') DO FOR /F "tokens=*" %%w IN ('DIR /B %%q') DO IF EXIST "%FILEDESTINATION%%%~nxw" (
      set /a counter=!counter!+1
      COPY %%q "%FILEDESTINATION%%%~nq (!counter!)%%~xq"
    ) ELSE COPY %%q "%FILEDESTINATION%%%~nxq" && set /a counter=0
    
    pause
    
    exit
    
    ::    refs
    :: :: https://coderedirect.com/questions/200647/windows-batch-file-to-copy-and-keep-duplicates
    ::    change the format of the "counter" uisng the following format examples
    :: eg "%FILEDESTINATION%%%~ni (!counter!)%%~xi"       >>  filename (3).ext
    :: eg "%FILEDESTINATION%%%~ni copy (!counter!)%%~xi"  >>  filename copy (3).ext
    :: 
    :: etc
    Attached zip with test-folder structure.

    test folder.zip

    PS Notes:
    1 I don't know how you would create temporary_log.txt using this batch.
    2 If you re-run the same batch file again, then, because the counter would be starting at Zero again, you will get lots of duplicated copies.
    3 As I said I leave the debugging to you, but I hope the test gives you some ideas.
      My Computers


  7. Posts : 7,606
    Windows 10 Home 20H2
       #7

    Trail said:
    Unfortunately, stackoverflow gave me rather short shrift!
    I was once banned from asking questions there because I had been asking too many stupid questions, which received tons of downvotes from the experts there.
      My Computer


  8. Posts : 8
    win10
    Thread Starter
       #8

    Thank you das10 for your considerable knowledge and valuable time in helping me with this.

    Your batch script works perfectly for my needs and is going to save me a huge amount of time and increase my accuracy immeasurably.

    - - - Updated - - -

    Sorry, excuse my severe lack of knowledge and naivety. I think you might have omitted the SEARCHFOLDER in your code.

    You have set the following SEARCHFOLDER in the code line 8 as

    Code:
    set "SEARCHFOLDER=%cd%\Overlaid-Images"
    but have not called it in the FOR loop first part of the code.

    It is therefore not only searching the specific SEARCHFOLDER when it creates the NewFileList.txt, but it is searching the whole of the current directory. If I have more than one folder in the current directory, it will add any matches in those other folders to the initial NewFileList.txt it creates.

    If I was only using (your fantastic) batch script search / copy / paste once, this would be fine. But I am going to be searching on multiple FILELISTS (with different names with the correct FILELIST set accordingly) for different sized images within the current directory.

    Is it possible to amend the code so that only the SEARCHFOLDER is actually searched? I have tried myself (again), but am unsure where to place the SEARCHFOLDER in the For loop.

    Yet again, thanks so much if you are able to help. I am very grateful.
      My Computer


  9. Posts : 3,271
    Win10
       #9

    Sorry for having missed the %SEARCHFOLDER% variable ( due to the last minute change over from using FORFILES to just FOR /F ). Hopefully, the folowing will restrict the search to that particular folder (and sub-folders) only.
    Code:
    :: First Part
    :: make full list of all the files existing from the your original FileList.txt with "full paths"
    :: call this new list "NewFileList.txt"
    
    @ECHO ON
    SETLOCAL ENABLEDELAYEDEXPANSION
    set "FILELIST=%cd%\FileList.txt"
    set "SEARCHFOLDER=%cd%\Overlaid-Images\"
    set "FILEDESTINATION=%cd%\FilesCategorised\Category01\"
    set "NewFileList=%cd%\NewFileList.txt"
    
    IF NOT EXIST "%FILEDESTINATION%" mkdir "%FILEDESTINATION%"
    IF EXIST "%cd%\NewFileList.txt" del "%cd%\NewFileList.txt"
    
    cd %SEARCHFOLDER%
    FOR /F "tokens=*" %%i IN ('TYPE "%FileList%"') DO FOR /F "tokens=*" %%j IN ('DIR /S/B "%%i"') DO ECHO "%%j">>"%NewFileList%"
    cd %~dp0
    
    pause
    
    :: Second Part
    :: Do the Copying and re-number duplicates
    
    set "NewFileList=%cd%\NewFileList.txt"
    set "FILEDESTINATION=%cd%\FilesCategorised\Category01\"
    set /a counter=0
    
    FOR /F "tokens=*" %%q IN ('TYPE "%NewFileList%"') DO FOR /F "tokens=*" %%w IN ('DIR /B %%q') DO IF EXIST "%FILEDESTINATION%%%~nxw" (
      set /a counter=!counter!+1
      COPY %%q "%FILEDESTINATION%%%~nq (!counter!)%%~xq"
    ) ELSE COPY %%q "%FILEDESTINATION%%%~nxq" && set /a counter=0
    
    pause
    
    exit
    
    ::    refs
    :: :: https://coderedirect.com/questions/200647/windows-batch-file-to-copy-and-keep-duplicates
    ::    change the format of the "counter" uisng the following format examples
    :: eg "%FILEDESTINATION%%%~ni (!counter!)%%~xi"       >>  filename (3).ext
    :: eg "%FILEDESTINATION%%%~ni copy (!counter!)%%~xi"  >>  filename copy (3).ext
    :: 
    :: etc
    Last edited by das10; 21 Nov 2021 at 06:10. Reason: Code correction line 7 (as per post #10)
      My Computers


  10. Posts : 8
    win10
    Thread Starter
       #10

    Thank you so much das10, working perfectly now and that's going to save me a tonne of work.

    Don't know if it was intentional, but line 7 FILELIST has been changed from the current %cd% to a specific filepath. Easily changed back though.

    Thanks again!!!
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 04:55.
Find Us




Windows 10 Forums