Saved Bookmarks
| 1. |
Solve : Rename/replace? |
|
Answer» Quote from: Geek-9pm on January 10, 2010, 08:49:37 PM Doing DIR /? gives the list of options. The result is still the same. I get the same error message. Sorry that did not help. I am just shooting in the dark. That is why I am the Geek after dark. You need the /s to get all sub directories. -Right? Well, just move the DIR thing out of the for loop. I have been following this third, but get lost in the stuff. Do you really need recursion? Can you just use iteration? Maybe I missed something. Could you just do DIR *.JPG /B/S >big-list.txt Then have the for loop precess each line in big-list.txt That way the for loop does not have to do recursion. I guess. I am not keen on using the recursion at all. I am just looking for a way to rename my files in many directories over many backup disks. In the process of renaming I need to remove/replace some strung from the file name. I don't need a full blown rbust solution. Just a way to reduce the manula work. The script works for a single directory and it renames the files correctly. Here is an idea a threw EARLIER today. I am reposting for easy reference. ==================================================== Since the basic code works for a directory I think the following idea might work. but I don't have the knowledge base to do the programming in shell. 1. Parameterize the basic code, ie. pass the directory path to this script. 2. Make another script to scan through the subdirectories RECURSIVELY and when a it finds a directory call the first batch file (from POINT #1) with the directory as parameter. This code seems to rename files recursively. May be the gurus can tweak it to make it work with the above routine. For /r %%x in (*.jpg) do ren "%%x" *.jpeg Notice the /r switch in the FOR clause for scanning the directories recursively. =============================================================== Thanks, myshutterclicks Quote from: Geek-9pm on January 10, 2010, 09:13:08 PM Sorry that did not help.Nobody gets any points for astuteness, perspicacity or just plain gumption here! None of you noticed that the OP posted the same number of error messages as there were files. Therefore, the problem was occurring in the loop, not in the FOR line which set it up. Also, nobody bothered (come on guys!) to type rename /? at the prompt and read the help, whereupon it would have become clear where the problem lies. Namely in the incorrect syntax being used with the ren / rename command. The syntax is Code: [Select]RENAME [drive:][path]filename1 filename2 The code was doing this Code: [Select]RENAME [drive:][path]filename1 [drive:][path]filename2 To give a concrete example This is wrong Code: [Select]Rename "C:\My Documents\My Pictures\My Meals\Cheeseburger105.jpg" "C:\My Documents\My Pictures\My Meals\Hamburger105.jpg" This is right Code: [Select]Rename "C:\My Documents\My Pictures\My Meals\Cheeseburger105.jpg" "Hamburger105.jpg" When you have to specify the full path of a file to rename, e.g. because it is not in the current directory, supply it once, before the present (old) filename and then supply just the new name (no path). It actually says this in the rename /? help Code: [Select]Note that you cannot specify a new drive or path for your destination file. Dir /s /b always produces a full path for each file, so you need to get at the drive, path, name and extension information using the FOR variable modifiers which you can read about in the FOR /? documentation Here is some commented code which (I hope) will make all this clear... Code: [Select] @echo off REM You need this because you are setting variables (and reading them) REM inside a parenthetical structure (a loop in this case) setlocal enabledelayedexpansion REM The /f switch tells FOR to treat the output of the command REM as a series of text lines to be processed REM I have changed the variable to upper case to make REM the variable modification clearer for /f "delims=" %%A in ('dir /s /b *_MG_*.JPG') do ( REM If %%A is a file path and name (or just a filename) REM %%~dpA is its drive and path... set filepath=%%~dpA REM ... and %%~nxA is its name and extension REM Thus we have removed the drive and path... set filenameold=%%~nxA REM Modify just the name without path REM to get the new name set filenamenew=!filenameold:_MG_=! REM Give the Rename command the full path & old name REM for the file to be renamed, and just the new name rename "!filepath!!filenameold!" "!filenamenew!" ) Since we have now got up to 7 pages, I feel justified in having a moan about how standards are slipping here on CH. Somebody posts a question, somebody else (who shall be nameless) posts a partial solution, the OP tries it out, it doesn't work, and then a whole bunch of people just post guesses. Nobody read the command documentation; nobody tried specimen code. Quote from: Salmon Trout on January 11, 2010, 01:26:10 AM
Hi Salmon, I just tried your code and it works like a charm. I cannot thank you enough for this. It's definitely going to save a lot of time for me. Your attention to the details and understanding of the problem are great and I am sure you enjoy a healthy dose of success and satisfaction in your daily LIFE. I wish you great success as well. I am a photographer and I know a bit about it. Do not hesitate to get in touch with me if you need any tips about photography. Great forums. Keep up the good work. Lifespan of one's residue is mostly determined by what one does for common good throughout one's lifetime. Best regards, http://www.kankooz.com |
|