Sometimes when making choices using R, you can use only a single value to base your choice on. You could apply that code on each value you have by hand, but it makes far more sense to automate this task.
Construct a for loop
As in many other programming languages, you repeat an action for every value in a vector by using a for loop. You construct a for loop in R as follows:
This for loop consists of the following parts:
- The keyword for, followed by parentheses.
- An identifier between the parentheses. In this example, we use i, but that can be any object name you like.
- The keyword in, which follows the identifier.
- A vector with values to loop over. In this example code, we use the object values, but that again can be any vector you have available.
- A code block between braces that has to be carried out for every value in the object values.
In the code block, you can use the identifier. Each time R loops through the code, R assigns the next value in the vector with values to the identifier.
Calculate values in a for loop
Let’s take another look at the priceCalculator() function. Earlier, we show you a few possibilities to adapt this function so you can apply a different VAT rate for public, private, and foreign clients. You can’t use any of these options in a vectorized way, but you can use a for loop so the function can calculate the price for multiple clients at once.
Use the values of the vector
Adapt the priceCalculator() function as follows:
The first and the last part of the function haven’t changed, but in the middle section, you do the following:
- Create a numeric vector with length 0 and call it VAT.
- For every value in the vector client, apply switch() to select the correct amount of VAT to be paid.
- In each round through the loop, add the outcome of switch() at the end of the vector VAT.
The result is a vector VAT that contains, for each client, the correct VAT that needs to be applied. You can test this by adding, for example, a variable type to the data frame clients you created in the previous section like this:
I have a batchfile that looks like this:
This works perfectly for my needs.My question is how do I make this run in all sub-folders?
SportySpiceSportySpice
1 Answer
How do I a run command in all subdirectories in a batch file?
You can use
for /r
for this.Use following batch file:
for /r - Loop through files (Recurse subfolders)
Syntax
FOR /R [[drive:]path] %%parameter IN (set) DO command
Key
drive:path
: The folder tree where the files are located. Extreme force special edition box.set
: A set of one or more files. Wildcards must be used. If (set) is a period character (.) then FOR will loop through every folder.command
: The command(s) to carry out, including any command-line parameters.%%parameter` : A replaceable parameter: in a batch file use %%G (on the command line %G)
Source for /r - Loop through files (Recurse subfolders).
Further Reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- for /r - Loop through files (Recurse subfolders).
112k2727 gold badges247247 silver badges282282 bronze badges
Not the answer you're looking for? Browse other questions tagged batchbatch-file or ask your own question.
I have the below code that works perfectly to loop through every file in a folder. However, I now need the code to loop through all the files in all the subfolders of the parent folder where this file is. Can someone please revise the code below to loop through all the subfolders?Thank you very much!
Sub CopyDataBtwnWorkbooks()
Dim sFolder As String
Dim sFile As String
Dim wbSource As Workbook
Dim wbMaster As Workbook
Dim shTarget As Worksheet
Dim shSource As Worksheet
sFolder = ThisWorkbook.Path & '
Set wbMaster = ThisWorkbook
Set shTarget = ThisWorkbook.Sheets('Audit')
On Error GoTo errHandler
Application.ScreenUpdating = False
sFile = Dir(sFolder & '*.xls*')
Do Until sFile = '
If sFile <> wbMaster.Name Then
Set wbSource = Workbooks.Open(sFolder & sFile)
Set shSource = wbSource.Sheets(1)
Call CopyData(shSource, shTarget, sFile)
wbSource.Close SaveChanges:=False
End If
sFile = Dir()
Loop
Application.ScreenUpdating = True
Application.EnableEvents = True
errHandler:
Application.ScreenUpdating = True
End Sub
Batch Loop Through Files In Subdirectories
I found this code on Mr.Excel, in my search, but I can't get it to work.Sub LoopFolder()
Dim fso As New FileSystemObject
Dim f As Folder, sf As Folder
Dim ofile As File
Dim MyPath As String, MyFile As String, File As Workbook
Set fso = CreateObject('Scripting.FileSystemObject')
Set f = fso.GetFolder('C:TestsBBR')
For Each sf In f.SubFolders
For Each ofile In sf.Files
If fso.GetExtensionName(ofile.Path) = 'xls' Then
Debug.Print ofile.Name
End If
Next
Next
End Sub
I have a batchfile that looks like this:
This works perfectly for my needs.My question is how do I make this run in all sub-folders?
SportySpiceSportySpice
1 Answer
How do I a run command in all subdirectories in a batch file?
You can use
for /r
for this.Use following batch file:
for /r - Loop through files (Recurse subfolders)
Syntax
FOR /R [[drive:]path] %%parameter IN (set) DO command
Key
drive:path
: The folder tree where the files are located.set
: A set of one or more files. Wildcards must be used. If (set) is a period character (.) then FOR will loop through every folder.Nonton film indonesia online. Nonton film X-Men Origins: Wolverine (2009) streaming dan download movie subtitle indonesia kualitas HD gratis terlengkap dan terbaru. Action, Adventure.
command
: The command(s) to carry out, including any command-line parameters.%%parameter` : A replaceable parameter: in a batch file use %%G (on the command line %G)
Source for /r - Loop through files (Recurse subfolders).
Further Reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- for /r - Loop through files (Recurse subfolders).
112k2727 gold badges247247 silver badges282282 bronze badges
Not the answer you're looking for? Browse other questions tagged batchbatch-file or ask your own question.
Hi Experts,I am trying to build a simple SSIS package that loops through subfolders within a Folder. Now there can be many ways of doing that but my intention is to use ForEach Loop Container that uses ForEach Item Enumerator.
Let me explain how !
Below is my simple package. Please note the two variables, objFolder and srcFilePath.
The first step is to access the Source Folder (Folder specified by srcFilePath) and create a collection of SubFolders. This is done within the Script folder using this simple statement:
Till here there's no issue.
Now I want to use a ForEach Loop Container that uses ForEach Item Enumerator and iterate through the objFolder collection. I have spent many hours reading through material on the Internet but I haven't got a single link that tells me how to use the collection variable to be assigned within the ForEach container. Can someone please point me in the correct direction. Given below is the screenshot of the foreach I need to configure.