Computing.Net > Forums > Programming > XP batch script to rename files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

XP batch script to rename files

Reply to Message Icon

Name: luminous
Date: August 4, 2006 at 02:55:45 Pacific
OS: XP Pro SP
CPU/Ram: Athlon XP 2500+ 2Gb
Product: Athlon
Comment:

Hi

I really need some help to do what I thought would be a simple task. I am a real n00b when it comes to programming, but have now spent many hours trying to get this to work....without success :(

The task:
1) I have a folder that contains about 200 files. Each of these files is a picture, and is named in the following format xxxxxxxxxx_64.png
2) I have another file called logo.png in another folder. All the files in the first folder need to display the logo picture, but retain their original name.

The end result should be mutliple copies of logo.png, but all will be called different names as set out by the files in the first folder.

I know it may sound like a daft task, but it is an IMPORTANT one, and needs to be done a few times a week. Each time having to be done by hand so far, which is most boring.

Any ideas, assistance or known scripts to help?

I have exported all the filenames into a text file, and then tried to read in the names from there, but got nowhere :(



Sponsored Link
Ads by Google

Response Number 1
Name: Shr0Om
Date: August 4, 2006 at 03:46:51 Pacific
Reply:

Lets see if i understood this right...
You then want 200 copies of logo.png, but ever copy should should be named after each file in folder1 (xxxxxxxxxx_64.png)?

Ok, lets assume folder 1 is located at
c:\folder1
and folder2 in c:\folder2

Copy&paste the code in notepad (edit paths as you like).
Save the file as a .bat or .cmd file

NOTE: The code should work, but i didnt test it, so try it on copies of your folders first.

::-----CODE-----
::Make a list of all files(names) in folder1
dir /b "c:\folder1" >>filelist.txt

::Goes trough every line in filelist
:: %%i contains the filename
for /f %%i in (filelist.txt) do
(
copy "c:\folder2\logo.png" "c:\folder2\%%i"
)
del filelist.txt
::------CODE------


0

Response Number 2
Name: Mechanix2Go
Date: August 4, 2006 at 04:03:08 Pacific
Reply:

"I know it may sound like a daft task, but it is an IMPORTANT one"

Sounds like the story of my life.

This should get you on the right track:

::== logo.bat
@echo off
if not exist F1 md F1
if not exist F2 md F2
for /L %%N in (1 1 5) do type nul > F1\%%N.png
> F2\logo.png echo this is logo
for /f %%F in ('dir/b/s F1\*.png') do (
copy /b F2\logo.png %%F > nul
)
:: DONE


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 3
Name: Mechanix2Go
Date: August 4, 2006 at 04:11:10 Pacific
Reply:

I guess I need to type faster; or think faster.

LOL


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 4
Name: Shr0Om
Date: August 4, 2006 at 04:31:47 Pacific
Reply:

M2: I often experience the same problem, hehe.


0

Response Number 5
Name: luminous
Date: August 4, 2006 at 04:41:52 Pacific
Reply:

The first program I kind of understand, but it throws a syntax error:

C:\LIH>dir /b "c:\LIH\portraits" >filelist.txt
The syntax of the command is incorrect.
C:\LIH>for /f %i in (filelist.txt) do

The second program looks scary, but I am trying it now :)


0

Related Posts

See More



Response Number 6
Name: Shr0Om
Date: August 4, 2006 at 05:13:17 Pacific
Reply:

Hmm.. Did you run it from a batch, or did you try to type in the commands manually?
Paste the code in notepad and save the file as a .bat

I rewrote the script to match your folders, and it works for me.

Test case:

I created:
c:\LIH
c:\LIH\portraits
c:\LIH\folder2

/portraits contains pictures
/folder2 contains logo.png
/tmp folder will contain the renamed copies

::------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-----

Anyway, im sure both scripts does the same job. Its just two different aproaches.


0

Response Number 7
Name: luminous
Date: August 4, 2006 at 07:34:33 Pacific
Reply:

works a treat, thanks :-))

Not sure what the difference was to the first program I tried, I must have copied a typo in somehow that I just could not find to correct.

Thanks a million.

I shall now fiddle to my hearts content :-))


0

Response Number 8
Name: Shr0Om
Date: August 4, 2006 at 11:01:36 Pacific
Reply:

It just came to mind while looking at M2's code, that you dont need to write the Dir to a file first.

Here's sum improved code:

::----CODE----
md c:\lih\tmp
for /f "tokens=*" %%i in ('dir/b "c:\lih\portraits"') do (
copy "c:\LIH\folder2\logo.png" "c:\LIH\tmp\%%i"
)
::----CODE----


M2: Ive noticed that after i runned this one, i cannot delete the TMP folder as Windows says its in use (by explorer i noticed). This problems seems to occur while using the FOR command with ('command').
Have you any idea why this occurs?


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: XP batch script to rename files

Batch Script To Rename File www.computing.net/answers/programming/batch-script-to-rename-file/16798.html

batch script to upload to ftp www.computing.net/answers/programming/batch-script-to-upload-to-ftp/16987.html

Batch file to rename without spaces www.computing.net/answers/programming/batch-file-to-rename-without-spaces/11415.html