Articles

DOS Batch: rename folder/file to folder_file

December 19, 2010 at 19:47:49
Specs: Windows 7

(I've combed through several posts here and found things that almost fit, but don't quite do this idea. I thought to ask experts here first before I try to patch work bits I've found. Hopefully the subject title will help others in the future).

Sitation

I have a long list of directories with files of various types inside of them. Some folders and files have spaces. I'd like to find a way to add the folder name to the file name and move it up the directory tree. For example:

c:/sync/programming cc/menu 01.doc ...to... c:/sync/programming cc - menu 01.doc

It's not critical that the new files have spaces, but it helps to break up potentially long names. Could this be a variable depth process -- something that would reach into sub-directories as well? New files would appear at the directory the batch file was run from.

This would be something used frequently. A separate mobile program I use can only import files, which blasts out the directory structure I have on my PC. However, if the file name also had the directory(s) name in it, I can quickly recreate the directory structure with the internal album feature they have.

Thanks for any direction or suggestions!
-Al


See More: DOS Batch: rename folder/file to folder_file

Report •


#1
December 20, 2010 at 08:00:33

(Sitation = Situation)

Report •

#2
December 24, 2010 at 12:46:53

maybe this can help .in simple steps.

1) create a list of files you want to process.. (you can use dir /s with parameters with suits your file types and then redirect the output to a text file.)
2) run a for loop which will process all files in that list one by one.
that loop will assign each part of the file name (Including path) to a variable.

example... "c:/sync/programming cc/menu 01.doc"
C:, Sync, Programming cc, Menu 01.doc will be variblaes like a,b,c and d.
3) Rename the file (In same loop) ,you can use C:\sync\%c%-%d%
this will rename the file as "c:/sync/programming cc-menu 01.doc"

Try to write code for this, incase errors or questions let us know.

Subhash Chandra.


Report •

#3
December 24, 2010 at 13:35:04

Does this call for some hopelessly obtuse FOR nesting? I think it does.
for /d %%a in (*) do @for %%b in ("%%a\*") do @for %%d in ("%%b\..") do @move "%%b" "%%~Nd - %%~NXb"

If you wanted variable depth, VBScript would probably be easier. Alternatively, you could just make a second batch script to run this script on every directory.

How To Ask Questions The Smart Way


Report •

Related Solutions


Ask Question