0

I need to get log files from 2 diff directories and copy them into same target directory based on some conditions on logfilenames. Logic of getting logs based on logfilenames is working fine and they are printed to console.

Below is the script:

@ECHO OFF
for /f "delims=" %%F in (
dir /b "C:\temp1\*web_feed*.out*.gz"^|findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.) do if "%%F" geq "20130101" if "%%F" lss "20130931" echo %%F
for /f "delims=" %%F in ('dir /b "C:\temp2\*web_feed*.out*.gz"^|findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.) do if "%%F" geq "20130101" if "%%F" lss "20130931" echo %%F
pause "Please enter any key to continue"

Now the above script brings log files from c:\temp1 and c:\temp2 based on date conditions and print to console using echo, but I want those log files to be copied to target directory. Where do I need to add the copy condition so that the logfiles obtained from c:\temp1 and c:\temp2 directory are copied into c:\temp3 directory?

Also can I accomplish the duplicate steps of coping log files from 2 different directories in single step, instead of 2 separate steps for separate directories.

I am novice to batch scripting, my script is not working when I add copy command.

5
  • Does the script above even working?I can see some syntax issues.What is the format of *web_feed*.out*.gz files? Commented Oct 16, 2013 at 5:06
  • Format of file:20130401.001_visual_sciences_web_feed.out Commented Oct 16, 2013 at 8:25
  • Above script is working fine, actually i have added 2 scripts in above , one for c:\temp1 and other from c:\temp2, For one @ECHO OFF for /f "delims=" %%F in ( 'dir /b "c:\temp1*feed.out.gz"^|findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.' ) do if "%%F" geq "20130101" if "%%F" lss "20130930" echo %%F Commented Oct 16, 2013 at 8:27
  • Now i want the filtered log filnames to be copied into some other directory say c:\temp3, for that i am using copy cmmand at end as, but it is not working @ECHO OFF for /f "delims=" %%F in ( 'dir /b "c:\temp1*feed.out.gz"^|findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.' ) do if "%%F" geq "20130101" if "%%F" lss "20130930" xcopy "%%F" "c:\temp3\" please suggest Commented Oct 16, 2013 at 8:30
  • Format YYYYMMDD.xxx_*feed.out.gz Commented Oct 16, 2013 at 8:39

1 Answer 1

1

Your script was broken, I have corrected the obvious errors I think and added code to copy the files to c:\temp3 and also added a prefix to the filenames - as if the filenames in the two folders are the same then they will overwrite each other.

@echo off
md "c:\temp3" 2>nul
pushd "C:\temp1\"
for /f "delims=" %%F in ('dir /b "*web_feed*.out*.gz"^|findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\."') do if "%%F" geq "20130101" if "%%F" lss "20130931" copy "%%F" "c:\temp3\temp1-%%F"
popd
pushd "C:\temp2\"
for /f "delims=" %%F in ('dir /b "*web_feed*.out*.gz"^|findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\."') do if "%%F" geq "20130101" if "%%F" lss "20130931" copy "%%F" "c:\temp3\temp2-%%F"
popd
pause
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks ,its working now , also i am not getting how to use for loop to iterate through the 2 directories(temp1 and temp2) , as they were doing the same thing , So that I want to avoid 2 individual steps ,Can you please let me know how we can do that ?
They are returning different files. You need to separate them and two loops is easy. Without further details on filenames and differences between the two sets of files, we're guessing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.