Pb with variables ans loops
Moderator: DosItHelp
Pb with variables ans loops
Hello,
I have txt files in a one folder :
file1.txt
file2.txt
file3.txt
...
each file has just one line that I want to save in 'backup.log' .
I want to save also the name of each files.
So I will have somthing like that in 'backup.log' :
file1;the-line-of-file1
file2;the-line-of-file2
file3;the-line-of-file3
...
In my little script I have done 2 loops, but it doesn't work. I think I have error with the variables
for /f "tokens=*" %%a in ('dir /b L:\*.txt') do (
for /f %%b in (L:\%%a) do (
set filename=%%a:~-4%
echo %filename%;%%b >> backup.log
)
)
ENDLOCAL
Thanks for your help,
I have txt files in a one folder :
file1.txt
file2.txt
file3.txt
...
each file has just one line that I want to save in 'backup.log' .
I want to save also the name of each files.
So I will have somthing like that in 'backup.log' :
file1;the-line-of-file1
file2;the-line-of-file2
file3;the-line-of-file3
...
In my little script I have done 2 loops, but it doesn't work. I think I have error with the variables
for /f "tokens=*" %%a in ('dir /b L:\*.txt') do (
for /f %%b in (L:\%%a) do (
set filename=%%a:~-4%
echo %filename%;%%b >> backup.log
)
)
ENDLOCAL
Thanks for your help,
Re: Pb with variables ans loops
Why not this:
Code: Select all
type *.txt>backup.log
Re: Pb with variables ans loops
This should work - make sure there are no subdirectories with matching files.
findstr /s /c:"jelly bean" *.txt >file.log
findstr /s /c:"jelly bean" *.txt >file.log
Re: Pb with variables ans loops
Or try this:
Edit: this might also work:
Code: Select all
(for /f "delims=" %i in ('dir /b /a-d *.txt') do @for /f "delims=" %j in ('type "%~i"') do @echo(%~i;%j)>backup.log
Edit: this might also work:
Code: Select all
findstr /svc:"jelly bean" *.txt >file.log
Re: Pb with variables ans loops
Thanks for your quick answer
I have put the line in a script and it works nearly :
echo > c:\backup.log
for /f "delims=" %%i in ('dir /b /a-d l:\mail\4*.txt') do (
@for /f "delims=" %%j in ('type "l:\mail\%%~i"') do (
@echo(%%~i:~-4;%%j) >> c:\backup.log
))
I do not manage to extract the name of the file, without '.txt'
I don't understand why there is %%~i instead of %%i
I have put the line in a script and it works nearly :
echo > c:\backup.log
for /f "delims=" %%i in ('dir /b /a-d l:\mail\4*.txt') do (
@for /f "delims=" %%j in ('type "l:\mail\%%~i"') do (
@echo(%%~i:~-4;%%j) >> c:\backup.log
))
I do not manage to extract the name of the file, without '.txt'
I don't understand why there is %%~i instead of %%i
Re: Pb with variables ans loops
Yes, you asked "fileX", without the .txt:
%%~i is %%i without double quotes, see "help for" for help.
Code: Select all
(for /f "delims=" %%i in ('dir /b /a-d "l:\mail\4*.txt"') do (
@for /f "delims=" %%j in ('type "l:\mail\%%~i"') do (
@echo(%%~ni;%%j
)))>c:\backup.log
%%~i is %%i without double quotes, see "help for" for help.
Re: Pb with variables ans loops
You may try:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
(for /F %%i in (L:\mail\4*.txt) do (
set /P line=< "%%i"
echo %%~Ni;!line!
)) > backup.log
Re: Pb with variables ans loops
Hello,
It's working with :
@echo off
(for /f "delims=" %%i in ('dir /b /a-d "l:\mail\4*.txt"') do (
@for /f "delims=" %%j in ('type "l:\mail\%%~i"') do (
@echo(%%~ni;%%j
)))>c:\backup.log
Thanks you very much for your quick accurate answers !
(2 days ago I didn't know Dostips, I have it know in my favorits)
It's working with :
@echo off
(for /f "delims=" %%i in ('dir /b /a-d "l:\mail\4*.txt"') do (
@for /f "delims=" %%j in ('type "l:\mail\%%~i"') do (
@echo(%%~ni;%%j
)))>c:\backup.log
Thanks you very much for your quick accurate answers !

(2 days ago I didn't know Dostips, I have it know in my favorits)