DosTips.com

A Forum all about DOS Batch
It is currently 02 Dec 2016 16:16

All times are UTC-06:00




Post new topic  Reply to topic  [ 19 posts ]  Go to page 1 2 Next
Author Message
PostPosted: 06 Apr 2012 07:02 
Offline

Joined: 05 Sep 2011 09:15
Posts: 114
log.log
Quote:
a
b


Current directory
Quote:
1.txt
2.txt


I want to rename 1.txt a.txt and 2.txt b.txt. I've tried using two loops below
but just produced results like

1.txt a.txt
1.txt b.txt
2.txt a.txt
2.txt b.txt

How to combine the loops together?

Code: Select all
@ECHO OFF

FOR /F "tokens=*" %%A IN ('DIR *.txt /B /S') DO SET "old=%%A" & CALL :old
GOTO :end

:old
FOR /F "tokens=1" %%B IN (log.log) DO SET "new=%%B" & CALL :new
PAUSE
GOTO :EOF

:new
ECHO %old%, %new%
PAUSE
GOTO :EOF

:end
EXIT


Top
   
PostPosted: 06 Apr 2012 07:48 
Offline
Expert

Joined: 17 Oct 2009 08:30
Posts: 378
Location: Russia
Code: Select all
@echo off &setlocal enableextensions

set /a cnt=0
for /f "delims=" %%a in ('dir /b /on *.txt') do call :ren "%%a"
goto :eof

:ren
for /f "tokens=1" %%b in ('more +%cnt% log.log') do (
   echo ren %1 "%%b.txt"
   set /a cnt+=1 &goto :eof
)


Top
   
PostPosted: 06 Apr 2012 07:48 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Try this it worked for me:
Code: Select all
@ECHO OFF

for /f %%a in ('dir *.txt /b') do (
for /f %%b in (log.log) do ren %%a %%b.txt
)
pause


Top
   
PostPosted: 06 Apr 2012 07:55 
Offline

Joined: 05 Sep 2011 09:15
Posts: 114
abc0502 wrote:
Try this it worked for me:
Code: Select all
@ECHO OFF

for /f %%a in ('dir *.txt /b') do (
for /f %%b in (log.log) do ren %%a %%b.txt
)
pause


This produces a CMD error:
Quote:
The system cannot find the file specified.
A duplicate file name exists, or the file
cannot be found.
Press any key to continue . . .

Yet it works.


Top
   
PostPosted: 06 Apr 2012 08:04 
Offline

Joined: 05 Sep 2011 09:15
Posts: 114
!k wrote:
Code: Select all
@echo off &setlocal enableextensions

set /a cnt=0
for /f "delims=" %%a in ('dir /b /on *.txt') do call :ren "%%a"
goto :eof

:ren
for /f "tokens=1" %%b in ('more +%cnt% log.log') do (
   echo ren %1 "%%b.txt"
   set /a cnt+=1 &goto :eof
)


It does not work, with the cmd window popping out for half a second and disappearing.


Top
   
PostPosted: 06 Apr 2012 08:05 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
i tried on my desktop and there was no other txt files maybe there is more txt file in your directory so the duplicat name will be found
to avoid that put more letters in the log.log file


Top
   
PostPosted: 06 Apr 2012 08:20 
Offline

Joined: 05 Sep 2011 09:15
Posts: 114
Don't know why.
Under the directory, there are only four files:

1.txt
2.txt
log.log
t.bat (where the script is stored)

And I put

a
b
c
d

in log.log

More errors are produced but the renaming still works.


Top
   
PostPosted: 06 Apr 2012 08:25 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
what os are u use 7 or xp
i use xp now but i will test on 7 and tell u
i tried again on xp i have the same errors now
i don't know what happened but i fix some thing and i get one error now i fix it and post it here just few minutes


Top
   
PostPosted: 06 Apr 2012 08:49 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
i know now why is the error
when i used this:
Code: Select all
@ECHO OFF
setlocal
for /f %%a in ('dir *.txt /a:-d /o:n /b') do (
echo %%a
for /f %%b in (log.log) do echo %%b
)
pause

