There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot browser bsod computer crash css dell desktop driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware help please hijackthis hjt hjt log install internet internet explorer itunes javascript keyboard laptop log malware monitor network networking openoffice outlook outlook 2003 outlook express password popups problem router seo slow sound sp3 spyware startup trojan usb video virtumonde virus vista vundo windows windows xp winxp wireless youtube
DOS/PDA/Other
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Operating Systems > DOS/PDA/Other >
Solved: Batch File to Rename Files - for /f Loop Not Picking Up Variables


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
voidstate's Avatar
Junior Member with 12 posts.
 
Join Date: Feb 2008
10-Apr-2008, 07:27 AM #1
Solved: Batch File to Rename Files - for /f Loop Not Picking Up Variables
Hi

I'm trying to write a DOS batch file for the first time in.. well, too many years to mention. My problem is that I have set two variables - the date and time formatted as I want them - and then want to loop through a directory renaming certain files to include these variables

Code:
:: Get date & time
for /f "tokens=1-3 delims=/- " %%a in ('date /t') do set XDate=%%a%%b%%c
for /f "tokens=1-2 delims=: " %%a in ('time /t') do set XTime=%%a%%b

:: Rename files
for /f "tokens=1-2 delims=. " %a in ('dir *.jpg /b') do rename c:\batch_test\source\%a%.%b% %a%%XDate%%XTime%-failed.%b%
So, the following directory:

c:\batch_test\source\test.jpg
c:\batch_test\source\anothertest.jpg

Should end up as:

c:\batch_test\source\test100420081214-failed.jpg
c:\batch_test\source\anothertest100420081214-failed.jpg

Any idea what I'm doing wrong?
devil_himself's Avatar
Distinguished Member with 4,794 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
10-Apr-2008, 08:57 AM #2
We Use %var% To Access A Variable
But In Case Of A FOR Loop We Need To Reference The Variable As %%I

Delims: used to break the line up into chunks that you can reference with variables %%i %%j %%k ...
Tokens: used so you can tell the FOR command how many chunks you want to break it up into and how many will be handled by variables

Code:
@echo off
:: Get date & time
for /f "tokens=1-3 delims=/- " %%a in ('date /t') do set XDate=%%a%%b%%c
for /f "tokens=1-2 delims=: " %%a in ('time /t') do set XTime=%%a%%b

:: Rename files
for /f "tokens=1-2 delims=. " %%a in ('dir *.jpg /b') do rename %%a.%%b %%a%XDate%%XTime%-failed.%%b
For More Info

Open Command Prompt

set /?
for /?
voidstate's Avatar
Junior Member with 12 posts.
 
Join Date: Feb 2008
10-Apr-2008, 09:17 AM #3
Thank you, I'd just (finally!) worked out that I needed to use %%... phew.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 05:30 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.