Pb with variables ans loops

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dosbenx
Posts: 3
Joined: 18 Apr 2013 05:43

Pb with variables ans loops

#1 Post by dosbenx » 18 Apr 2013 06:55

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,

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Pb with variables ans loops

#2 Post by Endoro » 18 Apr 2013 07:05

Why not this:

Code: Select all

type *.txt>backup.log

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Pb with variables ans loops

#3 Post by foxidrive » 18 Apr 2013 08:09

This should work - make sure there are no subdirectories with matching files.

findstr /s /c:"jelly bean" *.txt >file.log

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Pb with variables ans loops

#4 Post by Endoro » 18 Apr 2013 09:25

Or try this:

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

dosbenx
Posts: 3
Joined: 18 Apr 2013 05:43

Re: Pb with variables ans loops

#5 Post by dosbenx » 18 Apr 2013 10:20

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

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Pb with variables ans loops

#6 Post by Endoro » 18 Apr 2013 10:27

Yes, you asked "fileX", without the .txt:

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.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Pb with variables ans loops

#7 Post by Aacini » 18 Apr 2013 16:39

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

dosbenx
Posts: 3
Joined: 18 Apr 2013 05:43

Re: Pb with variables ans loops

#8 Post by dosbenx » 19 Apr 2013 04:25

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 ! :D
(2 days ago I didn't know Dostips, I have it know in my favorits)

Post Reply