1.

Solve : HELP(ADV): Batch file - rename file to directory name+++?

Answer»

I want to specify a folder in the batch file (ie. C:\Users\intellilogic\Documents\TopFolder) and have it take all the files and subdirectories in it and rename the files, appending there parent directories to the name and movie them to the root folder specified. I want to do this unlimitedly deep, but only renamed the files to its parent and then moving them all the way to the root. Ideally, I would like to avoid duplicate file names, so if duplicate files come about, appending a number to them would work well.

After doing all the renaming and moving, a confirmation of: “Check to make sure all the files successfully have been moved to the root before selecting yes. Would you like to delete all of the child folders now? (Y/N)” Then if I select yes, deleting all of the child folders that are ACTUALLY empty of files, ignoring blank directories, if they aren’t empty, don’t delete them. Assuming it ran correctly, they will be empty and all subdirectories will be cleared, leaving the root folder with just the renamed files.

Example:

[TopFolder]
[Folder1]
- File1.bla
- File2.bla
- File3.bla
[Folder2]
- File1.bla
- File2.bla
- File3.bla
[Folder3]
- File1.bla
- File2.bla
- File3.bla
[SubFolder1]
- File1.bla
- File2.bla
- File3.bla

Transformed into all files under the root folder

TopFolder
Folder1-File1.bla
Folder1-File2.bla
Folder1-File3.bla
Folder2-File1.bla
Folder2-File2.bla
Folder2-File3.bla
Folder3-File1.bla
Folder3-File2.bla
Folder3-File3.bla
SubFolder1-File1.bla
SubFolder1-File2.bla
SubFolder1-File3.bla

For stuff like this:


Thanks in advanced =]Good Luckfirst i would like to say that bill is making an effort at a joke, we don't charge at all!.

now i will continue with the creation of the script Hehe - I wouldn't think there would be a charge, I am currently broke anyhow, haha.

Thanks peace man =]Quote from: BatchFileBasics on September 30, 2009, 08:56:21 PM

first i would like to say that bill is making an effort at a joke, we don't charge at all!.

now i will continue with the creation of the script

teach him how to fish, not fish for him.Peace.Once I have the file, i will look at the code and learn it that way, it's like watching Jesus fish... =-0Why not break the job into smaller taks?
Do you have plenty of disk space?
First GENERAL rule is not to put stuff into the root.
Why do you want us to bend this rule?
How about a directory called
D:\BOGUSROOT

Quote from: billrich on October 01, 2009, 07:52:41 AM
How did Jesus feed the mulitude? Jesus fed the people fish. Jesus did not teach them to fish.
can you ask Jesus, whoever he is, to GIVE you a fish now? tell him to drop the fish from the SKY.. if you can do that, i will treat you to a cup of coffee. Bye@bill, stop babbling nonsense. Quote from: Geek-9pm on October 01, 2009, 12:12:00 PM
Why not break the job into smaller taks?
Do you have plenty of disk space?
First general rule is not to put stuff into the root.
Why do you want us to bend this rule?
How about a directory called
D:\BOGUSROOT



Sorry - I meant the root from which you start from, IE the top folder.OK you can use this as a starting PLACE.


Code: [Select]@ECHO OFF
CLS

REM THIS WILL SCAN THE CURRENT FOLDER AND RETURN SUB-FOLDERS TO A TEXT FILE
CD %CD%
DIR /b /s /ad>"%TEMP%\DIRTEST.TXT"

REM USE DIRTEST.TXT IN A FOR LOOP
FOR /f "delims==" %%A in (C:\DOCUME~1\%USERNAME%\LOCALS~1\Temp\DIRTEST.TXT) DO SET A=%%A& CALL :DIR %%A


CLS
IF EXIST "%TEMP%\DIRTEST.TXT" DEL "%TEMP%\DIRTEST.TXT"
IF EXIST "%TEMP%\DIRFILE.TXT" DEL "%TEMP%\DIRFILE.TXT"
ECHO JOB COMPLETE.....
Pause
GOTO END

:DIR

REM THIS WILL SCAN THE CURRENT FOLDER AND RETURN FILES TO A TEXT FILE

CD\
CD "%A%"

DIR /b /a-d>"%TEMP%\DIRFILE.TXT"

REM USE DIRTEST.TXT IN A FOR LOOP
FOR /f "delims==" %%A in (C:\DOCUME~1\%USERNAME%\LOCALS~1\Temp\DIRFILE.TXT) DO SET FILE=%%A& CALL :DIRFILE %%A

:DIRFILE
ECHO %FILE%
PAUSE

:END


Please let me know if this dose not work for you.This looks very helpful, I will play with it - I think I will start at the top w/
IF EXIST "%TEMP%\DIRTEST.TXT" DEL "%TEMP%\DIRTEST.TXT" JIC =]
Can I take a stab at this using a SCRIPTING approach ? Comments are inserted so you can see what the script is doing line by line.

Code: [Select]##############################################
# Script: tf.txt (Not sure what to call it)
##############################################
# Usage: script tf.txt root("C:\Users\intellilogic\Documents\TopFolder")
##############################################
# Script moves all files in the root folder (and its sub and sub-sub folders)
# to the root folder with the parent folder name prepended to file name.
#
# For example, if root is "C:\Users\intellilogic\Documents\TopFolder",
#
# the file C:\Users\intellilogic\Documents\TopFolder\Folder1\File1.bla
# will be moved to
# C:\Users\intellilogic\Documents\TopFolder\Folder1-File1.bla.
#
# File C:\Users\intellilogic\Documents\TopFolder\Folder3\SubFolder1\File1.bla
# will be moved to
# C:\Users\intellilogic\Documents\TopFolder\SubFolder1-File1.bla.
##############################################

# Input argument.
var str root # This value is passed to the script.
# If not assigned, current folder will be used.

# Go to root folder.
cd $root

# Collect a list of all flat files.
var str list ; lf -rng "*" "." ($ftype=="f") > $list

# Loop thru files.
while ($list <> "")
do

# Get the next file.
var str file ; lex "1" $list > $file

# $file has the full path. Get just the file name. It is after the last /.
# Preserver value in $file. We will need it in move command later.
var str name ; stex -p "^/^l[" $file > $name

# Get the parent folder of the file. It is between the last and
# secondlast /.
var str parent

# Get the portion of $file upto the last /.
stex -p "]^/^l" $file > $parent

# $parent now has full path of parent folder.
# Get just the folder name. That's after the last /.
stex "^/^l[" $parent > $parent

# The new file name is now $parent-$name.
# We want to move the file to $root/$parent-$name.
system -s "move /Y" ("\""+$file+"\"") ("\""+$root+"/"+$parent+"-"+$name+"\"")

done



Script is in biterscripting ( http://www.biterscripting.com ) . But you should translate it to any scripting language of your choice or batch commands. I have added comments for that. (The script just looks bigger because of all the comments and beautification.)




Discussion

No Comment Found