Batch file to copy files from a folder which contain values (anywhere in the filename) read from LIST.TXT?












0














This seems an easy task, but I cannot seem to work it out.



I have a LIST.TXT file with as follows:



123456
555789
8888988898
12125


I have a large volume of files with those numbers at the start of their file names:



123456_wedding1.jpg
123456_wedding2.doc
123456_wedding3.xls
555789_henrysales_horse.jpg
555789_goodtimes.mov
8888988898_33.avi
12125_some long description here.asx
12125_shor desc.asx
12125_shor desc2.xlsx


I declare vairables: theList, theSource & theDestination and I simply want to copy the files to theDestination folder (I don't even need subfolders).



Following is the code I attempted to adapt from other kind folk:



REM @ECHO OFF

REM **************************************************

REM Adjust location of list
SET "theList=C:2. ListList.txt"

REM Source dir
SET "theSource=C:2. Files"

REM Target dir
SET "theDestination=C:2. Found"

REM **************************************************

FOR /F "tokens=1,* delims=|" %%A IN (%theList%) DO (
ECHO.
ECHO %%A - %%B
CALL :copy "%%A - %%B"
)

ECHO.
ECHO Done^!
PAUSE
EXIT

:copy
FOR /R "%theSource%" %%F IN (*) DO (
ECHO "%%~nF" | FINDSTR /C:%1 >nul && COPY "%%~fF" "%theDestination%%%~nxF" && EXIT /B
)

pause
EXIT /B


However, I am still getting this error:




The system cannot find the file C:2..




It seems to be something to do with spaces in the files or folders, but I can't nut it out.



Your would be assistance greatly appreciated










share|improve this question
























  • There is an edit link below your post (and a delete link) :) I've fixed it for you ...
    – DavidPostill
    2 days ago












  • Your List.txt file seems to have only one number per line, but you try to parse two delimited by a verticval bar? Passing this to the :copy subroutine with a trailing space dash space won't find any correspondent file as they start with number underscore.
    – LotPings
    yesterday
















0














This seems an easy task, but I cannot seem to work it out.



I have a LIST.TXT file with as follows:



123456
555789
8888988898
12125


I have a large volume of files with those numbers at the start of their file names:



123456_wedding1.jpg
123456_wedding2.doc
123456_wedding3.xls
555789_henrysales_horse.jpg
555789_goodtimes.mov
8888988898_33.avi
12125_some long description here.asx
12125_shor desc.asx
12125_shor desc2.xlsx


I declare vairables: theList, theSource & theDestination and I simply want to copy the files to theDestination folder (I don't even need subfolders).



Following is the code I attempted to adapt from other kind folk:



REM @ECHO OFF

REM **************************************************

REM Adjust location of list
SET "theList=C:2. ListList.txt"

REM Source dir
SET "theSource=C:2. Files"

REM Target dir
SET "theDestination=C:2. Found"

REM **************************************************

FOR /F "tokens=1,* delims=|" %%A IN (%theList%) DO (
ECHO.
ECHO %%A - %%B
CALL :copy "%%A - %%B"
)

ECHO.
ECHO Done^!
PAUSE
EXIT

:copy
FOR /R "%theSource%" %%F IN (*) DO (
ECHO "%%~nF" | FINDSTR /C:%1 >nul && COPY "%%~fF" "%theDestination%%%~nxF" && EXIT /B
)

pause
EXIT /B


However, I am still getting this error:




The system cannot find the file C:2..




It seems to be something to do with spaces in the files or folders, but I can't nut it out.



Your would be assistance greatly appreciated










share|improve this question
























  • There is an edit link below your post (and a delete link) :) I've fixed it for you ...
    – DavidPostill
    2 days ago












  • Your List.txt file seems to have only one number per line, but you try to parse two delimited by a verticval bar? Passing this to the :copy subroutine with a trailing space dash space won't find any correspondent file as they start with number underscore.
    – LotPings
    yesterday














0












0








0


0





This seems an easy task, but I cannot seem to work it out.



I have a LIST.TXT file with as follows:



123456
555789
8888988898
12125


I have a large volume of files with those numbers at the start of their file names:



