Must rename 1024 files!

September 10, 2010 at 07:45:47
Specs: Windows Vista
Hi, i have 1024 files with garbage names in a folder and I need to rename them with "trial_x" where x goes from 1 to 1024. Now I tried using the code from an older thread

::------CODE-----
@echo off
md c:\lih\tmp
dir /b "c:\LIH\portraits" >>filelist.txt
for /f %%i in (filelist.txt) do (
copy "c:\LIH\folder2\logo.png" "c:\LIH\tmp\%%i"
)
del filelist.txt
::------CODE-----

but it said that "%%i was unexpected at this time". I'm not sure how to fix this.

If anyone could help that would be greatly appreciated!


See More: Must rename 1024 files!

Report •


#1
September 10, 2010 at 13:12:49
Try this:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET Number=1

FOR /f "tokens=*" %%a IN ('DIR /b "P:\ath\Of\Files"') DO (
	RENAME "%%a" "trial_!Number!"
	SET /a Number +=1
)

Can I put a question here?


Report •

#2
September 12, 2010 at 11:52:03
Hi, I have a few questions about what this means.

Firstly i'm confused as to what 'DIR /b "P:\ath\Of\Files"' does... When I write that it says: 'DIR' is not recognized as an internal or external command, operable program or batch file. What does this line do exactly? If anyone could help me out with this it would be really appreciated! Thanks so much!


Report •

#3
September 12, 2010 at 12:11:24
DIR should be recognized... It included in every for of windows I believe.


What it does is list all the files in that directory. Try a reboot, occasionally when you install certain programs it screws with your path variable a little. If it's still not working check your system32 folder and look for dir.exe.

"P:\ath\of\files" needs to be changed though to the actual location of the files that need sorting.

EDIT: Do what I said above, but recopy the code I pasted earlier. I made a mistake, that is fixed now.

Can I put a question here?


Report •

Related Solutions

#4
September 13, 2010 at 08:05:54
Hi, here is what i typed in (the files are in C:\LIH) and here is what command prompt gives back to me. (The files do not get changed)


Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.

C:\Users\Karl>@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET Number=1
FOR /f "tokens'*" %%a IN ('DIR /b "C:\LIH"') DO (
%%a was unexpected at this time.
RENAME "%%a" "trial_!Number!"
The system cannot find the file specified.
SET /a Number +=1
2)


Thank you so much for continuing to help me!


Report •

#5
September 13, 2010 at 12:27:48
Using the prompt is different.you need to change "%%a" to "%a", in both spots. Also, "tokens'*" needs to be "tokens=*"

if you enter it into a text file, and save it as a .bat file, it will work as i posted before.

Good Luck

Can I put a question here?


Report •

#6
September 13, 2010 at 14:57:50
Hi, I'm almost there I can feel it!

This is what I get now:

Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.

C:\Users\Karl>@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET Number=1
FOR /f "tokens=*" %a IN ('DIR /b "C:\LIH"') DO (
More? RENAME "%a" "trial_!Number!"
More? SET /a Number += 1
More? )
The system cannot find the file specified.
2The system cannot find the file specified.
3

The files I want renamed are certainly in C:\LIH, it seems it can't find all the files there... I messed around and I was able to rename the folder but I couldn't get renaming the files to work!

Thank you so much!


Report •

#7
September 13, 2010 at 18:16:41
Hmm, wierd... Try removing the file extensions.

C:\Users\Karl>@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET Number=1
FOR /f "tokens'*" %%a IN ('DIR /b "C:\LIH"') DO (.
RENAME "%~na" "trial_!Number!"
SET /a Number +=1
)

Still if not... Try adding *.* to the end of the file path, change the second * to a file extension if they are all they same type of file.

Can I put a question here?


Report •

#8
September 14, 2010 at 10:26:24
Thank you for your continuing help.. Here is what I currently have in my .bat file

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET Number=1
FOR /f "tokens'*" %%a IN ('DIR /b "C:\LIH\*.bmp"') DO (
RENAME "%%a" "trial_!Number!"
SET /a Number +=1
)

I tried switching the "%%a" to "%~na" at one point and it didn't make a difference... Also I assume the period after your opening parentheses was a typo (but I tried that once and it didn't work too).

It still doesn't change the files in LIH, could this be anything to do with how I use windows vista or something? Thanks again for your help...


Report •

#9
September 14, 2010 at 12:21:50
Try respecifying the path to the variable:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET Number=1

FOR /f "tokens=*" %%a IN ('DIR /b "C:\LIH\*.bmp"') DO (
RENAME "C:\LIH\%%a" "trial_!Number!"
SET /a Number +=1
)

Can I put a question here?


Report •

#10
September 19, 2010 at 12:24:28
Thank you so much it works! You're a genius! I really appreciated the continued support for everything, you're very helpful!


Report •

#11
September 19, 2010 at 16:31:57
Not a problem! Glad I was able to figure it out for you!

Can I put a question here?


Report •


Ask Question