Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2965
  • Last Modified:

renaming files with batch commands

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
josephfluckiger
Asked:
josephfluckiger
  • 3
1 Solution
 
gemartiCommented:
Why don't you remove @ECHO OFF from your current batch file?

Or am I missing something?
0
 
gemartiCommented:
hmmm...scratch that. :)

Isn't there a @ECHO ON?
0
 
arjanhCommented:
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
 
josephfluckigerAuthor Commented:
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
 
gemartiCommented:
No problem. I liked arjanh answer...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now