1

There is a system that generates a txt file dump every 15 minutes with a different file name each time. I need to find the latest file containing the text 'ASN' and copy it to a folder where I can work on it.

so far I have this, but I cannot get it to copy any file.

SET smart_server='Z:\JUL2017'

FOR /F "delims=|" %%I IN ('DIR "%smart_server%" /S /B /O:D ^| find /i "ASN" ') DO SET NewestFile=%%I
copy "%smart_server%\%NewestFile%" "C:\htdocs\smart_asn_downloads\new" 

The source directory is a mapped drive I'm looking to copy it to a local drive.

5
  • 1
    SET "smart_server=Z:\JUL2017" Your actual variable smart_server contains the path plus the apostrophes. also SET "NewestFile=%%I" as it may contain spaces. and copy "%NewestFile%" ... as it is fully qualified Commented Jul 17, 2017 at 10:51
  • 1
    After changing the content of your %smart_server% variable, as mentioned above, you can improve your DIR command further by removing the pipe to FIND, 'DIR/B/S/A-D-S-L/OD "%smart_server%\*ASN*.txt"'. I added the .txt extension because you said text file, please adjust if necessary.
    – Compo
    Commented Jul 17, 2017 at 11:18
  • @Compo: But he want to search in the content of the file not the filename. Commented Jul 20, 2017 at 7:23
  • Whilst I'm not in a position to disagree with your assumption, @Andre, mine differs based on their post content. They appear to be looking for the latest file with ASN in the file name. I made this assumption because their code tried to do exactly that and it is more reasonable to assume that the randomness of the file name is the issue.
    – Compo
    Commented Jul 20, 2017 at 8:09
  • @Compo: Okay I understand it the other way because of this sentence: I need to find the latest file containing the text 'ASN'. But I think the OP will clarify that ;-). Commented Jul 20, 2017 at 8:11

1 Answer 1

1

The following will copy the files that contains the text ASN ignoring the case in the file itself not the file name:

@echo off
set "smart_server=Z:\JUL2017"

rem list all files (recursive) latest first
for /F "delims=|" %%I in ('dir "%smart_server%" /S /B /A-D /O:-D') do (
   find /i "ASN" "%%I" > nul
   rem success (errorlevel == 0)?
   if not errorlevel 1 (
      set "NewestFile=%%I"
   )
)
if defined NewestFile (
   rem addionally echoing the command due copy is not verbose
   echo copy "%NewestFile%" "C:\htdocs\smart_asn_downloads\new" 
   copy "%NewestFile%" "C:\htdocs\smart_asn_downloads\new" 
) else (
   echo Found no file!
)

There are several things I've changed.

1. set:
I changed the setting of the variable smart_server because your set contains the ' in the path.

2. The dir command:
The sorting with /O:D would show the oldest first to reverse the list use: /O:-D. Further exclude directories to show with dir because you can't search in them with find, use: /A-D.

3. The pipe to find:
It seems that the pipe to find does not work with spaces in a filename therefore I cut it out of the command and do it in the for loop. If the find is successful I set the NewestFile variable. I use find with /I so it ignores the case of the text.


If you need the script to copy files that contain ASN in the file name itself and ends with .txt you can use (this also ignores the case):

@echo off

set "smart_server=Z:\JUL2017"
for /F "delims=|" %%I IN ('DIR "%smart_server%\*ASN*.txt" /S /B /A-D /O:-D') DO SET NewestFile=%%I
echo copy "%NewestFile%" "C:\htdocs\smart_asn_downloads\new" 
copy "%NewestFile%" "C:\htdocs\smart_asn_downloads\new" 
1
  • Hi @IForceRedditch if this has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. If it not solved your problem feel free to ask for more details. Commented Jul 24, 2017 at 6:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.