it give this:
Code: Select all
1.txt
a
b
2.txt
a
b

the batch rename 1.txt to 1.txt then try to rename the 1.txt to b.txt but 1.txt is not there it is already renamed so it give the error


Top
   
PostPosted: 06 Apr 2012 08:55 
Offline
Expert

Joined: 17 Oct 2009 08:30
Posts: 378
Location: Russia
tinfanide wrote:
It does not work, with the cmd window popping out for half a second and disappearing.
remove "echo" in
Code: Select all
   echo ren %1 "%%b.txt"


Top
   
PostPosted: 06 Apr 2012 09:05 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
!k it work with me here
with no errors at all :)


Top
   
PostPosted: 06 Apr 2012 09:09 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
This sort of technique was shown recently.

It will work if the number of lines in log.log is equal to the number of *.txt files.
The renaming is in alphabetical order as the DIR command returns the filenames.

remove the echo command to make it work.

Code: Select all
@echo off
setlocal enabledelayedexpansion

< "log.log" (for /f "delims=" %%a in ('dir *.txt /b /o:n') do (
set var=
set /p "var="
if defined var echo ren  "%%a" "!var!%%~xa"
)
)
pause


Top
   
PostPosted: 06 Apr 2012 10:01 
Offline

Joined: 05 Sep 2011 09:15
Posts: 114
!k wrote:
Code: Select all
@echo off &setlocal enableextensions

set /a cnt=0
for /f "delims=" %%a in ('dir /b /on *.txt') do call :ren "%%a"
goto :eof

:ren
for /f "tokens=1" %%b in ('more +%cnt% log.log') do (
   echo ren %1 "%%b.txt"
   set /a cnt+=1 &goto :eof
)


I hope I won't be too fussy if I ask further about the MORE command.
I didn't know the use of MORE and tried testing it:
Code: Select all
FOR /F "tokens=1" %%A IN ('MORE +1 log.log') DO ECHO %%A

It works like skip in FOR. The bit I don't understand why in your codes it only shows the text in that line?
Quote:
b
c
d


Top
   
PostPosted: 06 Apr 2012 10:02 
Offline

Joined: 05 Sep 2011 09:15
Posts: 114
abc0502 wrote:
i know now why is the error
when i used this:
Code: Select all
@ECHO OFF
setlocal
for /f %%a in ('dir *.txt /a:-d /o:n /b') do (
echo %%a
for /f %%b in (log.log) do echo %%b
)
pause

it give this:
Code: Select all
1.txt
a
b
2.txt
a
b

the batch rename 1.txt to 1.txt then try to rename the 1.txt to b.txt but 1.txt is not there it is already renamed so it give the error


Yes, exactly.
Me using Win 7, but I presume there should be no difference.


Top
   
PostPosted: 06 Apr 2012 10:39 
Offline

Joined: 05 Sep 2011 09:15
Posts: 114
foxidrive wrote:
This sort of technique was shown recently.

It will work if the number of lines in log.log is equal to the number of *.txt files.
The renaming is in alphabetical order as the DIR command returns the filenames.

remove the echo command to make it work.

Code: Select all
@echo off
setlocal enabledelayedexpansion

< "log.log" (for /f "delims=" %%a in ('dir *.txt /b /o:n') do (
set var=
set /p "var="
if defined var echo ren  "%%a" "!var!%%~xa"
)
)
pause


First, what's the use of the redirect output operator?
Code: Select all
< "log.log"

And a lot more I cannot make sense of...

The FOR loop is inside a ()
Code: Select all
(FOR ...
)


Why set var = nothing?
Code: Select all
SET var=
SET /P "var="


SET var=
to unset the variable %var%?
Is it to reset the variable with the delayed expansion?

SET /P "var="
It waits for users to enter a value for %var%.
If the user does not enter anything, the variable is still empty. Then what's the use of it in this case?

If SET var=nothing, why var is defined?


Sorry if I've asked too much.
I couldn't figure out most of them online on my own.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 19 posts ]  Go to page 1 2 Next

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited