title: Download all the txt files from a website
body: <p>I would like to download multiple text files which ends with .txt from a website. I am trying to use these answers <a href="https://stackoverflow.com/questions/33790052/download-all-files-from-a-folder-on-a-website">Download All Files From a Folder on a Website</a></p>
  <p>So I changed the answers to txt ending files but none of the solutions from this page worked for me. I think I have to change this line of the first answer</p>
  <pre><code>  xpQuery = &quot;//a/@href['.txt'=substring(., string-length(.) - 3)]&quot;
  </code></pre>
  <p>for txt files but I don't know how.</p>
  <p>The edited code is something like below:</p>
  <pre><code>## your base url
  url &lt;- &quot;https://...&quot;
  ## query the url to get all the file names ending in '.txt'
  txts &lt;- XML::getHTMLLinks(
    url, 
    xpQuery = &quot;//a/@href['.txt'=substring(., string-length(.) - 3)]&quot;
  )
  ## create a new directory 'mytxts' to hold the downloads
  dir.create(&quot;mytxts&quot;)
  ## save the current directory path for later
  wd &lt;- getwd()
  ## change working directory for the download
  setwd(&quot;mytxts&quot;)
  ## create all the new files
  file.create(txts)
  ## download them all
  lapply(paste0(url, txts), function(x) download.file(x, basename(x)))
  ## reset working directory to original
  setwd(wd)
  </code></pre>