Quickly Perform a Function on Several Files
I was doing some tweaks on my new Vista system the other day. One of the things I wanted to so was add a bunch of Visio stencils to my system that I had downloaded from one of the hardware manufacturers. Unfortunately, there were over a hundred stencils and each one was compressed into a ZIP archive. I really didn’t relish right clicking on each zip file, clicking on Extract all…, etc.
So, in situations like this, I immediately drop down to the command prompt. This is where I decided to use the for command to perform and action on every file in the directory.
So, in my example, I used the following command:
for /R %z in (*.zip) do unzip "%z"
Within seconds, I had all of the files extracted and I was ready to go!
Let’s break down this command into its smaller components to see what each one does.
for - indicates that we are going to be using the for command
/R - tells the for command to recursively look in all folders within the active folder as well. Thus, if there are any files in subfolders, they will also be found
%z - this is the variable that will contain the filename that is found. This can be any number but it is case sensitive. If you want to use the for command in a batch file, you will need to use %% (e.g. %%z) instead of just %.
in (*.zip) - this is the search pattern that will be found. The portion in parenthesis uses the same wildcard scheme as the dir command.
do - indicates that the string following is the command that should be executed on each file found
unzip “%z” - this is the actual command that is executed for each file. The %z portion is replaced by the name of the file found. So, for example, if the file found was archive.zip, the command that would be executed would be unzip "archive.zip". Remember that is you are going to be doing this in a batch file, replace the single % with %%.
10 Responses to “Quickly Perform a Function on Several Files”
-
reddiggulo.us - The Most Recent Popular Stories Says:
November 30th, 1999 at 12:00 amDaily Cup of Tech » Quickly Perform a Function on Several Files [IMG Most interesting. ][IMG More information about this story. ][IMG Share this story. ] [IMG This story came from del.icio.us.]
-
doggdot.us Says:
November 30th, 1999 at 12:00 amDaily Cup of Tech » Quickly Perform a Function on Several Filessource: del.icio.usposted: Fri Mar 30, 2007
-
LegHumped Says:
November 30th, 1999 at 12:00 amunzip “%1” - the command that is executed for each file. The %1 portion is replaced by the name of the file found. So, for example, if the file found was archive.zip, the command that would be executed would be unzip “archive.zip”. DailyCupOfTech
-
Gadgettes.com Says:
November 30th, 1999 at 12:00 amQuickly Perform a Function on Several Files [Daily Cup of Tech] [IMG] More: continued here Comments(0)
-
EntreGeeks Says:
November 30th, 1999 at 12:00 am[IMG ]Enlaces Technorati Windows Vista flummoxes consumersQuickly Perform a Function on Several FilesMicrosoft previews DeepfishPhatware: Calligrapher 8.5 PromotionDeepfish - mobile browser causing a splash.アドレスバーのパス名Распознавание речиWindows Vista Hardware Assessment Guidance Pages now available in 8 new languages
-
Aggregatore notizie | iomeneandrei Says:
November 30th, 1999 at 12:00 amnon ci scrivete un post e lo pubblichiamo su TANTO. Colgo l’occasione per fare i complimenti agli utenti di questi forum. Per saperne di più sui “cicli for” nel prompt del dos ecco alcuni link: Windows XP Professional Product Documentation Daily Cup of Tech » Quickly Perform a Function on Several Files Still More Windows Command Shell Goodies - the FOR Command Tutte le utility di FWTools, sono opencource e disponibili anche per Linux. Tutto quello di cui abbiamo parlato si può fare anche in Linux, con tutte le possibilità che da in più la riga
-
TANTO Says:
November 30th, 1999 at 12:00 amnon ci scrivete un post e lo pubblichiamo su TANTO. Colgo l’occasione per fare i complimenti agli utenti di questi forum. Per saperne di più sui “cicli for” nel prompt del dos ecco alcuni link: Windows XP Professional Product Documentation Daily Cup of Tech » Quickly Perform a Function on Several Files Still More Windows Command Shell Goodies - the FOR Command Tutte le utility di FWTools, sono opencource e disponibili anche per Linux. Tutto quello di cui abbiamo parlato si può fare anche in Linux, con tutte le possibilità che da in più la riga
-
MogBlog Says:
November 30th, 1999 at 12:00 amlink
-
CodeWorker Says:
November 30th, 1999 at 12:00 amHandy little snippet. Using For…In to process multiple files in the command window. Daily Cup of Tech » Quickly Perform a Function on Several Files for /R %z in (*.zip) do unzip “%z”
-
danieljdoughty Says:
November 30th, 1999 at 12:00 amXP system(obviously not complete, but it will help me get set up at the new job tomorrow): Turn off filterkeys: http://www.ofzenandcomputing.com/zanswers/463 Useful windows scripting tool. allows you to process everything in a directory recursively: http://www.dailycupoftech.com/2007/03/29/quickly-perform-a-function-on-several-files/
-
Kolz Blog Says:
November 30th, 1999 at 12:00 amfor /R %z in (*.zip) do unzip “%z” Iterates through all the zip archives in a folder and extracts them. If you’re starting to get your feet wet automating tasks with batch scripts, this is a nice for primer. —Gina Trapani Quickly Perform a Function on Several Files [Daily Cup of Tech] [IMG] Posted in Software, Lifehacker | [IMG ] No Comments »
-
ViralBabble.com - The best of del.icio.us, Digg, Lifehacker, YouTube, Fark, Slashdot, iFilm, Gizmodo, Furl and Reddit Says:
November 30th, 1999 at 12:00 amDaily Cup of Tech » Quickly Perform…
-
doggdot.us Says:
November 30th, 1999 at 12:00 amDaily Cup of Tech » Quickly Perform a Function on Several Filessource: del.icio.usposted: Fri Mar 30, 2007
-
Rich Says:
March 29th, 2007 at 6:39 amYou do realize that there is an “Extract all” feature right?
-
Rich Says:
March 29th, 2007 at 6:40 amSorry I meant to say that it is also available after you have selected all 100+ files…
-
Tim Fehlman Says:
March 29th, 2007 at 8:51 amThat was exactly what I did initially but it only extracts one of the archives (at least in Vista, I didn’t try it in XP).
But, this is not limited to unzipping archives. This is just the example that I used.
Tim
-
John May Says:
March 29th, 2007 at 9:35 amThe for command is very cool, I use it to do many things. The basic syntax is as follows:
FOR %variable IN (set) DO command [command-parameters]
Also be sure to check out: http://www.robvanderwoude.com/ntfor.html
-
Rich Says:
March 29th, 2007 at 7:09 pmI cannot speak for WinZip/WinRAR + Vista (which I somehow missed you are using big DUH on my part)
Also apparently I was speaking for WinRAR. You can select all the files then right click and select “Extract All” and it will extract all the compressed files into the directory that the files are in all in one motion.
Anyway, I wanted to apologize for my lack of clarity and my stupid assumption that you were using XP.
-
Tim Fehlman Says:
March 29th, 2007 at 7:41 pmRich,
No big deal. WinRAR (an awesome program, BTW) does give you that ability. Unfortunately, the maker’s of Vista forgot to put that functionality into the archiving program that comes with it.Tim
-
Brian Says:
March 29th, 2007 at 8:59 pmNice job.
Somehow, in all my years screwing around with DOS (and they number in the many!) I never got around to this command.
I ended up using other scripting languages…
but sometimes, this kind of thing, right in DOS, (or do we call it “Command Prompt” now?) is convenient and elegant.
Thanks!Brian
-
Rich Says:
March 30th, 2007 at 1:11 amTim,
BTW I should ask, have you written anything yet on how you feel about Vista? I didn’t see anything in a quick search/scan. If you haven’t do you plan on it?
Rich
-
Echilon Says:
March 30th, 2007 at 2:39 pmGreat tip, cheers.
-
pligg.com Says:
March 30th, 2007 at 2:40 pmQuickly Perform a Function on Several Files…
The command prompt is where I decided to use the for command to perform and action on every file in the directory….
