Click to See Complete Forum and Search --> : Batch file assistance


neville310
August 22nd, 2005, 05:44 PM
I need to create a batch file, which locates all files with a particular extension and moves them several levels up in the folder structure. Here is the situation. My zip utility extracted numerous archives, RAR archives within a ZIP files. Each archive was extracted with a default path, which create three or four levels of folders. The last folder level contains the extracted files.

I want to copy these files into a folder 4 levels up and delete the extraneous folders. The manual approach seems doable, yet two hours later I found myself with ¼ way through. These directories number in the hundreds.

Then I thought a batch file could simplify the solution. But I only have fundamental experience with batch files. Please let me know if anyone could provide me with some assistance and lead me in the right direction.

How do you move multiple files with different file extensions (like *.doc, *.txt, *.htm)? How do you count the number of child directories so the batch file moves the files up to the parent folder? How do you search recursive folders in a directory?

For illustration purposes, the folder hiearchy follows below. I need to get all text, html, and doc files from ArchiveNameAgain into ArchiveName. I need a batch file since the scenario exist for a couple hundred archive folders.

uncompressing\ArchiveName\UnZipArchive\UnRAR\ArchiveNameAgain\files.txt, file.htm

Here’s my starting point.

@ECHO ON
REM This batch file should search for recursive directory and extract the files from the last
REM final folder level; and move the files a specific number of folders up.


F:
CD Uncompressing\


:AGAIN
ECHO MOVING %1
MOVE %1\%1\*.*
REM DELTREE /Y %1
SHIFT
IF NOT "%1" == "" GOTO AGAIN

ECHO

MrViggy
August 22nd, 2005, 06:32 PM
Maybe some of these links will help:

http://home7.inet.tele.dk/batfiles/
http://www.cs.ntu.edu.au/homepages/bea/home/subjects/ith305/description.html

Viggy

neville310
August 23rd, 2005, 02:51 AM
These links are a great help. Thanks!

neville310
August 24th, 2005, 08:38 PM
@echo ON

SET "dir=f:\uncompressing"
SET "ext=*.doc *.txt *.htm"

FOR /r "%dir%" %%* IN (%ext%) DO (
MOVE "%%*" "%%~dp*..\..\")

FOR
* Used to repeat a command on a group of files / drives.
* SYNTAX -:
o FOR /r "%dir%" %%* IN (%ext%) DO (
echo ^
move "%%*" "%%~dp*..\..\")
+ WHERE :
# %%* is a variable name
# (%ext%) is a group of files
# move is a command that is to be repeated for each member of list.


I am almost there with this batch file. It does the job, yet could use additional modifications. Upon activating the batch file, it runs through the recursive directories; identifies the files; and moves them up two levels. The issue occurs in the folders where I already manually performed these tasks. The batch file moves these files outside the "uncompressing" folder and creates a mess. I could try and separate these folders; but will miss a few.

I wonder if it possible to include the following functionality. If these text files are four levels deep, move them up three levels; if three levels exist, move them up two levels; if two levels exist move them up one level; if one level exist don't move anything.

I need the files to end up in the Archive folder (different names for each archive); NOT the uncompressed folder; not the root dirve. Let me know if anything is possible.

f:\uncompressing\Archive\document.txt

f:\uncompressing\Archive\Unzip\document.txt

f:\uncompressing\Archive\Unzip\UnRAR\document.txt

MrViggy
August 25th, 2005, 11:03 AM
Sounds like you'll need PERL, or something else. I don't beleive there is a batch command that can tell you how deep a file is. You'd have to parse the full folder name, which is super easy in PERL.

Viggy