123456_wedding1.jpg
123456_wedding2.doc
123456_wedding3.xls
555789_henrysales_horse.jpg
555789_goodtimes.mov
8888988898_33.avi
12125_some long description here.asx
12125_shor desc.asx
12125_shor desc2.xlsx


I declare vairables: theList, theSource & theDestination and I simply want to copy the files to theDestination folder (I don't even need subfolders).



Following is the code I attempted to adapt from other kind folk:



REM @ECHO OFF

REM **************************************************

REM Adjust location of list
SET "theList=C:2. ListList.txt"

REM Source dir
SET "theSource=C:2. Files"

REM Target dir
SET "theDestination=C:2. Found"

REM **************************************************

FOR /F "tokens=1,* delims=|" %%A IN (%theList%) DO (
ECHO.
ECHO %%A - %%B
CALL :copy "%%A - %%B"
)

ECHO.
ECHO Done^!
PAUSE
EXIT

:copy
FOR /R "%theSource%" %%F IN (*) DO (
ECHO "%%~nF" | FINDSTR /C:%1 >nul && COPY "%%~fF" "%theDestination%%%~nxF" && EXIT /B
)

pause
EXIT /B


However, I am still getting this error:




The system cannot find the file C:2..




It seems to be something to do with spaces in the files or folders, but I can't nut it out.



Your would be assistance greatly appreciated










share|improve this question















This seems an easy task, but I cannot seem to work it out.



I have a LIST.TXT file with as follows:



123456
555789
8888988898
12125


I have a large volume of files with those numbers at the start of their file names:



123456_wedding1.jpg
123456_wedding2.doc
123456_wedding3.xls
555789_henrysales_horse.jpg
555789_goodtimes.mov
8888988898_33.avi
12125_some long description here.asx
12125_shor desc.asx
12125_shor desc2.xlsx


I declare vairables: theList, theSource & theDestination and I simply want to copy the files to theDestination folder (I don't even need subfolders).



Following is the code I attempted to adapt from other kind folk:



REM @ECHO OFF

REM **************************************************

REM Adjust location of list
SET "theList=C:2. ListList.txt"

REM Source dir
SET "theSource=C:2. Files"

REM Target dir
SET "theDestination=C:2. Found"

REM **************************************************

FOR /F "tokens=1,* delims=|" %%A IN (%theList%) DO (
ECHO.
ECHO %%A - %%B
CALL :copy "%%A - %%B"
)

ECHO.
ECHO Done^!
PAUSE
EXIT

:copy
FOR /R "%theSource%" %%F IN (*) DO (
ECHO "%%~nF" | FINDSTR /C:%1 >nul && COPY "%%~fF" "%theDestination%%%~nxF" && EXIT /B
)

pause
EXIT /B


However, I am still getting this error:




The system cannot find the file C:2..




It seems to be something to do with spaces in the files or folders, but I can't nut it out.



Your would be assistance greatly appreciated







batch-file path






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









DavidPostill

104k25224258




104k25224258










asked 2 days ago









juzzlejuzzle

193




193












  • There is an edit link below your post (and a delete link) :) I've fixed it for you ...
    – DavidPostill
    2 days ago












  • Your List.txt file seems to have only one number per line, but you try to parse two delimited by a verticval bar? Passing this to the :copy subroutine with a trailing space dash space won't find any correspondent file as they start with number underscore.
    – LotPings
    yesterday


















  • There is an edit link below your post (and a delete link) :) I've fixed it for you ...
    – DavidPostill
    2 days ago












  • Your List.txt file seems to have only one number per line, but you try to parse two delimited by a verticval bar? Passing this to the :copy subroutine with a trailing space dash space won't find any correspondent file as they start with number underscore.
    – LotPings
    yesterday
















There is an edit link below your post (and a delete link) :) I've fixed it for you ...
– DavidPostill
2 days ago






There is an edit link below your post (and a delete link) :) I've fixed it for you ...
– DavidPostill
2 days ago














Your List.txt file seems to have only one number per line, but you try to parse two delimited by a verticval bar? Passing this to the :copy subroutine with a trailing space dash space won't find any correspondent file as they start with number underscore.
– LotPings
yesterday




Your List.txt file seems to have only one number per line, but you try to parse two delimited by a verticval bar? Passing this to the :copy subroutine with a trailing space dash space won't find any correspondent file as they start with number underscore.
– LotPings
yesterday










