robert_neville Posted August 22, 2005 Share Posted August 22, 2005 (edited) 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.htmHere’s my starting point.@ECHO ONREM This batch file should search for recursive directory and extract the files from the lastREM final folder level; and move the files a specific number of folders up.F:CD Uncompressing\:AGAINECHO MOVING %1MOVE %1\%1\*.*REM DELTREE /Y %1SHIFTIF NOT "%1" == "" GOTO AGAINECHO Edited August 22, 2005 by robert_neville Link to comment Share on other sites More sharing options...
robert_neville Posted August 25, 2005 Author Share Posted August 25, 2005 @echo ONSET "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.txtf:\uncompressing\Archive\Unzip\document.txtf:\uncompressing\Archive\Unzip\UnRAR\document.txt Link to comment Share on other sites More sharing options...
Dahi Posted August 25, 2005 Share Posted August 25, 2005 (edited) 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 August 25, 2005 by Dahi Link to comment Share on other sites More sharing options...
nakira Posted August 25, 2005 Share Posted August 25, 2005 Maybe this way@echo offcd /d f:\Uncompressingfor /d %%i in (*) do ( pushd %%i for /f "delims=/" %%a in ('dir /s/b *.txt;*.htm;*.doc') do move "%%a" "%%~nxa" popd ) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now