1.

Solve : "For" and "If" command doubt?

Answer»

Hi,

This is my first post. I'm here for asking a question about a doubt I can't solve. I've already read most of on line help available, but I cannot do my batch work correctly.

This batch should look for every *inside.jpg files under my Music folder and if the file "Front Cover Inside.jpg" doesn't exist it should rename *inside.jpg file to "Front Cover Inside.jpg".
But if "Front Cover Inside.jpg" exist and there's some *inside file, it should log "Similar file to '*inside.jpg' already exists"

My batch:
Code: [Select]for /R %%x in (*inside.jpg) do if not exist "Front Cover Inside.jpg" (
ren "%%x" "Front Cover Inside.jpg"
echo "%%x" renamed to "Front Cover Inside.jpg" >> Log.txt
) ELSE (
echo Similar file to "%%x" already exists >> Log.txt
)

PS: Log.txt file already exists. I'm working on a Windows XP Professional SP3 machine.I just ran your script twice, and the first time, it renamed "0D0A inside.jpg" to "Front Cover Inside.jpg", and added this line to log.txt

"S:\Test\Batch\After 03-07-2010\for-if\0D0A inside.jpg" renamed to "Front Cover Inside.jpg" 

The second time it did nothing and wrote this line

Similar file to "S:\Test\Batch\After 03-07-2010\for-if\Front Cover Inside.jpg" already exists

Isn't this what you want?

Quote from: Salmon Trout on September 08, 2010, 02:13:06 PM

I just ran your script twice, and the first time, it renamed "0D0A inside.jpg" to "Front Cover Inside.jpg", and added this line to log.txt

"S:\Test\Batch\After 03-07-2010\for-if\0D0A inside.jpg" renamed to "Front Cover Inside.jpg" 

The second time it did nothing and wrote this line

Similar file to "S:\Test\Batch\After 03-07-2010\for-if\Front Cover Inside.jpg" already exists

Isn't this what you want?

My problem is when "Front Cover Inside.jpg" coexists with some file like "0D0A inside.jpg".
What I want to be logged is:
Code: [Select]"Similar file to "S:\Test\Batch\After 03-07-2010\for-if\0D0A inside.jpg" already exists" which in this case is "Front Cover Inside.jpg").

Then, after reading the log, I will manually check which file is "0D0A inside.jpg" and if it's quality is better than "Front Cover Inside.jpg" it will delete this one.
I need this because I have thousands of album art files I don't want to check then one by one...

TIAFinally I think I've solved my problem. My final batch looks like this:

Quote
echo off
for /R %%x in (*-inside.jpg) do if not exist "Front Cover Inside.jpg" (
ren "%%x" "Front Cover Inside.jpg"
if errorlevel 1 (
echo Similar file to "%%x" already exists >> Log.txt
goto end
)
echo "%%x" renamed to "Front Cover Inside.jpg" >> Log.txt
)

:end

The trick is using errorlevel. When trying to rename the file, if there's already a file with the same name, MS-DOS will return a error message. I've just use this to sove my problem.
I've tried different scenarios and it worked the way I want it. So, I'm proud of myself, considering how newbie I'm in MS-DOS command line... Quote from: vitorb on September 11, 2010, 07:40:29 AM
Finally I think I've solved my problem. My final batch looks like this:

The trick is using errorlevel. When trying to rename the file, if there's already a file with the same name, MS-DOS will return a error message. I've just use this to sove my problem.
I've tried different scenarios and it worked the way I want it. So, I'm proud of myself, considering how newbie I'm in MS-DOS command line...


This is going to start driving me completely INSANE. Quote from: BC_Programmer on September 11, 2010, 08:33:52 AM

This is going to start driving me completely insane.

  Sorry... You're completely right! I know that MS-DOS it's a operating system and what I'm using is just a command line interpreter.
Ten YEARS ago I USED MS-DOS for a few months, but I never developed any SKILLS on it's usage...And the tweaking goes on...

My previous batch had one problem. This code:
Code: [Select]for /R %%x in (*inside.jpg) do if not exist "Front Cover Inside.jpg"doesn't seems to work correctly because after the batch finish I saw that every "Front Cover Inside.jpg" files in various directories had been renamed again to it's current name.

Code: [Select]ren "%%x" "Front Cover Inside.jpg"Whenever %x is "Front Cover Inside.jpg" it will be:
Code: [Select]ren "Front Cover Inside.jpg" "Front Cover Inside.jpg" which is useless...

So I've change it this way:
Code: [Select]for /R %%x in (*inside.jpg) do if "%%~nxx"=="Front Cover Inside.jpg" (
echo "Front Cover Inside.jpg" already exists
) else (
ren "%%x" "Front Cover Inside.jpg"
if errorlevel 1 (
echo Similar file to "%%x" already exists >> Log.txt
) else (
echo "%%x" renomed to "Front Cover Inside.jpg" >> Log.txt
)
)


Discussion

No Comment Found