2 Answers
2






active

oldest

votes


















0














Would PowerShell be an option for this? This seems like a better tool for the job and would certainly make it simpler. For example, the bones of the solution could be:



$theList        = "C:2. ListList.txt"
$theSource = "C:2. Files"
$theDestination = "C:2. Found"
$toCheck = gc $theList

Get-ChildItem $theSource | Foreach-Object{
if($toCheck -match $_.BaseName)
{
write-host "matches in list file $($theList):" $_.FullName
copy-item $_.FullName -destination $theDestination
}
}


This will just copy the file. If you want to move it consider using move-item.



If you really want sub-strings of the base file name to match entries in the list file, a verbose way, to make it readable and for you to modify or extend, the following could help:



$theList        = "C:2. ListList.txt"
$theSource = "C:2. Files"
$theDestination = "C:2. Found"
$toCheck = gc $theList

function inArrayasSub([string]$stringToCheck)
{
write-host "`tchecking: '$($stringToCheck)' exists (substring) in file " $theList

foreach ($entry in $toCheck)
{
if ($stringToCheck -match $entry)
{
write-host "`tExists based on entry: $entry"
return $true
}
}
return $false
}

Get-ChildItem $theSource | Foreach-Object{

write-host "Testing file basename: "$_.BaseName

if (inArrayasSub $_.BaseName)
{
write-host $_.BaseName "matches in list file $($theList):" $_.FullName
copy-item $_.FullName -destination $theDestination
}
write-host "======================"
}





