1.

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

Answer» <html><body><p>Hi,<br/><br/>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.<br/><br/>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". <br/>But if "Front Cover Inside.jpg" exist and there's some *inside file, it should log <em>"Similar file to '*inside.jpg' already exists"</em><br/><br/>My batch:<br/> Code: <a>[Select]</a>for /R %%x in (*inside.jpg) do if not exist "Front Cover Inside.jpg" (<br/>ren "%%x" "Front Cover Inside.jpg"<br/>echo "%%x" renamed to "Front Cover Inside.jpg" &gt;&gt; Log.txt<br/>) <a href="https://interviewquestions.tuteehub.com/tag/else-printfy-344787" style="font-weight:bold;" target="_blank" title="Click to know more about ELSE">ELSE</a> (<br/>echo Similar file to "%%x" already exists &gt;&gt; Log.txt<br/>)<br/><br/>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<br/><br/>"S:\Test\Batch\After 03-07-2010\for-if\0D0A inside.jpg" renamed to "Front Cover Inside.jpg"  <br/><br/>The second time it did nothing and wrote this line<br/><br/>Similar file to "S:\Test\Batch\After 03-07-2010\for-if\Front Cover Inside.jpg" already exists <br/><br/>Isn't this what you want?<br/><br/> Quote from: Salmon Trout on September 08, 2010, 02:13:06 PM</p><blockquote>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<br/><br/>"S:\Test\Batch\After 03-07-2010\for-if\0D0A inside.jpg" renamed to "Front Cover Inside.jpg"  <br/><br/>The second time it did nothing and wrote this line<br/><br/>Similar file to "S:\Test\Batch\After 03-07-2010\for-if\Front Cover Inside.jpg" already exists <br/><br/>Isn't this what you want?<br/></blockquote> <br/>My problem is when "Front Cover Inside.jpg" <strong>coexists</strong> with some file like "0D0A inside.jpg".<br/>What I want to be logged is:<br/> Code: <a>[Select]</a>"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").<br/><br/>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.<br/>I need this because I have thousands of album art files I don't want to check then one by one...<br/><br/>TIAFinally I think I've solved my problem. My final batch looks like this:<br/><br/> Quote<blockquote>echo off<br/>for /R %%x in (*-inside.jpg) do if not exist "Front Cover Inside.jpg" (<br/>ren "%%x" "Front Cover Inside.jpg"<br/>if errorlevel 1 (<br/>echo Similar file to "%%x" already exists &gt;&gt; Log.txt<br/>goto end<br/>)<br/>echo "%%x" renamed to "Front Cover Inside.jpg" &gt;&gt; Log.txt<br/>)<br/><br/>:end</blockquote> <br/>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.<br/>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<blockquote>Finally I think I've solved my problem. My final batch looks like this:<br/><br/>The trick is using errorlevel. When trying to rename the file, if there's already a file with the same name, <strong>MS-DOS</strong> will return a error message. I've just use this to sove my problem.<br/>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 <strong>MS-DOS</strong> command line...<br/></blockquote> <br/><br/>This is going to start driving me completely <a href="https://interviewquestions.tuteehub.com/tag/insane-516431" style="font-weight:bold;" target="_blank" title="Click to know more about INSANE">INSANE</a>. Quote from: BC_Programmer on September 11, 2010, 08:33:52 AM<blockquote><br/>This is going to start driving me completely insane.<br/></blockquote> <br/>  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. <br/>Ten <a href="https://interviewquestions.tuteehub.com/tag/years-239545" style="font-weight:bold;" target="_blank" title="Click to know more about YEARS">YEARS</a> ago I <a href="https://interviewquestions.tuteehub.com/tag/used-763273" style="font-weight:bold;" target="_blank" title="Click to know more about USED">USED</a> MS-DOS for a few months, but I never developed any <a href="https://interviewquestions.tuteehub.com/tag/skills-25483" style="font-weight:bold;" target="_blank" title="Click to know more about SKILLS">SKILLS</a> on it's usage...And the tweaking goes on...<br/><br/>My previous batch had one problem. This code:<br/> Code: <a>[Select]</a>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.<br/><br/> Code: <a>[Select]</a>ren "%%x" "Front Cover Inside.jpg"Whenever %x is "Front Cover Inside.jpg" it will be:<br/> Code: <a>[Select]</a>ren "Front Cover Inside.jpg" "Front Cover Inside.jpg" which is useless...<br/><br/>So I've change it this way:<br/> Code: <a>[Select]</a>for /R %%x in (*inside.jpg) do if "%%~nxx"=="Front Cover Inside.jpg" (<br/>echo "Front Cover Inside.jpg" already exists<br/>) else (<br/>ren "%%x" "Front Cover Inside.jpg"<br/>if errorlevel 1 (<br/>echo Similar file to "%%x" already exists &gt;&gt; Log.txt<br/>) else (<br/>echo "%%x" renomed to "Front Cover Inside.jpg" &gt;&gt; Log.txt<br/>)<br/>)</body></html>


Discussion

No Comment Found