Solved

renaming files with batch commands

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Most of the time we are in fix when all of sudden our systems behave weirdly.  Such problems cost time and effort... so it's best to take some preventive actions so that we can avoid such issues or overcome such problems more easily. Preventive M…
Ok I have been working on this for some time having learned and gained certification in XenDesktop 4 along came version 5 which was released last month. Since then I have been working to deploy XenDesktop 5 in a small environment with only 2 virt…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now