Solved

renaming files with batch commands

Posted on 2003-11-22
5
2,918 Views
Last Modified: 2008-03-10
In PCMagazine I found a cool way to rename all files with a *.jpg extention to the filestamp of that file, which is useful for organizing pictures from digital cameras.  

So world_at_night.jpg becomes 20030313_0938.jpg where 20030313 is the day and 0938 is the hour and min of the file stamp. It also check so make sure the file doesn't already exist, and if it does give the message "Cannot rename <filename>.jpg"
---------------------------
@ECHO OFF
FOR %%V IN (*.jpg) DO FOR /F "tokens=1-5 delims=/: " %%J  IN ("%%~tV") DO IF EXIST %%L%%J%%K_%%M%%N%%~xV (ECHO Cannot rename %%V) ELSE (RENAME "%%V" %%L%%J%%K_%%M%%N%%~xV)
---------------------------

Is there a way to run two commands at once in the last set of parenthesis, so that I can both see the command and have it execute? For example, I'd like something like:
RENAME "%%V" %%L%%J%%K_%%M%%N%%~xV AND ECHO RENAME "%%V" %%L%%J%%K_%%M%%N%%~xV
(note the "AND" that I added, but doesn't work.)

I picked a pretty complicated example for pretty simple question, but I hope someone can help.

0
Comment
Question by:josephfluckiger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 21

Expert Comment

by:gemarti
ID: 9803078
Why don't you remove @ECHO OFF from your current batch file?

Or am I missing something?
0
 
LVL 21

Expert Comment

by:gemarti
ID: 9803083
hmmm...scratch that. :)

Isn't there a @ECHO ON?
0
 
LVL 5

Accepted Solution

by:
arjanh earned 500 total points
ID: 9803112
I think '&' should do the trick. It at least works correctly for me if I do
dir & cd .. & dir

So:
@ECHO OFF
FOR %%V IN (*.jpg) DO FOR /F "tokens=1-5 delims=/: " %%J  IN ("%%~tV") DO IF EXIST %%L%%J%%K_%%M%%N%%~xV (ECHO Cannot rename %%V) ELSE (ECHO RENAME "%%V" %%L%%J%%K_%%M%%N%%~xV & RENAME "%%V" %%L%%J%%K_%%M%%N%%~xV)
0
 
LVL 1

Author Comment

by:josephfluckiger
ID: 9803233
arjanh, beautiful, thanks!

gemarti, the output when removing @ECHO OFF is pretty ugly, but the output from arhanh's is very pretty:
----------------------------------------------------------
RENAME "2.jpg" 20030411_0815.jpg
RENAME "spence_on_couch2.jpg" 20030411_0818.jpg
RENAME "world_at_night.jpg" 20030313_0938.jpg
----------------------------------------------------------

The output from @ECHO ON:
----------------------------------------------------------
C:\joeDev\batchFileCommands>FOR %V IN (*.jpg) DO FOR /F "tokens=1-5 delims=/: "
%J IN ("%~tV") DO IF EXIST %L%J%K_%M%N%~xV (ECHO Cannot rename %V )  ELSE (RENAM
E "%V" %L%J%K_%M%N%~xV )

C:\joeDev\batchFileCommands>FOR /F "tokens=1-5 delims=/: " %J IN ("04/11/2003 08
:15 PM") DO IF EXIST %L%J%K_%M%N.jpg (ECHO Cannot rename 2.jpg )  ELSE (RENAME "
2.jpg" %L%J%K_%M%N.jpg )

C:\joeDev\batchFileCommands>IF EXIST 20030411_0815.jpg (ECHO Cannot rename 2.jpg
 )  ELSE (RENAME "2.jpg" 20030411_0815.jpg )

C:\joeDev\batchFileCommands>FOR /F "tokens=1-5 delims=/: " %J IN ("04/11/2003 08
:18 PM") DO IF EXIST %L%J%K_%M%N.jpg (ECHO Cannot rename spence_on_couch2.jpg )
 ELSE (RENAME "spence_on_couch2.jpg" %L%J%K_%M%N.jpg )

C:\joeDev\batchFileCommands>IF EXIST 20030411_0818.jpg (ECHO Cannot rename spenc
e_on_couch2.jpg )  ELSE (RENAME "spence_on_couch2.jpg" 20030411_0818.jpg )

C:\joeDev\batchFileCommands>FOR /F "tokens=1-5 delims=/: " %J IN ("03/13/2003 09
:38 AM") DO IF EXIST %L%J%K_%M%N.jpg (ECHO Cannot rename world_at_night.jpg )  E
LSE (RENAME "world_at_night.jpg" %L%J%K_%M%N.jpg )

C:\joeDev\batchFileCommands>IF EXIST 20030313_0938.jpg (ECHO Cannot rename world
_at_night.jpg )  ELSE (RENAME "world_at_night.jpg" 20030313_0938.jpg )
----------------------------------------------------------

0
 
LVL 21

Expert Comment

by:gemarti
ID: 9803476
No problem. I liked arjanh answer...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If your system is showing symptoms of browser hijacks or 'google search redirects' check out my other article (http://rdsrc.us/u3GP7A) first and run the tool TDSSKiller (http://rdsrc.us/GDBBs4) to get rid of the infection. Once done, and if the …
It is only natural that we all want our PCs to be in good working order, improved system performance, so that is exactly how programs are advertised to entice. They say things like:            •      PC crashes? Get registry cleaner to repair it!    …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question