1

I want to search files with .log extension recursively in a directory. I am trying like below command in a batch file.

for /R  %%A in (*.log) do echo %%A >> All_logs.txt

I tried this also:

for /R  %%A in (*.log) do echo %%~nxA >> All_logs.txt

But I am either getting full path like this:

D:\Folder1\sub_folder1\bootmicro.log
D:\Folder1\sub_folder2\debug.log 

or

bootmicro.log
debug.log

respectively.

Since I am running the batch file in the path D:\Folder1, I want search result to be like:

sub_folder1\bootmicro.log
sub_folder2\debug.log

Further, in the same batch file, I want to create folders using the resultant file names as below:

sub_folder1_bootmicro_log_Analysis
sub_folder2_debug_log_Analysis

or

sub_folder1_bootmicro.log_Analysis
sub_folder2_debug.log_Analysis

How can I do this all?

Yash
  • 173
  • 2
  • 13

2 Answers2

3

When you call xcopy with a relative path you get a list of files to copy (the /l means only get the list) with relative paths.

@echo off
    setlocal enableextensions disabledelayedexpansion

    for /f "tokens=1,* delims=\" %%a in ('
        xcopy /l /s .\*.log "%temp%"
    ') do if not "%%b"=="" echo(%%b

The for /f loop will process the list removing the starting .\ (using the backslash as delimiter) for each file in the list.

As the output from xcopy will contain and ending line with the number of files, and this line will not have a second token, the if has been included to avoid an empty line at the end.

edited For the folder creation, delayed expansion will be needed to handle the string replacement, but to prevent problems with ! included in any of the files/folders names, it needs to be enabled/disabled where needed.

@echo off
    setlocal enableextensions disabledelayedexpansion

    for /f "tokens=1,* delims=\" %%a in ('
        xcopy /l /s .\*.log "%temp%"
    ') do if not "%%b"=="" (
        echo(%%b
        set "target=%%b"
        setlocal enabledelayedexpansion
        for %%c in ("!target:\=_!") do (
            endlocal
            md "%%~c_Analysis"
        )
    )
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • 2
    Good idea using `xcopy` to get relative paths; I was thinking of using `forfiles` which is capable of returning relative paths (`@relpath`), but there are always annoying quotes around the output; so it'd become quite complicated removing them and building the new relative path as requested... – aschipfl Sep 14 '15 at 09:53
  • 3
    @aschipfl, Not very different. Tested from command line `forfiles /m *.log /s /c "cmd /v /q /c for /f tokens^=1^,*^ delims^=\ %a in (@relpath) do set x=\"%b\" & echo md ^!x:\=_^!"`. A perfect valid idea. I tend not to use `forfiles` if there is another alternative because I don't like starting one `cmd` instance for each command. Just my bad habits. – MC ND Sep 14 '15 at 10:12
  • Your habits are not that bad actually, @MCND ;-) – aschipfl Sep 14 '15 at 10:24
0

Similar with forfiles

for /F "usebackq delims=" %%a in (`forfiles /s /m *.txt /C "cmd /c echo @relpath"`) do (
  set "myfile=%%~a"
  setlocal EnableDelayedExpansion
  set "myfile=!myfile:~2!
  for %%c in ("!myfile:\=_!") do (
    endlocal
    echo md "%%~c_Analysis"
  )
)
Exit /B
Paul
  • 2,620
  • 2
  • 17
  • 27