0

I need to find all files that are in the .txt file list within folder and all subfolders

then move the files into a new location

I have tried /r command to search subfolder but it dose not seem to work

it seem to work fine when all files are in the main directery but I cannot get the script to Search the subfolders

Layout of imageslist.txt file

image1*.png     1 
image2*.png     4 
Image 6*.png    2

below if the code I have

    @echo off 
    set Source=C:\Users\Desktop\Folder1\IMAGES
    set Target=C:\Users\Desktop\IMAGES MOVED
    set FileList=C:\Users\Desktop\imageslist.txt
    echo.
    if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit 
    if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
    if not exist "%Target%" md "%Target%" 

    for /F "usebackq tokens=1-2" %%a in ("%FileList%") do call :CopyFile "%%a" %%b
    for /R %%f in ("%FileList%") do call :CopyFile "%%a" %%b


    :Exit
    echo.
    echo press the Space Bar to close this window.
    pause > nul
    exit /b 0
:CopyFile
:: first argument  = filename
:: second argument = number of copies

REM A little trick that will put limit on 0 if second argument is empty or not a number
set secondarg=%~2
set /a limit=secondarg

REM if limit is invalid (not strict positive), exit the function
IF %limit% LEQ 0 (
    echo Invalid number of copies
    exit /b 1
)
IF NOT EXIST "%Target%\%~1" (
    copy "%Source%\%~1" "%Target%"
    IF %limit% LEQ 1 exit /b 0
    set /a limit-=1
)

REM File already exists: search correct index for filename
set index=0
set "targetfile=%target%\%~n1"
set file_ext=%~x1

:following
set /a index+=1
Rem if file with index %index% already exists, go back to get following index
IF exist "%targetfile%(%index%).%file_ext%" goto :following

Rem we have the correct index, now we can copy
set /a limit=index+limit-1
FOR /L %%g IN (%index%,1,%limit%) DO copy "%Source%\%~1" "%targetfile%(%%g).%file_ext%"
exit /b 0
sid
  • 1
  • 1
  • sid, you really need to provide more information and show us the code you've tried in order to achieve your goal, _(what you've provided makes no attempt at checking or searching sub folders)_. Additionally, you haven't provided the `:CopyFile` portion of the script, the content and layout of `imageslist.txt` or properly explained the source subfolder structure. You can [edit your question](https://stackoverflow.com/posts/49184679/edit) to provide that information, please do not add it to the comment area. – Compo Mar 09 '18 at 14:55
  • Thank you for your comments have made the changes hope this makes more sense. – sid Mar 09 '18 at 15:36
  • Are you sure that your imageslist.txt file looks like that, _with everything on one line with numbers separated by multiple spaces between them_? – Compo Mar 09 '18 at 15:48
  • sorry its in multiple lines, when adding it as a block quote it seem to add it all to 1 line, have made the change – sid Mar 09 '18 at 15:54
  • Also can you please explain how your question differs from your last two, [here](https://stackoverflow.com/questions/43378554/batch-process-copy-rename-and-move-file-to-move-copy-file-location) and [here](https://stackoverflow.com/questions/41913799/batch-file-to-move-files-based-on-a-list-without-overwriting-old-file-and-number), especially as the latter has an accepted answer from @J.Baoby. – Compo Mar 09 '18 at 16:03
  • It differs as now I am trying to search the subfolders not just the main directory – sid Mar 09 '18 at 16:08
  • You should have at least referenced the questions and the user who provided the accepted answer. It would have at least given us an opportunity to better understand the background and @J.Baoby, a chance to refine his solution for you. Also creating a new account, even though it's the same name, made those questions and answers even less accessible to us! – Compo Mar 09 '18 at 16:17

0 Answers0