Solved

renaming files with batch commands

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

cPanel is a Unix based web hosting control panel that provides a graphical interface and automation tools designed to simplify the process of hosting a web site. cPanel utilizes a 3 tier structure that provides functionality for administrators, rese…
Issue: Unstable cursor in Windows XP and Windows runs extremely slow in that any click will bring up the Hour glass (sometimes for several seconds before giving you what you want) . Troubleshooting Process and the FINAL FIX: This issue see…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

21 Experts available now in Live!

Get 1:1 Help Now