<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">

<channel>
	<title>Automation Beyond &#187; File System Operations</title>
	<atom:link href="http://automation-beyond.com/category/automation/routines/routines-files/feed/" rel="self" type="application/rss+xml" />
	<link>http://automation-beyond.com</link>
	<description>An engineering approach to Software Testing and Test Automation</description>
	<lastBuildDate>Thu, 02 Feb 2012 14:56:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-nd/3.0/</creativeCommons:license>		<item>
		<title>How to find last modified file (TestComplete, QTP, VBScript)</title>
		<link>http://automation-beyond.com/2011/07/26/how-to-find-last-modified-file/</link>
		<comments>http://automation-beyond.com/2011/07/26/how-to-find-last-modified-file/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 10:20:02 +0000</pubDate>
		<dc:creator>Albert Gareev</dc:creator>
				<category><![CDATA[File System Operations]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[3. Automation]]></category>
		<category><![CDATA[answer]]></category>
		<category><![CDATA[Attribute]]></category>
		<category><![CDATA[check]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[DateCreated]]></category>
		<category><![CDATA[DateLastAccessed]]></category>
		<category><![CDATA[DateLastModified]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[filename unknown]]></category>
		<category><![CDATA[FileSystemObject]]></category>
		<category><![CDATA[FSO]]></category>
		<category><![CDATA[last]]></category>
		<category><![CDATA[modified]]></category>
		<category><![CDATA[most]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[property]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[question]]></category>
		<category><![CDATA[recent]]></category>
		<category><![CDATA[testcomplete]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://automation-beyond.com/?p=3759</guid>
		<description><![CDATA[Reference page: Service Functions – System (QTP, VBScript) Description  Check folder exists &#8211; check folder has files. Iterate through collection of files and keep reference of the most recently modified file (its DateLastModified property is always the highest number). The remaining reference is the one that you need. In case of errors the function returns [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fautomation-beyond.com%252F2011%252F07%252F26%252Fhow-to-find-last-modified-file%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FnDgvOC%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22How%20to%20find%20last%20modified%20file%20%28TestComplete%2C%20QTP%2C%20VBScript%29%22%20%7D);"></div>
<p><div style="position: relative; z-index:1;"><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* Banner 468x60 */
google_ad_slot = "8933038987";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></p>
<p>Reference page: <a title="Service Functions – System (QTP, VBScript)" href="http://automation-beyond.com/2009/12/05/service-functions-system/" target="_blank">Service Functions – System (QTP, VBScript)</a></p>
<h4>Description </h4>
<p>Check folder exists &#8211; check folder has files. Iterate through collection of files and keep reference of the most recently modified file (its <em>DateLastModified</em> property is always the highest number). The remaining reference is the one that you need. In case of errors the function returns <em>Nothing</em>. </p>
<p>Note. In some cases, you may want to use <em>DateCreated</em> or <em>DateLastAccessed</em> properties.</p>
<h4>Implementation</h4>
<p><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* 4 links text line */
google_ad_slot = "5367351963";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<pre class="brush: vb; title: ;">

Public Function GetLastModifiedFile(ByVal sFolderPath)
  Dim FSO, objFolder, objFile
  Dim objFileResult, longDateTime
  Dim boolRC
 
  Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
  boolRC = FSO.FolderExists(sFolderPath)
  If Not boolRC Then
    Set FSO = Nothing
    Set GetLastModifiedFile = Nothing
    Exit Function
  End If

  Set objFolder = FSO.GetFolder(sFolderPath)
  If objFolder.Files.Count = 0 Then
    Set FSO = Nothing
    Set objFolder = Nothing
    Set GetLastModifiedFile = Nothing
    Exit Function
  End If
 
  Set objFileResult = Nothing
  longDateTime = CDate(0)
 
  For Each objFile in objFolder.Files
 
    If objFile.DateLastModified &gt; longDateTime Then
      Set objFileResult = objFile
      longDateTime = objFile.DateLastModified
    End If
   
  Next
 
  Set FSO = Nothing
  Set objFolder = Nothing
  Set GetLastModifiedFile = objFileResult

End Function
</pre>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://automation-beyond.com/2008/06/13/createxmldomfromfile/" rel="bookmark" class="crp_title">CreateXMLDOMFromFile (QTP, TestComplete, VBScript)</a></li><li><a href="http://automation-beyond.com/2010/01/12/service-functions-excel/" rel="bookmark" class="crp_title">Service Functions – Excel (QTP, TestComplete, VBScript)</a></li><li><a href="http://automation-beyond.com/2008/11/10/minor-major-defect/" rel="bookmark" class="crp_title">Minor defect as a symptom of a major defect</a></li><li><a href="http://automation-beyond.com/2008/06/30/service-functions-msxmldom/" rel="bookmark" class="crp_title">Service Functions &#8211; MSXMLDOM (QTP, TestComplete, VBScript)</a></li><li><a href="http://automation-beyond.com/2010/11/10/testcomplete-find-child/" rel="bookmark" class="crp_title">TestComplete &#8211; Find Child or Find Yourself?</a></li></ul></div>
]]></content:encoded>
			<wfw:commentRss>http://automation-beyond.com/2011/07/26/how-to-find-last-modified-file/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Checking structure of created folders (Excel/VBA)</title>
		<link>http://automation-beyond.com/2010/05/05/checking-folder-structure/</link>
		<comments>http://automation-beyond.com/2010/05/05/checking-folder-structure/#comments</comments>
		<pubDate>Wed, 05 May 2010 13:24:31 +0000</pubDate>
		<dc:creator>Albert Gareev</dc:creator>
				<category><![CDATA[Back-end]]></category>
		<category><![CDATA[File System Operations]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[check]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[folder]]></category>
		<category><![CDATA[FSO]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[structure]]></category>
		<category><![CDATA[subfolders]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[VBA]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://automation-beyond.com/?p=2199</guid>
		<description><![CDATA[In the previous post I warned about possible synchronization issues while copying a complex and/or large file-folder structure. To ensure reliability of the operation performed we may verify the structure that was created. We need to answer the following questions before designing the script. Which folders and files should it verify? Which way should it [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fautomation-beyond.com%252F2010%252F05%252F05%252Fchecking-folder-structure%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Checking%20structure%20of%20created%20folders%20%28Excel%2FVBA%29%22%20%7D);"></div>
<p><div style="position: relative; z-index:1;"><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* Banner 468x60 */
google_ad_slot = "8933038987";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></p>
<p>In the <a href="http://automation-beyond.com/2010/05/03/creating-folder-structure-from-template/" target="_blank">previous post</a> I warned about possible synchronization issues while copying a complex and/or large file-folder structure. To ensure reliability of the operation performed we may verify the structure that was created.</p>
<p>We need to answer the following questions before designing the script.</p>
<ul>
<li>Which folders and files should it verify?</li>
<li>Which way should it verify specified object?</li>
<li>Where this information is stored?</li>
<li>What is the format of data provided?</li>
</ul>
<p> </p>
<p>For this example, we make the following assumptions.</p>
<ul>
<li>A file named &#8220;folders.txt&#8221; is stored in the root folder of a template and automatically copied with it while creating a user folder.</li>
<li>The file is in raw text format</li>
<li>Only folders are specified</li>
</ul>
<p>Assuming that we use sample structure from the previous example as a baseline, &#8220;folders.txt&#8221; file will contain the following lines.</p>
<pre class="brush: plain; title: ;">
Application Data\Application1
Application Data\Application2
Application Data\Application1\INI
Documents
Documents\Back-up
</pre>
<p>Order of the folders specified is not important.</p>
<p><em>CheckFolderStructure</em> function provided below performs a sequential checking.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* 4 links text line */
google_ad_slot = "5367351963";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<pre class="brush: vb; title: ;">

Public Function CheckFolderStructure(ByVal sTargetFolder)
Dim boolRC
Dim sCheckFile, objCheckFile
Dim sCheckLine, sCheckPath

Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)

boolRC = objFSO.FolderExists(sTargetFolder)
If Not boolRC Then
    Set objFSO = Nothing
    CheckFolderStructure = False
    Exit Function
End If

sCheckFile = sTargetFolder &amp; &quot;\folders.txt&quot;
boolRC = objFSO.FileExists(sCheckFile)
If Not boolRC Then
    Set objFSO = Nothing
    CheckFolderStructure = False
    Exit Function
End If

Set objCheckFile = objFSO.OpenTextFile(sCheckFile, 1)

Do While (Not objCheckFile.AtEndOfStream)
    sCheckLine = objCheckFile.ReadLine()
    sCheckPath = sTargetFolder &amp; &quot;\&quot; &amp; sCheckLine
    boolRC = objFSO.FolderExists(sCheckPath)
    If Not boolRC Then
        objCheckFile.Close
        Set objCheckFile = Nothing
        Set objFSO = Nothing
        CheckFolderStructure = False
        Exit Function
    End If
Loop

objCheckFile.Close
Set objCheckFile = Nothing
Set objFSO = Nothing
CheckFolderStructure = True

End Function
</pre>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://automation-beyond.com/2010/07/08/snapshot-folder-structure/" rel="bookmark" class="crp_title">Taking snapshot of a folder structure</a></li><li><a href="http://automation-beyond.com/2010/05/03/creating-folder-structure-from-template/" rel="bookmark" class="crp_title">Creating folder structure from a template (Excel/VBA)</a></li><li><a href="http://automation-beyond.com/2010/09/28/automation-tutorials-new-chapter/" rel="bookmark" class="crp_title">Automation Tutorials &#8211; New Chapter</a></li><li><a href="http://automation-beyond.com/2010/01/12/service-functions-excel/" rel="bookmark" class="crp_title">Service Functions – Excel (QTP, TestComplete, VBScript)</a></li><li><a href="http://automation-beyond.com/2009/01/09/service-functions-dictionary/" rel="bookmark" class="crp_title">Service Functions – Dictionary (QTP, TestComplete, VBScript)</a></li></ul></div>
]]></content:encoded>
			<wfw:commentRss>http://automation-beyond.com/2010/05/05/checking-folder-structure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating folder structure from a template (Excel/VBA)</title>
		<link>http://automation-beyond.com/2010/05/03/creating-folder-structure-from-template/</link>
		<comments>http://automation-beyond.com/2010/05/03/creating-folder-structure-from-template/#comments</comments>
		<pubDate>Mon, 03 May 2010 14:01:28 +0000</pubDate>
		<dc:creator>Albert Gareev</dc:creator>
				<category><![CDATA[Back-end]]></category>
		<category><![CDATA[File System Operations]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[folder]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[structure]]></category>
		<category><![CDATA[subfolders]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[VBA]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://automation-beyond.com/?p=2188</guid>
		<description><![CDATA[How to create a complex folder structure by using a template In the previous post I provided code example that creates a folder and then applies security permissions on it. However, as per requirements, user folders often come with a predefined structure of folders and some files created by default. Sure, we can write script [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fautomation-beyond.com%252F2010%252F05%252F03%252Fcreating-folder-structure-from-template%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Creating%20folder%20structure%20from%20a%20template%20%28Excel%2FVBA%29%22%20%7D);"></div>
<p><div style="position: relative; z-index:1;"><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* Banner 468x60 */
google_ad_slot = "8933038987";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></p>
<h3>How to create a complex folder structure by using a template</h3>
<p>In the <a href="http://automation-beyond.com/2010/04/29/setacl-command-line-automation/" target="_blank">previous post</a> I provided code example that creates a folder and then applies security permissions on it. However, as per requirements, user folders often come with a predefined structure of folders and some files created by default. Sure, we can write script that creates folders one by one. But will it be a compact and maintainable solution?</p>
<p>Let&#8217;s try an alternative way.</p>
<p>We will use the following template as an example.</p>
<p>Root folder (will become user&#8217;s personal folder): <strong>&#8220;testuser&#8221;</strong><br />
Pre-defined user folders: <strong>&#8220;Application Data&#8221;</strong>, <strong>&#8220;Documents&#8221;</strong><br />
Files coming by default: <strong>&#8220;Application Data\readme.txt&#8221;</strong>, <strong>&#8220;Documents\readme.txt&#8221;</strong></p>
<p><a href="http://automation-beyond.com/wp/wp-content/uploads/2010/04/userfolders.jpg"><img class="alignnone size-full wp-image-2196" title="userfolders" src="http://automation-beyond.com/wp/wp-content/uploads/2010/04/userfolders.jpg" alt="" width="473" height="216" /></a></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* 4 links text line */
google_ad_slot = "5367351963";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<pre class="brush: vb; title: ;">
'Declare external function
'It will be used to pause execution during asynchronous calls
Private Declare Sub Sleep Lib &quot;kernel32&quot; (ByVal dwMilliseconds As Long)
'
Dim sTemplateFolder, sUserFolder
sTemplateFolder = &quot;C:\Temp\Users\testuser&quot;
sUserFolder = &quot;C:\Temp\Users\DoeJohn&quot;
           
'Create folder - through XCOPY
sCommandLine = &quot;c:\windows\system32\XCOPY.EXE &quot;&quot;&quot; &amp; sTemplateFolder &amp; &quot;&quot;&quot; &amp; &quot; &quot;&quot;&quot; &amp; sUserFolder &amp; &quot;&quot;&quot; /E /I /H /K /O&quot;
intRC = Shell(sCommandLine, vbHide)
'Shell is an asynchronous call - system needs time to process it
'Sync
Sleep XCOPY_THINK_TIME
       
'Error-handling
If boolRC Then
'code your recovery steps
End If
</pre>
<p style="text-align: center;">
<h4 style="text-align: center;">XCOPY Tool</h4>
<p><strong>The Tool.</strong> <a href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true" target="_blank">XCopy</a> is a Microsoft&#8217;s tool that comes with an operating system. Using the link provided you can learn more about it.</p>
<p><strong>The Path.</strong> In the example provided the path is hard-coded. You can also make it configurable/customizable.</p>
<p><strong>Command line.</strong> The following command line switches were used.</p>
<p>/E &#8211; Copy entire subdirectory structure, including empty ones.<br />
/I &#8211; To specify that destination objects must be created (otherwise, the tool asks for a confirmation).<br />
/H &#8211; Copy files with &#8220;hidden&#8221; and &#8220;system&#8221; attributes (the tool skips them by default).<br />
/K &#8211; Retain &#8220;read-only&#8221; attribute on destination files if presented on the source files.<br />
/O &#8211; Copy files/folders with security permissions as on the source files.</p>
<h4 style="text-align: center;">Synchronization</h4>
<p>XCOPY_THINK_TIME must be defined considering the following factors: total size of a template structure, caching, network speed, and priority of your process. Worth to try copying a few times through command line manually to define a sufficient time for a delay.</p>
<p>If your template has a very complex structure or big in size you should check consistency of the created structure to ensure reliability of the automatic process.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://automation-beyond.com/2010/05/05/checking-folder-structure/" rel="bookmark" class="crp_title">Checking structure of created folders (Excel/VBA)</a></li><li><a href="http://automation-beyond.com/2009/12/05/service-functions-system/" rel="bookmark" class="crp_title">Service Functions – System (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2009/01/09/service-functions-dictionary/" rel="bookmark" class="crp_title">Service Functions – Dictionary (QTP, TestComplete, VBScript)</a></li><li><a href="http://automation-beyond.com/2010/07/08/snapshot-folder-structure/" rel="bookmark" class="crp_title">Taking snapshot of a folder structure</a></li><li><a href="http://automation-beyond.com/2010/06/22/quick-questions-quick-answers/" rel="bookmark" class="crp_title">Quick questions &#8211; quick answers</a></li></ul></div>
]]></content:encoded>
			<wfw:commentRss>http://automation-beyond.com/2010/05/03/creating-folder-structure-from-template/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SetACL command line automation examples</title>
		<link>http://automation-beyond.com/2010/04/29/setacl-command-line-automation/</link>
		<comments>http://automation-beyond.com/2010/04/29/setacl-command-line-automation/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 13:17:47 +0000</pubDate>
		<dc:creator>Albert Gareev</dc:creator>
				<category><![CDATA[Back-end]]></category>
		<category><![CDATA[File System Operations]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[2. Testing]]></category>
		<category><![CDATA[3. Automation]]></category>
		<category><![CDATA[attributes]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[deny]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[folder]]></category>
		<category><![CDATA[grant]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[infrastructure]]></category>
		<category><![CDATA[line]]></category>
		<category><![CDATA[permission]]></category>
		<category><![CDATA[sample]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[SetACL]]></category>
		<category><![CDATA[settings]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://automation-beyond.com/?p=2180</guid>
		<description><![CDATA[In this post  I provide code snippets for SetACL tool command line automation.  Examples of command line instructions were taken from here. The code presented below is written in VBA for MS Excel. CreateUserFolder function can create folders on a local or network drive. After user folder is created, the function sets typical access permissions by [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fautomation-beyond.com%252F2010%252F04%252F29%252Fsetacl-command-line-automation%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22SetACL%20command%20line%20automation%20examples%22%20%7D);"></div>
<p><div style="position: relative; z-index:1;"><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* Banner 468x60 */
google_ad_slot = "8933038987";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></p>
<p>In this post  I provide code snippets for <a href="http://automation-beyond.com/2010/04/20/security-tools-setacl/" target="_blank">SetACL tool</a> command line automation.  Examples of command line instructions were taken from <a href="http://automation-beyond.com/2010/04/27/setacl-command-line-examples/" target="_blank">here</a>.</p>
<p>The code presented below is written in VBA for MS Excel.<br />
<em>CreateUserFolder</em> function can create folders on a local or network drive. After user folder is created, the function sets typical access permissions by calling SetACL tool through command line interface (by using Shell function of Excel/VBA).<br />
Pass-in parameters<br />
<em>sUserFolder</em> &#8211; full path to the target folder<br />
<em>sLogin</em> &#8211; user account name (login name)</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* 4 links text line */
google_ad_slot = "5367351963";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<pre class="brush: vb; title: ;">
'Declare external function
'It will be used to pause execution during asynchronous calls
Private Declare Sub Sleep Lib &quot;kernel32&quot; (ByVal dwMilliseconds As Long)
'
'
'Main function
Public Function CreateUserFolder(ByVal sUserFolder, ByVal sLogin)
Dim boolRC, intRC
Dim objFSO, objUserFolder
Dim sToolPath, sCommandLine

'We assume that SetACL is stored at the same location as our Excel file
sToolPath = Workbooks.Item(1).Path &amp; &quot;\SetACL.exe&quot;

Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)

'Create user folder
On Error Resume Next
    Set objUserFolder = objFSO.CreateFolder(sUserFolder)
    boolRC = (Err.Number &lt;&gt; 0)
On Error GoTo 0
Set objFSO = Nothing

'Error-handling
If boolRC Then
   CreateUserFolder = False
   Exit Function
End If
   
'Sync
Sleep 250
   
'Remove inheritance
sCommandLine = &quot;&quot;&quot;&quot; &amp; sToolPath &amp; &quot;&quot;&quot; -on &quot;&quot;&quot; &amp; sUserFolder &amp; &quot;&quot;&quot; -ot file -actn setprot -op &quot;&quot;dacl:p_c;sacl:p_c&quot;&quot;&quot;
intRC = Shell(sCommandLine, vbHide)
'Shell is asynchronous call - system needs time to process it
'Sync
Sleep 1000
   
'Limited error-handling
If intRC = 0 Then
   CreateUserFolder = False
   Exit Function
End If
   
'Remove &quot;Users&quot;/&quot;Domain Users&quot; groups
sCommandLine = &quot;&quot;&quot;&quot; &amp; sToolPath &amp; &quot;&quot;&quot; -on &quot;&quot;&quot; &amp; sUserFolder  &amp; &quot;&quot;&quot; -ot file -actn trustee &quot;
sCommandLine = sCommandLine &amp; &quot;-trst &quot;&quot;n1:users;ta:remtrst;w:dacl&quot;&quot; &quot;
sCommandLine = sCommandLine &amp; &quot;-actn trustee -trst &quot;&quot;n1:domain users;ta:remtrst;w:dacl&quot;&quot;&quot;
intRC = Shell(sCommandLine, vbHide)
'Shell is an asynchronous call - system needs time to process it
'Sync
Sleep 1000
   
'Limited error-handling
If intRC = 0 Then
   CreateUserFolder = False
   Exit Function
End If
   
'Add Modify user permissions
sCommandLine = &quot;&quot;&quot;&quot; &amp; sToolPath &amp; &quot;&quot;&quot; -on &quot;&quot;&quot; &amp; sUserFolder  &amp; &quot;&quot;&quot; -ot file -actn ace &quot;
sCommandLine = sCommandLine &amp; &quot;-ace &quot;&quot;n:&quot; &amp; sLogin &amp; &quot;;p:change&quot;&quot;&quot;
intRC = Shell(sCommandLine, vbHide)
'Shell is an asynchronous call - system needs time to process it
'Sync
Sleep 1000
   
'Limited error-handling
If intRC = 0 Then
   CreateUserFolder = False
   Exit Function
End If
   
'Deny Delete Folder user permissions
sCommandLine = &quot;&quot;&quot;&quot; &amp; sToolPath &amp; &quot;&quot;&quot; -on &quot;&quot;&quot; &amp; sUserFolder &amp; &quot;&quot;&quot; -ot file -actn ace &quot;
sCommandLine = sCommandLine &amp; &quot;-ace &quot;&quot;n:&quot; &amp; sLogin &amp; &quot;;p:delete;i:np;m:deny;w:dacl&quot;&quot;&quot;
intRC = Shell(sCommandLine, vbHide)
'Shell is an asynchronous call - system needs time to process it
'Sync
Sleep 1000
   
'Limited error-handling
If intRC = 0 Then
   CreateUserFolder = False
   Exit Function
End If
   
CreateUserFolder = True

End Function
</pre>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://automation-beyond.com/2010/04/27/setacl-command-line-examples/" rel="bookmark" class="crp_title">SetACL command line examples</a></li><li><a href="http://automation-beyond.com/2010/04/20/security-tools-setacl/" rel="bookmark" class="crp_title">Security Administration Tool &#8211; (Open Source) SetACL</a></li><li><a href="http://automation-beyond.com/2010/05/03/creating-folder-structure-from-template/" rel="bookmark" class="crp_title">Creating folder structure from a template (Excel/VBA)</a></li><li><a href="http://automation-beyond.com/2009/12/21/loops-dexterity-macro/" rel="bookmark" class="crp_title">How to use loops in Dexterity Macro</a></li><li><a href="http://automation-beyond.com/2010/03/25/security-tools-cacls/" rel="bookmark" class="crp_title">Security Administration Tool &#8211; Microsoft CACLS</a></li></ul></div>
]]></content:encoded>
			<wfw:commentRss>http://automation-beyond.com/2010/04/29/setacl-command-line-automation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Processing files contained in a folder</title>
		<link>http://automation-beyond.com/2010/03/15/processing-files-in-folder/</link>
		<comments>http://automation-beyond.com/2010/03/15/processing-files-in-folder/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 13:17:20 +0000</pubDate>
		<dc:creator>Albert Gareev</dc:creator>
				<category><![CDATA[File System Operations]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[.Exists]]></category>
		<category><![CDATA[answer]]></category>
		<category><![CDATA[check]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[filename unknown]]></category>
		<category><![CDATA[FileSystemObject]]></category>
		<category><![CDATA[FSO]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[question]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://automation-beyond.com/?p=2001</guid>
		<description><![CDATA[Reference page: Service Functions – System (QTP, VBScript) Question The question was asked here. I need to check number of text files in the folder but i dont know names Answer Checking what number of files is contained in a folder is simple. More challenging tasks would be processing files based on type or name [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fautomation-beyond.com%252F2010%252F03%252F15%252Fprocessing-files-in-folder%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Processing%20files%20contained%20in%20a%20folder%22%20%7D);"></div>
<p><div style="position: relative; z-index:1;"><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* Banner 468x60 */
google_ad_slot = "8933038987";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></p>
<p>Reference page: <a title="Service Functions – System (QTP, VBScript)" href="http://automation-beyond.com/2009/12/05/service-functions-system/" target="_blank">Service Functions – System (QTP, VBScript)</a></p>
<h3>Question</h3>
<p>The question was asked <a href="http://automation-beyond.com/2009/10/24/how-to-check-if-file-exists/" target="_blank">here</a>.</p>
<blockquote><p>I need to check number of text files in the folder but i dont know names</p></blockquote>
<h3>Answer</h3>
<p>Checking what number of files is contained in a folder is simple. More challenging tasks would be processing files based on type or name patterns.</p>
<p><strong>Implementation</strong></p>
<p><strong>1. Retrieve total number of files</strong><strong> </strong></p>
<pre class="brush: vb; title: ;">
Dim sParentFolder

sParentFolder = &quot;C:\TEMP&quot;

Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
boolRC = FSO.FolderExists(sParentFolder)
Set FSO = Nothing 'release an object
If Not boolRC Then
'do what you need to do if
'folder does not exist
End If

Set objParentFolder = FSO.GetFolder(sParentFolder)

Set objFilesColl = objParentFolder.Files

'objFilesColl.Count contains total number of files
</pre>
<p><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* 4 links text line */
google_ad_slot = "5367351963";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p><strong>2. Count files based on extension</strong><strong> </strong></p>
<p>Used resources: <a title="Service Functions – String (QTP, VBScript)" href="http://automation-beyond.com/2009/10/01/service-functions-string/" target="_blank">Service Functions – String (QTP, VBScript)</a></p>
<p>In the second example number of text files is just counted. Excel files&#8217; names are stored in Dictionary object for further processing.</p>
<pre class="brush: vb; title: ;">Dim sFolder
Dim FSO, objFolder, objFile, objXLSList
Dim intTXTCount

sFolder = &quot;C:\TEMP&quot;

Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objXLSList = CreateObject(&quot;Scripting.Dictionary&quot;)

Set objFolder = FSO.GetFolder(sFolder)
intTXTCount = 0
For Each objFile In objFolder.Files
 If Regex_Test(objFile.Name, &quot;.*\.[t,T][t,T][t,T]&quot;) Then
  intTXTCount = intTXTCount + 1
 End If
 If Regex_Test(objFile.Name, &quot;.*\.[x,X][l,L][s,S]&quot;) Then
  objXLSList.Add objXLSList.Count, objFile.Name
 End If
Next
</pre>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://automation-beyond.com/2009/12/05/service-functions-system/" rel="bookmark" class="crp_title">Service Functions – System (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2009/01/09/service-functions-dictionary/" rel="bookmark" class="crp_title">Service Functions – Dictionary (QTP, TestComplete, VBScript)</a></li><li><a href="http://automation-beyond.com/2011/07/26/how-to-find-last-modified-file/" rel="bookmark" class="crp_title">How to find last modified file (TestComplete, QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2007/06/15/test-automation-architecture-winrunner/" rel="bookmark" class="crp_title">Test Automation Architecture (WinRunner)</a></li><li><a href="http://automation-beyond.com/2009/05/27/vbscript-xml-xsl-2/" rel="bookmark" class="crp_title">Text File compare in &#8220;WDIFF&#8221; style (QTP, VBScript, XML, XSL) &#8211; Instructions and XSL script</a></li></ul></div>
]]></content:encoded>
			<wfw:commentRss>http://automation-beyond.com/2010/03/15/processing-files-in-folder/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to check if folder exists (QTP, VBScript)</title>
		<link>http://automation-beyond.com/2010/01/24/how-to-check-if-folder-exists/</link>
		<comments>http://automation-beyond.com/2010/01/24/how-to-check-if-folder-exists/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 12:59:25 +0000</pubDate>
		<dc:creator>Albert Gareev</dc:creator>
				<category><![CDATA[File System Operations]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[.Exists]]></category>
		<category><![CDATA[.FolderExists]]></category>
		<category><![CDATA[answer]]></category>
		<category><![CDATA[check]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[FileSystemObject]]></category>
		<category><![CDATA[folder]]></category>
		<category><![CDATA[FSO]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[question]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://automation-beyond.com/?p=2022</guid>
		<description><![CDATA[Parent page: Service Functions – System (QTP, VBScript) Use FileSystemObject.FolderExists method. Argument should be a full path string.   When to use it? Always before trying to access the folder (delete, rename, or read from) or create files in it. Note. Always release the object you created when you no longer need it. If you don&#8217;t do [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fautomation-beyond.com%252F2010%252F01%252F24%252Fhow-to-check-if-folder-exists%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22How%20to%20check%20if%20folder%20exists%20%28QTP%2C%20VBScript%29%22%20%7D);"></div>
<p><div style="position: relative; z-index:1;"><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* Banner 468x60 */
google_ad_slot = "8933038987";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></p>
<p>Parent page: <a title="Permanent Link to Service Functions – System (QTP, VBScript)" rel="bookmark" href="http://automation-beyond.com/2009/12/05/service-functions-system/" target="_blank">Service Functions – System (QTP, VBScript)</a></p>
<p>Use <strong>FileSystemObject.FolderExists</strong> method.<br />
Argument should be a <a href="http://en.wikipedia.org/wiki/Full_path" target="_blank">full path</a> string.</p>
<p> <br />
<strong>When to use it?</strong> Always before trying to access the folder (delete, rename, or read from) or create files in it.</p>
<p><strong>Note.</strong> Always release the object you created when you no longer need it. If you don&#8217;t do that then in the long run it&#8217;ll cause you memory leakage problem.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* 4 links text line */
google_ad_slot = "5367351963";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<pre class="brush: vb; title: ;">

Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
boolRC = FSO.FolderExists(sFolderName)
Set FSO = Nothing 'release an object
If Not boolRC Then
'do what you need to do if
'folder does not exist
End If
</pre>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://automation-beyond.com/2009/12/05/service-functions-system/" rel="bookmark" class="crp_title">Service Functions – System (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2011/07/26/how-to-find-last-modified-file/" rel="bookmark" class="crp_title">How to find last modified file (TestComplete, QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2009/10/25/how-to-separate-file-name-and-path/" rel="bookmark" class="crp_title">How to separate file name and path (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2010/03/15/processing-files-in-folder/" rel="bookmark" class="crp_title">Processing files contained in a folder</a></li><li><a href="http://automation-beyond.com/2009/01/09/service-functions-dictionary/" rel="bookmark" class="crp_title">Service Functions – Dictionary (QTP, TestComplete, VBScript)</a></li></ul></div>
]]></content:encoded>
			<wfw:commentRss>http://automation-beyond.com/2010/01/24/how-to-check-if-folder-exists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to create a unique subfolder (QTP, VBScript)</title>
		<link>http://automation-beyond.com/2009/12/16/how-to-create-unique-subfolder/</link>
		<comments>http://automation-beyond.com/2009/12/16/how-to-create-unique-subfolder/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 12:13:41 +0000</pubDate>
		<dc:creator>Albert Gareev</dc:creator>
				<category><![CDATA[File System Operations]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[answer]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[date-based]]></category>
		<category><![CDATA[FileSystemObject]]></category>
		<category><![CDATA[FSO]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[name]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[question]]></category>
		<category><![CDATA[subfolder]]></category>
		<category><![CDATA[unique]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://automationbeyond.wordpress.com/?p=1162</guid>
		<description><![CDATA[Parent page: Service Functions – System (QTP, VBScript) How to create a unique subfolder Description A subfolder is created based on the name pattern and a date-time stamp, e.g. &#8220;AccountSearch 2009-10-14 17-18-03&#8243;.  You can add extra steps and form the code as a function that returns full path of the newly created subfolder. sName – name pattern sFullPath [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fautomation-beyond.com%252F2009%252F12%252F16%252Fhow-to-create-unique-subfolder%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22How%20to%20create%20a%20unique%20subfolder%20%28QTP%2C%20VBScript%29%22%20%7D);"></div>
<p><div style="position: relative; z-index:1;"><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* Banner 468x60 */
google_ad_slot = "8933038987";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><br />
Parent page: <a title="Permanent Link to Service Functions – System (QTP, VBScript)" rel="bookmark" href="http://automation-beyond.com/2009/12/05/service-functions-system/" target="_blank">Service Functions – System (QTP, VBScript)</a></p>
<h4 style="text-align: center;">How to create a unique subfolder</h4>
<p><strong>Description</strong></p>
<p>A subfolder is created based on the name pattern and a date-time stamp, e.g. &#8220;AccountSearch 2009-10-14 17-18-03&#8243;.  You can add extra steps and form the code as a function that returns full path of the newly created subfolder.</p>
<p>sName – name pattern</p>
<p>sFullPath - full path of the newly created subfolder</p>
<p style="text-align: left;"> <br />
<script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* 4 links text line */
google_ad_slot = "5367351963";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<pre class="brush: vb; title: ;">

Dim date_now, dd, dt
Dim FSO
Dim sFullPath

date_now = Now

dd = Year(date_now) &amp; &quot;-&quot; &amp; AlignNumber(Month(date_now), 2, &quot;0&quot;) &amp; &quot;-&quot; &amp; AlignNumber(Day(date_now), 2, &quot;0&quot;)
dt = AlignNumber(Hour(date_now), 2, &quot;0&quot;) &amp; &quot;-&quot; &amp; AlignNumber(Minute(date_now), 2, &quot;0&quot;) &amp; &quot;-&quot; &amp; AlignNumber(Second(date_now), 2, &quot;0&quot;)

If sName = &quot;&quot; Then
sSubfolder = &quot;Data&quot;&amp;&quot; &quot; &amp; dd &amp; &quot; &quot; &amp; dt
Else
sSubfolder = sName &amp; &quot; &quot; &amp; dd &amp; &quot; &quot; &amp; dt
End If
sFullPath = sLogPath &amp; &quot;\&quot; &amp; sSubfolder &amp; &quot;\&quot;

Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
FSO.CreateFolder(sFullPath)
Set FSO = Nothing
</pre>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://automation-beyond.com/2009/12/05/service-functions-system/" rel="bookmark" class="crp_title">Service Functions – System (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2010/10/18/testcomplete-gui-recognition/" rel="bookmark" class="crp_title">TestComplete &#8211; Object Recognition Properties</a></li><li><a href="http://automation-beyond.com/2009/12/14/generate-unique-filename/" rel="bookmark" class="crp_title">How to generate unique file name (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2009/12/15/how-to-duplicate-existing-file/" rel="bookmark" class="crp_title">How to duplicate an existing file (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2010/10/13/considering-probability/" rel="bookmark" class="crp_title">Considering probability in Boundary Testing, and beyond</a></li></ul></div>
]]></content:encoded>
			<wfw:commentRss>http://automation-beyond.com/2009/12/16/how-to-create-unique-subfolder/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to duplicate an existing file (QTP, VBScript)</title>
		<link>http://automation-beyond.com/2009/12/15/how-to-duplicate-existing-file/</link>
		<comments>http://automation-beyond.com/2009/12/15/how-to-duplicate-existing-file/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 12:27:07 +0000</pubDate>
		<dc:creator>Albert Gareev</dc:creator>
				<category><![CDATA[File System Operations]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[already exists]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[copy]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[FileSystemObject]]></category>
		<category><![CDATA[FSO]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://automationbeyond.wordpress.com/?p=1147</guid>
		<description><![CDATA[Parent page: Service Functions – System (QTP, VBScript) How to duplicate an existing file (If you want to access the file but it&#8217;s locked) Description sFileName – name of the file to duplicate sNewName – name of created file (generated automatically), pass-back argument. Uses UniqueFilename function to generate a name for the duplicate file. Public Function DuplicateFile(ByVal [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fautomation-beyond.com%252F2009%252F12%252F15%252Fhow-to-duplicate-existing-file%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22How%20to%20duplicate%20an%20existing%20file%20%28QTP%2C%20VBScript%29%22%20%7D);"></div>
<p><div style="position: relative; z-index:1;"><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* Banner 468x60 */
google_ad_slot = "8933038987";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><br />
Parent page: <a title="Permanent Link to Service Functions – System (QTP, VBScript)" rel="bookmark" href="http://automation-beyond.com/2009/12/05/service-functions-system/" target="_blank">Service Functions – System (QTP, VBScript)</a></p>
<h4 style="text-align: center;">How to duplicate an existing file</h4>
<p>(If you want to access the file but it&#8217;s locked)</p>
<p><strong>Description</strong></p>
<p>sFileName – name of the file to duplicate<br />
sNewName – name of created file (generated automatically), pass-back argument.</p>
<p>Uses <a href="http://automation-beyond.com/2009/12/14/generate-unique-filename/" target="_blank">UniqueFilename</a> function to generate a name for the duplicate file.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* 4 links text line */
google_ad_slot = "5367351963";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<pre class="brush: vb; title: ;">
Public Function DuplicateFile(ByVal sFileName, ByRef sNewName)
Dim boolRC
Dim FSO, objFile
Dim sName, sPath
Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)

'Verify source file exists
boolRC = FSO.FileExists(sFileName)
If Not boolRC Then
Set FSO = Nothing
DuplicateFile = FALSE
Exit Function
End If

'Separate filename and filepath
Set objFile = FSO.GetFile(sFileName)
sName = objFile.Name
sPath = objFile.Path
sPath = Left(sPath, Len(sPath)-Len(sName))
Set objFile = Nothing

'Generate unique name for the dup file
sNewName = sPath &amp; &quot;\&quot; _
_ &amp; UniqueFilename(sName, sPath, AssociateParameters(&quot;prefix = dup, index = 100&quot;))

'Copy file
FSO.CopyFile sFileName, sNewName

Set FSO = Nothing
DuplicateFile = TRUE
End Function
</pre>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://automation-beyond.com/2009/12/05/service-functions-system/" rel="bookmark" class="crp_title">Service Functions – System (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2009/12/14/generate-unique-filename/" rel="bookmark" class="crp_title">How to generate unique file name (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2010/04/08/using-locked-excel-workbook/" rel="bookmark" class="crp_title">How to use test data from a locked Excel Workbook (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2009/05/27/vbscript-xml-xsl-2/" rel="bookmark" class="crp_title">Text File compare in &#8220;WDIFF&#8221; style (QTP, VBScript, XML, XSL) &#8211; Instructions and XSL script</a></li><li><a href="http://automation-beyond.com/2009/10/24/how-to-check-if-file-exists/" rel="bookmark" class="crp_title">How to check if file exists (QTP, VBScript)</a></li></ul></div>
]]></content:encoded>
			<wfw:commentRss>http://automation-beyond.com/2009/12/15/how-to-duplicate-existing-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to generate unique file name (QTP, VBScript)</title>
		<link>http://automation-beyond.com/2009/12/14/generate-unique-filename/</link>
		<comments>http://automation-beyond.com/2009/12/14/generate-unique-filename/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 12:14:42 +0000</pubDate>
		<dc:creator>Albert Gareev</dc:creator>
				<category><![CDATA[File System Operations]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[already exists]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[filename]]></category>
		<category><![CDATA[FileSystemObject]]></category>
		<category><![CDATA[FSO]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[name]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[unique]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://automationbeyond.wordpress.com/?p=1135</guid>
		<description><![CDATA[Parent page: Service Functions – System (QTP, VBScript) How to generate a unique file name if the file name already exists in the given folder (That often happens, if you need to create a series of files or duplicate an existing file) Description sFileName &#8211; the &#8220;original&#8221; file name or file name template sFolderName &#8211; parent folder [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fautomation-beyond.com%252F2009%252F12%252F14%252Fgenerate-unique-filename%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22How%20to%20generate%20unique%20file%20name%20%28QTP%2C%20VBScript%29%22%20%7D);"></div>
<p><div style="position: relative; z-index:1;"><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* Banner 468x60 */
google_ad_slot = "8933038987";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><br />
Parent page: <a title="Permanent Link to Service Functions – System (QTP, VBScript)" rel="bookmark" href="http://automation-beyond.com/2009/12/05/service-functions-system/" target="_blank">Service Functions – System (QTP, VBScript)</a></p>
<h4 style="text-align: center;">How to generate a unique file name<br />
if the file name already exists in the given folder</h4>
<p>(That often happens, if you need to create a series of files or duplicate an existing file)</p>
<p><strong>Description</strong></p>
<p>sFileName &#8211; the &#8220;original&#8221; file name or file name template<br />
sFolderName &#8211; parent folder (full path required)<br />
objParameter &#8211; Dictionary object containing <a href="http://automation-beyond.com/2009/05/24/qtp-vbscript/" target="_blank">optional parameters</a>.</p>
<ul>
<li><strong>Prefix.</strong> If you want the file name to be preceded with some text (e.g. <em>copy_</em>userdata.xls) define a prefix.</li>
<li><strong>Index.</strong> If you want to define starting number in the series (e.g. userdata<em>_100</em>.xls) use index.</li>
</ul>
<p><strong>Note.</strong> The function <em>does not</em> generate a file. It generates a file name!</p>
<p>Example. <a href="http://automation-beyond.com/2009/12/15/how-to-duplicate-existing-file/" target="_blank">Duplicate an existing file</a></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* 4 links text line */
google_ad_slot = "5367351963";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<pre class="brush: vb; title: ;">

Public Function UniqueFilename(ByVal sFileName, ByVal sFolderName, ByVal objParameter)

Dim FSO, boolRC, sTypeName
Dim index,prefix
Dim sNewName, BaseName, Extension

Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
'to check if the objParameter passed is a dictionary object
sTypeName = TypeName (objParameter)
If sTypeName &lt;&gt; &quot;Dictionary&quot; Then
Set objParameter = CreateObject(&quot;Scripting.Dictionary&quot;)
End If

'retrieving and defaulting the parameters from objParameter
prefix = objParameter.item(&quot;prefix&quot;)
If prefix &lt;&gt; &quot;&quot; Then prefix = prefix &amp; &quot;_&quot;

index = objParameter.item(&quot;index&quot;)
If Not IsNumeric(index) Then index = 1

'Separate filename and extension
BaseName = FSO.GetBaseName(sFileName)
Extension = FSO.GetExtensionName(sFileName)

'Generate filename and verify it doesn't exist

Do
sNewName = prefix &amp; BaseName &amp; &quot;_&quot; &amp; Cstr(index) &amp; &quot;.&quot; &amp; Extension
boolRC = FSO.FileExists(sFolderName &amp; &quot;\&quot; &amp; sNewName)
index = index+1
Loop Until boolRC = FALSE

Set FSO = Nothing
UniqueFilename = sNewName
End Function
</pre>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://automation-beyond.com/2009/12/15/how-to-duplicate-existing-file/" rel="bookmark" class="crp_title">How to duplicate an existing file (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2009/12/05/service-functions-system/" rel="bookmark" class="crp_title">Service Functions – System (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2007/02/21/service-functions-system-winrunner/" rel="bookmark" class="crp_title">Service Functions &#8211; System (WinRunner, TSL)</a></li><li><a href="http://automation-beyond.com/2009/12/23/gp-automation-execute-dexterity-macro/" rel="bookmark" class="crp_title">GP/QTP Automation: Execute Dexterity Macro</a></li><li><a href="http://automation-beyond.com/2009/12/09/generating-text-file-from-xml-template/" rel="bookmark" class="crp_title">Generating text file from XML template (QTP, VBScript)</a></li></ul></div>
]]></content:encoded>
			<wfw:commentRss>http://automation-beyond.com/2009/12/14/generate-unique-filename/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to separate file name and path (QTP, VBScript)</title>
		<link>http://automation-beyond.com/2009/10/25/how-to-separate-file-name-and-path/</link>
		<comments>http://automation-beyond.com/2009/10/25/how-to-separate-file-name-and-path/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 15:12:04 +0000</pubDate>
		<dc:creator>Albert Gareev</dc:creator>
				<category><![CDATA[File System Operations]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[answer]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[full]]></category>
		<category><![CDATA[name]]></category>
		<category><![CDATA[path]]></category>
		<category><![CDATA[QTP]]></category>
		<category><![CDATA[question]]></category>
		<category><![CDATA[separate]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://automationbeyond.wordpress.com/?p=1120</guid>
		<description><![CDATA[Parent page: Service Functions – System (QTP, VBScript) Use FileSystemObject.GetFile method to obtain a File object. Argument should be a full path string. Set FSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;) Check if the file exists first. When to use it? There is a variety of utility tasks related to it: get parent folder, create another file in the same folder, create another [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fautomation-beyond.com%252F2009%252F10%252F25%252Fhow-to-separate-file-name-and-path%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22How%20to%20separate%20file%20name%20and%20path%20%28QTP%2C%20VBScript%29%22%20%7D);"></div>
<p><div style="position: relative; z-index:1;"><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* Banner 468x60 */
google_ad_slot = "8933038987";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></p>
<p>Parent page: <a title="Permanent Link to Service Functions – System (QTP, VBScript)" rel="bookmark" href="http://automation-beyond.com/2009/12/05/service-functions-system/" target="_blank">Service Functions – System (QTP, VBScript)</a></p>
<p>Use <strong>FileSystemObject.GetFile</strong> method to obtain a <strong>File</strong> object.<br />
Argument should be a <a href="http://en.wikipedia.org/wiki/Full_path" target="_blank">full path</a> string.</p>
<blockquote><p>Set FSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)</p></blockquote>
<p><a href="http://automation-beyond.com/2009/10/24/how-to-check-if-file-exists/" target="_blank">Check</a> if the file exists first.</p>
<p><strong>When to use it?</strong> There is a variety of utility tasks related to it: get parent folder, create another file in the same folder, create another file with the same name, etc.</p>
<p><strong>Does file format matter? </strong>No. It could be text file, excel spreadsheet, bitmap, binary file, – anything. Just make sure you pass into both filename and extension.</p>
<p><strong>Note.</strong> Always release the object you created when you no longer need it. If you don’t do that in the long run it’ll cause you memory leakage problem.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-4470793787913935";
/* 4 links text line */
google_ad_slot = "5367351963";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<pre class="brush: vb; title: ;">

sFileName = &quot;C:\TEMP\Log145473.txt&quot;

Set objFile = FSO.GetFile(sFileName)
sName = objFile.Name
sPath = objFile.Path
sPath = Left(sPath, Len(sPath)-Len(sName))
Set objFile = Nothing
Set FSO = Nothing

'sName contains &quot;Log145473.txt&quot;
'sPath contains &quot;C:\TEMP\&quot;
</pre>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://automation-beyond.com/2009/12/05/service-functions-system/" rel="bookmark" class="crp_title">Service Functions – System (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2009/12/15/how-to-duplicate-existing-file/" rel="bookmark" class="crp_title">How to duplicate an existing file (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2009/12/14/generate-unique-filename/" rel="bookmark" class="crp_title">How to generate unique file name (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2009/10/24/how-to-check-if-file-exists/" rel="bookmark" class="crp_title">How to check if file exists (QTP, VBScript)</a></li><li><a href="http://automation-beyond.com/2009/05/27/vbscript-xml-xsl-2/" rel="bookmark" class="crp_title">Text File compare in &#8220;WDIFF&#8221; style (QTP, VBScript, XML, XSL) &#8211; Instructions and XSL script</a></li></ul></div>
]]></content:encoded>
			<wfw:commentRss>http://automation-beyond.com/2009/10/25/how-to-separate-file-name-and-path/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

