Search code examples
batch-filecmdscriptingwindows-scripting

batch - echo multiple do in for loop


I'm unable to output 2 values on the same line using for /r. The for loop is:

FOR /R C:\users\<user_name>\desktop\<folder_path> %%F in (*.*) do (
echo %%~nF >> C:\users\usernameA\desktop\test-forr.txt, #Output 1
C:\folderB\tail -n 1 C:\users\usernameA\desktop\folderABC\%%~nF.txt >> C:\users\joseph.borg\desktop\test-forr.txt # Output 2

The issue is that this loop prints the outputs ontop of each other. Can I place them in the same line next to each other (on the same line seperated by a comma) ?

The variable %%~nf stores text file names (without extension) in the same directory. The line C:\folderB\tail -n 1 refers to the linux command tail, which allows you to retrieve a number of 'n' lines starting from the bottom of the file specified (in this case the file specified is C:\users\usernameA\desktop\folderABC\%%~nF.txt).

Thanks in advance


Solution

  • setlocal enabledelayedexpansion
    FOR /R C:\users\<user_name>\desktop\<folder_path> %%F in (*.*) do (
     for /f %%a in ('C:\folderB\tail -n 1 C:\users\usernameA\desktop\folderABC\%%~nF.txt') do set "line=%%a"
     echo %%~nF,!line!>> C:\users\joseph.borg\desktop\test-forr.txt
    )
    

    I haven't tried this as I don't have tail but it should append filename,lastlineofthefile to the given output file (whose name appear to be different for the two separate redirections in your question.)