|
Answer» Hi all! I came across this site searching for some pieces to help me solve a batch puzzle I'm up against. I am vaguely familiar with DOS and programming in general, but lack specific knowledge of dos commands; I don't want to inadvertently modify files that don't need it, and I don't know how to apply a command to all subdirectories.
I am pulling all my files off of my college server, and have discovered that everything I ftp'd off the server now has ";1" appended to the end of the filename, e.g. I now have an .mp3;1 collection, and my pictures are in .jpg;1 format. Of course I only noticed this after they were all mixed with my normal files, so I have to check to see if the name is incorrect before I rename it, so as not to change the ok files to .m's and .j's.
Here is the problem I'm trying to solve: I want to batch rename all files in a folder and it's subdirectories (actually across my entire flash drive, but I don't think that distinction matters?) by reading in the current name, checking to see if the name ends in ;1, and if it does then removing the last 2 characters and using the corrected string for the new name.
i.e.
pic.jpg -> pic.jpg pic2.jpg;1 -> pic2.jpg music\01.mp3;1 -> music\01.mp3 music\okay.mp3 -> music\okay.mp3 misc\1\file.doc;1 -> misc\1\file.doc
It seems like this could be done with just one if statement within a for loop; For (all files in directory and subdirectory) get oldname If (oldname ends in ;1) { newname = truncated oldname (remove last 2 characters only regardless of length) rename oldname newname } Next
Any suggestions on how to best ACCOMPLISH this? Code examples (or a working batch file!) would be much appreciated. Also PLEASE explain briefly exactly what each piece of code would do what.
Thanks for any help!For instance, could I do
for /f %%f in('dir /s /b') do If %%f:-2% equ ";1" ren %%f %%f:~0,-2%
or something similar? Haven't tried running this since I'm don't have the files I want to run it on with me.This might work. By using the semi-colon as the delimiter, you can deconstruct the file name, then reconstruct it for the ren command
Code: [Select]@echo off for /f "tokens=1-2 delims=;" %%i in ('dir /s /b') do ( ren "%%i;%%j" "%%i" )
Thanks for the help; unfortunately I can't get it to run; I get alternating lines of:
"The SYNTAX of the command is incorrect." "The system cannot find the file specified."
I am simply cutting and pasting to a .txt file with Notepad, saving to the top directory I want to affect (test folder), and renaming to a .bat EXTENSION. I can't get any version of the above code to do anything. Is my procedure off or am I creating a syntax error somewhere?
p.s. entering the following - for /f "tokens=1-2 delims=;" %%i in ('dir /s /b') ren "%%i;%%j" "%%i" - in the command prompt returns "%%i unexpected at this time" and nothing happens. Modifying to %i returns "ren unexpected at this time"Quote p.s. entering the following - for /f "tokens=1-2 delims=;" %%i in ('dir /s /b') ren "%%i;%%j" "%%i" - in the command prompt returns "%%i unexpected at this time" and nothing happens. Modifying to %i returns "ren unexpected at this time"
Fixed the ren command. See code below.
Quote"The system cannot find the file specified."
I should have mentioned this message was expected. Files not containing the semi-colon did not need to be renamed. By appending a semi-colon, the file would not be found, throwing an error. Sloppy perhaps, but effective.
Code: [Select]@echo off for /f "tokens=1-2 delims=;" %%i in ('dir /s /b') do ( ren "%%i;%%j" "%%~nxi" 2>nul )
I made to slight change to the ren command to send errors to the nul device. Sorta like throwing dirt under the rug. since OP needs to rename only the files that end in ;1, why not just do a dir *;1 ??Quote from: Sidewinder on October 27, 2008, 04:51:24 PM Code: [Select]@echo off for /f "tokens=1-2 delims=;" %%i in ('dir /s /b') do ( ren "%%i;%%j" "%%~nxi" 2>nul )
Worked perfectly! It was great to have explorer open and watch everything SNAP back to normal.
Thanks a ton.
|