|
| Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free! |
XP batch script to rename files
|
Original Message
|
Name: luminous
Date: August 4, 2006 at 02:55:45 Pacific
Subject: XP batch script to rename filesOS: XP Pro SPCPU/Ram: Athlon XP 2500+ 2GbModel/Manufacturer: 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 :(
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Shr0Om
Date: August 4, 2006 at 03:46:51 Pacific
|
Reply: (edit)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------
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: Mechanix2Go
Date: August 4, 2006 at 04:03:08 Pacific
|
Reply: (edit)"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
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: Mechanix2Go
Date: August 4, 2006 at 04:11:10 Pacific
|
Reply: (edit)I guess I need to type faster; or think faster. LOL ===================================== If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: luminous
Date: August 4, 2006 at 04:41:52 Pacific
|
Reply: (edit)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 :)
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: Shr0Om
Date: August 4, 2006 at 05:13:17 Pacific
|
Reply: (edit)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.
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: luminous
Date: August 4, 2006 at 07:34:33 Pacific
|
Reply: (edit)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 :-))
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: Shr0Om
Date: August 4, 2006 at 11:01:36 Pacific
|
Reply: (edit)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?
Report Offensive Follow Up For Removal
|

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