0

Good Morning!

Coming from another article, which i tried to adopt, but failed, i would like to ask my question in a separate post. I need to achieve two things:

  1. Copy all files that contain a certain string in their filename from one directory to another, but only if that file does not already exists in that target directory. The filename could be something like EventLog_12345.txt and i would want to copy only the files where the filename contains EventLog.

  2. In a set of files identify in every .txt file a certain string. This string indicates the line that contains the string i am looking for. I want to get to the end of this line and save the .txt file as a new .txt file with a new name based on the string i find at the end of this line. Example: My file is EventLog_12345.txt and somewhere in this file there is a line like this:


2018-06-22 08:21:19 0133 LET vVariable                  = 'h**ps://somedomain.com/test/1/2/4/jobs/joblog.XML'

The string indicating the line is vVariable. The string i want to use within the new filename in this example is joblog.xml. The file should be stored as a new .txt file with the name: joblog_12345.txt. Note, that the length of the line can vary; so can the length of the domain string; also the names of the XMLs are different. The constant is that i always want to have the name of the XML file which is always the last piece of the domain.

Adding info on efforts so far
Copy & Paste - this is actually working, but does not check whether a file already exists:

@echo off  
for /f "delims=" %%a in (  
'xcopy /l /e /y "\\myPath\*EventLog*.txt" "D:\Target\" ^|find "EventLog"'
) do copy "%%a" "D:\Target\"    

For the identification of string and then SaveAs i dont really have anything. I was basically hoping i could somehow adjust the solution provided here: (Rename text files based on multiple strings in their contents)

kalinkula
  • 3
  • 1
  • 5
  • 1
    What have you tried ? – Moudiz Jun 22 '18 at 06:53
  • Well, i found some batch that i could adopt to do the copy of the files but didnt get the "if not exists" piece done. And for the renaming of the file i tried to use this approach: https://stackoverflow.com/questions/23095874/rename-text-files-based-on-multiple-strings-in-their-contents But i have to admit that i am not blessed with any batch script skills... – kalinkula Jun 22 '18 at 06:55
  • 1
    Please share your efforts by providing a [mcve] ([edit] your question, don't comment)! Also read the [tour] and learn [ask] here! – aschipfl Jun 22 '18 at 10:34

1 Answers1

0

For 1st

@echo off
Set "Target=D:\Target\"
for /f "delims=" %%A in (  
  'xcopy /l /e /y "\\myPath\*EventLog*.txt" "%Target%" '
) do if not exist "%Target%%%~nxA" copy "%%A" "%Target%"

For 2nd

  • process a list of files containing vVariable with findstr /I /M "vVariable" *_*.txt in %%A
  • Split the found name at the underscore %%B
  • search file to get the Line and exchange all / in the line to spaces to then
  • get the very last element in %%D
  • use %%~nD without extension to form the new name

:: Q:\Test\2018\05\22\SO_50982243.cmd
@Echo off & Setlocal EnableDelayedExpansion
For /f "delims=" %%A in ('findstr /I /M "vVariable" *_*.txt ') Do (
    For /f "tokens=2delims=_" %%B in ("%%~nA") Do (
        For /f "delims=" %%C in ('findstr /I "vVariable" ^<"%%A"') Do Set "Line=%%C"
        Set "Line=!Line:/= !"
        For %%D in (!Line!) Do Set "NewName=%%~nD_%%B%%~xA"
        Echo Ren "%%~A" "!NewName!"
    )
)

Sample output based on your information

> SO_50982243.cmd
Ren "EventLog_12345.txt" "joblog_12345.txt"

If the output looks OK remove the echo in front of ren to really rename.

  • First of all: Thank you very much for your help. One thing that i would like to understand before closing this topic is: How would this approach work if the target files that i want to rename contain a different name ? For example: "Worker FFM Generator.qcc.2018_06_24_14_46_55.txt" and my desired filename should only keep the "2018_06_24_14_46_55" part (ending up in: joblog_2018_06_24_14_46_55.txt) Thanks again! – kalinkula Jun 24 '18 at 15:08
  • Sorry , but no - put a new question into a new question. If my answer solves your question or is helpful consider to [check mark as answer](https://stackoverflow.com/help/accepted-answer) and/or [vote up](https://stackoverflow.com/help/privileges/vote-up) –  Jun 24 '18 at 15:52