Jump to content

Batch file assistance


Recommended Posts

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

Edited by robert_neville
Link to comment
Share on other sites


@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

Link to comment
Share on other sites

Is this related to unattended installs?

Could you have avoided the problem in the first place by uncompressing without using paths? For example using 7zip with the e option (instead of x) extracts files ignoring the path in the archive.

Recursing directorys with a batch file is extremely difficult, as you are probably finding out. Perl or Windows script would be more suitable.

This untested code should move everything from folders called *\UnzipArchive\UnRar\* to F:\Uncompressing. Test well before use.

for %%I in ( f:\uncompressing\*. ) (
 for %%J in ( %%I\*. ) (
   if /I %%~nJ==UnZipArchive (
     move \y %%J\UnRAR\%%~nI %%I
   )
 )
)

Edited by Dahi
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...