Solved

renaming files with batch commands

Posted on 2003-11-22
5
2,937 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Can you find a fax from a vendor you saved a decade ago in seconds? Have you ever cursed your PC under your breath during an audit because you couldn’t find the requested statement or driver history?  If you answered no to the first question or yes …
We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

630 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