# Solved: Batch File to Rename Files - for /f Loop Not Picking Up Variables



## voidstate (Feb 19, 2008)

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


```
:: 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 (Apr 7, 2007)

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


```
@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 (Feb 19, 2008)

Thank you, I'd just (finally!) worked out that I needed to use %%... phew.


----------

