Saved Bookmarks
| 1. |
Solve : Create a bartch file that concatenates filename with directory name? |
|
Answer» Hi all, I'm trying to write a batchfile (called "Convert" in the example below) that concatenates from a given directory, all file names in that directory with the directory names of teh subdirectories. E.g. running "Convert" in the RootDir of this directory structure : RootDir > Test1Dir > MyFile.txt would result in a filename "RootDirTest1DirMyFile.txt" The batchfile would need to convert all files in RootDir. Any help really appriciated.Do the files get new names but stay in their original locations, or do they get moved? Quote from: Steven on January 23, 2010, 07:54:03 AM RootDir > Test1Dir > MyFile.txt C:\>type filedirname.bat Code: [Select]@echo off cd \ cd Test1Dir dir copy MyFile.txt xMyFile.txt ren MyFile.txt RootDirTest1DirMyFile.txt dir Output: C:\>filedirname.bat Volume in drive C has no label. Volume Serial Number is F4A3-D6B3 Directory of C:\test1dir 01/23/2010 11:23 AM . 01/23/2010 11:23 AM .. 01/23/2010 11:23 AM 8 MyFile.txt 1 File(s) 8 bytes 2 Dir(s) 303,144,689,664 bytes free 1 file(s) copied. Volume in drive C has no label. Volume Serial Number is F4A3-D6B3 Directory of C:\test1dir 01/23/2010 11:24 AM . 01/23/2010 11:24 AM .. 01/23/2010 11:23 AM 8 RootDirTest1DirMyFile.txt 01/23/2010 11:23 AM 8 xMyFile.txt 2 File(s) 16 bytes 2 Dir(s) 303,144,689,664 bytes free C:\test1dir>Thanks all. The difficult thing to get solved is the automatic scan of the entire directory structure. The batch file would need to start from the root directory, and from that point, scan all subdirectories (multiple levels), search all files in those subdirs, and convert the filenames. Quote from: Steven on January 24, 2010, 02:27:52 AM Thanks all. The difficult thing to get solved is the automatic scan of the entire directory structure. The batch file would need to start from the root directory, and from that point, scan all subdirectories (multiple levels), search all files in those subdirs, and convert the filenames. There are ways of doing this. Can I please ask a clarifying question? It seems to me that what you want to do is this: (Where D represents the drive letter) - start in some SUBFOLDER, an arbitrary DISTANCE down the folder tree, say: D:\Some Folder\Some Other Folder maybe\Topdir and look in all the subfolders under that folder, renaming all the files found as follows: take the path from Topdir and the filename, and rename each file with a string which is its path and filename with the slashes removed thus Topdir\animals\cats\lion.txt becomes Topdiranimalscatslion.txt Each file keeping its existing extension and remaining in the folder in which it is found. The previous part of the complete path (e.g. Some Folder\Some Other Folder maybe\) not to be part of the new filename? That is what I hope you can clarify. Is that correct? I have written and tested a batch script that seems to do what the OP requires ... if he/she clarifies as I asked, I will probably post it here, so let's see if they come back... Wouldn't this work? @echo off setlocal enabledelayedexpansion set path2top=D:\somedir\otherdir\ For /f "delims=" %%a in ('dir /b /s %path2top%') do ( set filepath=%%a set filepath=!filepath:\=! REM ren %%a !filepath! echo Rename %%a to !filepath! ) Untested. If it says it has "Renamed" the desired files to the desired name, remove the word REN and RUN the script again. Quote from: Helpmeh on January 24, 2010, 09:57:44 AM Wouldn't this work? I thought the OP WANTED the relative path from some top folder which was not necessarily the root folder of a drive, therefore the full dir /b /s path would have to be processed somehow, and an /a-d might be handy... This is what I hacked out... Code: [Select]@echo off setlocal enabledelayedexpansion REM set top folder set TopFolder="D:\SubRename\Animals" REM make Topdir current directory cd /d "%TopFolder%" REM Get top dir's path for /f "delims=" %%P in ("%TopFolder%") do set TopDirPath=%%~dpP REM Set char to replace Set ReplacedChar=\ REM Set replacement character(s) REM Leave blank for none set ReplaceWith= REM Or e.g. replace with dash REM set ReplaceWith=- REM Or e.g. replace with -$$$- REM set ReplaceWith=-$$$- REM Or e.g. replace with a space This is how you would do a space set "ReplaceWith= " REM For each file in and under top dir... REM Change the filespec from *.* if desired to filter files REM standard wildcard rules apply REM e.g. *.txt to only operate upon .txt files set filespec=*.* for /f "delims=" %%F in ('dir /s /b /a-d %filespec%') do ( REM Find the file's full path, name and extension set FullPath=%%~dpnxF REM Remove part of path above TopDir Set SubPath=!FullPath:%TopDirPath%=! REM Replace char(s) in path and name set NewName=!SubPath:%ReplacedChar%=%ReplaceWith%! REM Inform user of old file path+name and new name echo File: "!FullPath!" echo New name: "!NewName!" REM Rename command syntax REM Rename [Drive:][\Path\]OldName NewName REM Remove REM from following line after testing REM Carry out rename operation REM Remove REM to activate the next line REM Rename "!FullPath!" "!NewName!" echo. ) echo Finished pause Before... Code: [Select]D:\SubRename\Animals>dir /s /b /a-d D:\SubRename\Animals\Test.txt D:\SubRename\Animals\Birds And Fish\Eagle\Test.txt D:\SubRename\Animals\Birds And Fish\Salmon\Test.txt D:\SubRename\Animals\Cats\Lion\Test.txt D:\SubRename\Animals\Cats\Panther\Test.txt D:\SubRename\Animals\Dogs\Bichon Frise\Test.txt D:\SubRename\Animals\Dogs\Poodle\Test.txt D:\SubRename\Animals\Horses\Foal\Test.txt D:\SubRename\Animals\Horses\Mare\Test.txt D:\SubRename\Animals\Horses\Stallion\Test.txt Run the batch... Code: [Select]File: "D:\SubRename\Animals\Test.txt" New name: "AnimalsTest.txt" File: "D:\SubRename\Animals\Birds And Fish\Eagle\Test.txt" New name: "AnimalsBirds And FishEagleTest.txt" File: "D:\SubRename\Animals\Birds And Fish\Salmon\Test.txt" New name: "AnimalsBirds And FishSalmonTest.txt" File: "D:\SubRename\Animals\Cats\Lion\Test.txt" New name: "AnimalsCatsLionTest.txt" File: "D:\SubRename\Animals\Cats\Panther\Test.txt" New name: "AnimalsCatsPantherTest.txt" File: "D:\SubRename\Animals\Dogs\Bichon Frise\Test.txt" New name: "AnimalsDogsBichon FriseTest.txt" File: "D:\SubRename\Animals\Dogs\Poodle\Test.txt" New name: "AnimalsDogsPoodleTest.txt" File: "D:\SubRename\Animals\Horses\Foal\Test.txt" New name: "AnimalsHorsesFoalTest.txt" File: "D:\SubRename\Animals\Horses\Mare\Test.txt" New name: "AnimalsHorsesMareTest.txt" File: "D:\SubRename\Animals\Horses\Stallion\Test.txt" New name: "AnimalsHorsesStallionTest.txt" Finished Press any key to continue . . . After... Code: [Select]D:\SubRename\Animals>dir /s /b /a-d D:\SubRename\Animals\AnimalsTest.txt D:\SubRename\Animals\Birds And Fish\Eagle\AnimalsBirds And FishEagleTest.txt D:\SubRename\Animals\Birds And Fish\Salmon\AnimalsBirds And FishSalmonTest.txt D:\SubRename\Animals\Cats\Lion\AnimalsCatsLionTest.txt D:\SubRename\Animals\Cats\Panther\AnimalsCatsPantherTest.txt D:\SubRename\Animals\Dogs\Bichon Frise\AnimalsDogsBichon FriseTest.txt D:\SubRename\Animals\Dogs\Poodle\AnimalsDogsPoodleTest.txt D:\SubRename\Animals\Horses\Foal\AnimalsHorsesFoalTest.txt D:\SubRename\Animals\Horses\Mare\AnimalsHorsesMareTest.txt D:\SubRename\Animals\Horses\Stallion\AnimalsHorsesStallionTest.txtQuote from: Salmon Trout on January 24, 2010, 11:05:06 AM Hack Mr. Trout you are very thorough and usually accurate. ( I have not tested your latest creation. ) What is the practical application for the batch that Steven, the Original Poster has requested?Thank you Mr Richardson. |
|