share|improve this answer































    0














    Batch file which:




    • splits all files from the source at the underscore

    • uses findstr /G: to compare the splitted number with theList

    • if the number is present copy else echo message


    Untested:



    :: Q:Test201916SU_1390824.cmd
    @ECHO OFF

    :: Adjust location of list
    Set "theList=C:2. ListList.txt"
    :: Source dir
    Set "theSource=C:2. Files"
    :: Target dir
    Set "theDestination=C:2. Found"

    For /R "%theSource%" %%A IN (*) DO For /F "delims=_" %%B in ("%%~nA") DO (
    ECHO:%%B|FINDSTR /XG:"%theList%" >nul 2>&1 && (
    COPY "%%~fA" "%theDestination%%%~nxA"
    )||(
    Echo Not in theList: %%B , %%A
    )
    )
    Echo Done^!
    Pause
    Exit /B





    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1390824%2fbatch-file-to-copy-files-from-a-folder-which-contain-values-anywhere-in-the-fil%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Would PowerShell be an option for this? This seems like a better tool for the job and would certainly make it simpler. For example, the bones of the solution could be:



      $theList        = "C:2. ListList.txt"
      $theSource = "C:2. Files"
      $theDestination = "C:2. Found"
      $toCheck = gc $theList

      Get-ChildItem $theSource | Foreach-Object{
      if($toCheck -match $_.BaseName)
      {
      write-host "matches in list file $($theList):" $_.FullName
      copy-item $_.FullName -destination $theDestination
      }
      }


      This will just copy the file. If you want to move it consider using move-item.



      If you really want sub-strings of the base file name to match entries in the list file, a verbose way, to make it readable and for you to modify or extend, the following could help:



      $theList        = "C:2. ListList.txt"
      $theSource = "C:2. Files"
      $theDestination = "C:2. Found"
      $toCheck = gc $theList

      function inArrayasSub([string]$stringToCheck)
      {
      write-host "`tchecking: '$($stringToCheck)' exists (substring) in file " $theList

      foreach ($entry in $toCheck)
      {
      if ($stringToCheck -match $entry)
      {
      write-host "`tExists based on entry: $entry"
      return $true
      }
      }
      return $false
      }

      Get-ChildItem $theSource | Foreach-Object{

      write-host "Testing file basename: "$_.BaseName

      if (inArrayasSub $_.BaseName)
      {
      write-host $_.BaseName "matches in list file $($theList):" $_.FullName
      copy-item $_.FullName -destination $theDestination
      }
      write-host "======================"
      }





      share|improve this answer




























        0














        Would PowerShell be an option for this? This seems like a better tool for the job and would certainly make it simpler. For example, the bones of the solution could be:



        $theList        = "C:2. ListList.txt"
        $theSource = "C:2. Files"
        $theDestination = "C:2. Found"
        $toCheck = gc $theList

        Get-ChildItem $theSource | Foreach-Object{
        if($toCheck -match $_.BaseName)
        {
        write-host "matches in list file $($theList):" $_.FullName
        copy-item $_.FullName -destination $theDestination
        }
        }


        This will just copy the file. If you want to move it consider using move-item.



        If you really want sub-strings of the base file name to match entries in the list file, a verbose way, to make it readable and for you to modify or extend, the following could help:



        $theList        = "C:2. ListList.txt"
        $theSource = "C:2. Files"
        $theDestination = "C:2. Found"
        $toCheck = gc $theList

        function inArrayasSub([string]$stringToCheck)
        {
        write-host "`tchecking: '$($stringToCheck)' exists (substring) in file " $theList

        foreach ($entry in $toCheck)
        {
        if ($stringToCheck -match $entry)
        {
        write-host "`tExists based on entry: $entry"
        return $true
        }
        }
        return $false
        }

        Get-ChildItem $theSource | Foreach-Object{

        write-host "Testing file basename: "$_.BaseName

        if (inArrayasSub $_.BaseName)
        {
        write-host $_.BaseName "matches in list file $($theList):" $_.FullName
        copy-item $_.FullName -destination $theDestination
        }
        write-host "======================"
        }





        share|improve this answer


























          0












          0








          0






          Would PowerShell be an option for this? This seems like a better tool for the job and would certainly make it simpler. For example, the bones of the solution could be:



          $theList        = "C:2. ListList.txt"
          $theSource = "C:2. Files"
          $theDestination = "C:2. Found"
          $toCheck = gc $theList

          Get-ChildItem $theSource | Foreach-Object{
          if($toCheck -match $_.BaseName)
          {
          write-host "matches in list file $($theList):" $_.FullName
          copy-item $_.FullName -destination $theDestination
          }
          }


          This will just copy the file. If you want to move it consider using move-item.



          If you really want sub-strings of the base file name to match entries in the list file, a verbose way, to make it readable and for you to modify or extend, the following could help:



          $theList        = "C:2. ListList.txt"
          $theSource = "C:2. Files"
          $theDestination = "C:2. Found"
          $toCheck = gc $theList

          function inArrayasSub([string]$stringToCheck)
          {
          write-host "`tchecking: '$($stringToCheck)' exists (substring) in file " $theList

          foreach ($entry in $toCheck)
          {
          if ($stringToCheck -match $entry)
          {
          write-host "`tExists based on entry: $entry"
          return $true
          }
          }
          return $false
          }

          Get-ChildItem $theSource | Foreach-Object{

          write-host "Testing file basename: "$_.BaseName

          if (inArrayasSub $_.BaseName)
          {
          write-host $_.BaseName "matches in list file $($theList):" $_.FullName
          copy-item $_.FullName -destination $theDestination
          }
          write-host "======================"
          }





          share|improve this answer














          Would PowerShell be an option for this? This seems like a better tool for the job and would certainly make it simpler. For example, the bones of the solution could be:



          $theList        = "C:2. ListList.txt"
          $theSource = "C:2. Files"
          $theDestination = "C:2. Found"
          $toCheck = gc $theList

          Get-ChildItem $theSource | Foreach-Object{
          if($toCheck -match $_.BaseName)
          {
          write-host "matches in list file $($theList):" $_.FullName
          copy-item $_.FullName -destination $theDestination
          }
          }


          This will just copy the file. If you want to move it consider using move-item.



          If you really want sub-strings of the base file name to match entries in the list file, a verbose way, to make it readable and for you to modify or extend, the following could help:



          $theList        = "C:2. ListList.txt"
          $theSource = "C:2. Files"
          $theDestination = "C:2. Found"
          $toCheck = gc $theList

          function inArrayasSub([string]$stringToCheck)
          {
          write-host "`tchecking: '$($stringToCheck)' exists (substring) in file " $theList

          foreach ($entry in $toCheck)
          {
          if ($stringToCheck -match $entry)
          {
          write-host "`tExists based on entry: $entry"
          return $true
          }
          }
          return $false
          }

          Get-ChildItem $theSource | Foreach-Object{

          write-host "Testing file basename: "$_.BaseName

          if (inArrayasSub $_.BaseName)
          {
          write-host $_.BaseName "matches in list file $($theList):" $_.FullName
          copy-item $_.FullName -destination $theDestination
          }
          write-host "======================"
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          HelpingHandHelpingHand

          98549




          98549

























              0














              Batch file which:




              • splits all files from the source at the underscore

              • uses findstr /G: to compare the splitted number with theList

              • if the number is present copy else echo message


              Untested:



              :: Q:Test201916SU_1390824.cmd
              @ECHO OFF

              :: Adjust location of list
              Set "theList=C:2. ListList.txt"
              :: Source dir
              Set "theSource=C:2. Files"
              :: Target dir
              Set "theDestination=C:2. Found"

              For /R "%theSource%" %%A IN (*) DO For /F "delims=_" %%B in ("%%~nA") DO (
              ECHO:%%B|FINDSTR /XG:"%theList%" >nul 2>&1 && (
              COPY "%%~fA" "%theDestination%%%~nxA"
              )||(
              Echo Not in theList: %%B , %%A
              )
              )
              Echo Done^!
              Pause
              Exit /B





              share|improve this answer


























                0














                Batch file which:




                • splits all files from the source at the underscore

                • uses findstr /G: to compare the splitted number with theList

                • if the number is present copy else echo message


                Untested:



                :: Q:Test201916SU_1390824.cmd
                @ECHO OFF

                :: Adjust location of list
                Set "theList=C:2. ListList.txt"
                :: Source dir
                Set "theSource=C:2. Files"
                :: Target dir
                Set "theDestination=C:2. Found"

                For /R "%theSource%" %%A IN (*) DO For /F "delims=_" %%B in ("%%~nA") DO (
                ECHO:%%B|FINDSTR /XG:"%theList%" >nul 2>&1 && (
                COPY "%%~fA" "%theDestination%%%~nxA"
                )||(
                Echo Not in theList: %%B , %%A
                )
                )
                Echo Done^!
                Pause
                Exit /B





                share|improve this answer
























                  0












                  0








                  0






                  Batch file which:




                  • splits all files from the source at the underscore

                  • uses findstr /G: to compare the splitted number with theList

                  • if the number is present copy else echo message


                  Untested:



                  :: Q:Test201916SU_1390824.cmd
                  @ECHO OFF

                  :: Adjust location of list
                  Set "theList=C:2. ListList.txt"
                  :: Source dir
                  Set "theSource=C:2. Files"
                  :: Target dir
                  Set "theDestination=C:2. Found"

                  For /R "%theSource%" %%A IN (*) DO For /F "delims=_" %%B in ("%%~nA") DO (
                  ECHO:%%B|FINDSTR /XG:"%theList%" >nul 2>&1 && (
                  COPY "%%~fA" "%theDestination%%%~nxA"
                  )||(
                  Echo Not in theList: %%B , %%A
                  )
                  )
                  Echo Done^!
                  Pause
                  Exit /B





                  share|improve this answer












                  Batch file which:




                  • splits all files from the source at the underscore

                  • uses findstr /G: to compare the splitted number with theList

                  • if the number is present copy else echo message


                  Untested:



                  :: Q:Test201916SU_1390824.cmd
                  @ECHO OFF

                  :: Adjust location of list
                  Set "theList=C:2. ListList.txt"
                  :: Source dir
                  Set "theSource=C:2. Files"
                  :: Target dir
                  Set "theDestination=C:2. Found"

                  For /R "%theSource%" %%A IN (*) DO For /F "delims=_" %%B in ("%%~nA") DO (
                  ECHO:%%B|FINDSTR /XG:"%theList%" >nul 2>&1 && (
                  COPY "%%~fA" "%theDestination%%%~nxA"
                  )||(
                  Echo Not in theList: %%B , %%A
                  )
                  )
                  Echo Done^!
                  Pause
                  Exit /B






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  LotPingsLotPings

                  4,6861722




                  4,6861722






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Super User!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1390824%2fbatch-file-to-copy-files-from-a-folder-which-contain-values-anywhere-in-the-fil%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to make a Squid Proxy server?

                      Is this a new Fibonacci Identity?

                      19世紀