Im writing a batch file, I need to get the parent folder of this bat file. Is it possibile?NB I mean the parent folder of the batch file not of the current directory of the prompt calling that batch.
However, it's bad because it does rely on PowerShell existing within your environment path, and it relies on no other script modifying your temporary storage file between your script writing to it and reading it.
Looking at your first few attempts, the one thing you don't need to be doing is worrying about the -Recurse parameter since it has no meaning when moving a file, while directory moves implicitly cause nested files and directories to move - barring where a file/directory of the same name already exists in the destination beforehand.
So, what we want to do is iterate one level (i.e. not recursively) below each parent (where "parent" is a directory object, not a file object) and move that object up to the root level, and if that happens without any errors, then remove the parent directory.
This script has basic safety built in but it's nothing profound, as I'm leveraging the way in which Move-Item is designed. Specifically, I'm referring to the behaviour described above about not moving a file or directory if it already exists in the destination.
So, the only additional safety check I've added is not removing the parent directory if any errors were thrown from the prior Move-Item command. That allows you to inspect the conflicting files and avoid data loss through unconditionally copying duplicate file(s) over the top of ones already within the destination.
In the output, I've written the issue to the warning channel, but I've also written a summary to the pipeline. This allows you to assign the failure results to a variable or simply pass them down another pipeline chain for further process (such as piping out to a CSV file using Export-Csv - just to list one option).
Inside the Test Folder I created 4 Parent folders.
Inside Parent 1 I created 2 child folders (as a parent may have none or many child folders)
Inside Parent 3 - 4 I created one child folder in each.
When I checked the child folders, the code had not removed any of them.
in
01 Parent [For] a (Test) 1\Child 1 [For] a (Test)
It had deleted all of the test files but it did not copy or move them up a level to the parent folder, it had simply deleted them.
And here's an example of me using handle to find any references to the word "forum", since I'm interested in seeing what currently has access to the "forum" directory below D:\Data\Temp on my computer.
If you're not interested in finding which specific process(s) has the lock, you can simply close everything - including Explorer windows like the one from your screenshot, then re-open PowerShell and start again.
The key takeaway is that it's not the script that has the issue in this specific case, as nothing can move a resource locked by another process. In other words, the content not moving is an expected outcome for this specific error.
I've reproduced the "process lock" scenario you've run into, and the outcome matches expectations on my side - keeping in mind none of this requires administrative access (I'm always operating as an unprivileged user in accordance with best practice).
Some FTP clients like FileZilla or WinSCP are using two dots (..) for navigating to parent directories. rclone FTP does not handle it correctly (unlike servers like vsftpd or FileZilla Server), so going to the parent directory by clicking two dots ends with an error.
Use the directory resource to manage a directory, which is a hierarchy of folders that comprises all of the information stored on a computer. The root directory is the top-level, under which the rest of the directory is organized. The directory resource uses the name property to specify the path to a location in a directory. Typically, permission to access that location in the directory is required.
directory 'name' do group String, Integer inherits true, false mode String, Integer owner String, Integer path String # defaults to 'name' if not specified recursive true, false rights Hash action Symbol # defaults to :create if not specifiedend
A string or ID that identifies the group owner by group name or SID,including fully qualified group names such as domain\group orgroup@domain. If this value is not specified, existing groupsremain unchanged and new group assignments use the default POSIXgroup (if available).
A quoted 3-5 character string that defines the octal mode. Forexample: '755', '0755', or 00755. If mode is not specifiedand if the directory already exists, the existing mode on thedirectory is used. If mode is not specified, the directory doesnot exist, and the :create action is specified, Chef Infra Clientassumes a mask value of '0777', and then applies the umask for thesystem on which the directory is to be created to the mask value.For example, if the umask on a system is '022', Chef Infra Clientuses the default value of '0755'.
Microsoft Windows: A quoted 3-5 character string that defines theoctal mode that is translated into rights for Microsoft Windowssecurity. For example: '755', '0755', or 00755. Values up to'0777' are allowed (no sticky bits) and mean the same in MicrosoftWindows as they do in UNIX, where 4 equals GENERIC_READ, 2equals GENERIC_WRITE, and 1 equals GENERIC_EXECUTE. Thisproperty cannot be used to set :full_control. This property has noeffect if not specified, but when it and rights are bothspecified, the effects are cumulative.
A string or ID that identifies the group owner by user name or SID,including fully qualified user names such as domain\user oruser@domain. If this value is not specified, existing ownersremain unchanged and new owner assignments use the current user(when necessary).
Microsoft Windows only. The permissions for users and groups in a Microsoft Windows environment. For example: rights , , where specifies the rights granted to the principal, is the group or user name, and is a Hash with one (or more) advanced rights options.
The remote_directory resource can be used to recursively create thepath outside of remote directory structures, but the permissions ofthose outside paths are not managed. This is because the recursiveattribute only applies group, mode, and owner attribute values tothe remote directory itself and any inner directories the resourcecopies.
The rights property can be used in a recipe to manage access controllists (ACLs), which allow permissions to be given to multiple users andgroups. Use the rights property can be used as many times asnecessary; Chef Infra Client will apply them to the file or directory asrequired. The syntax for the rights property is as follows:
Use to specify a group or user. The principal can be specified byeither name or SID. When using name, this is identical to what isentered in the login box for Windows, such as user_name,domain\user_name, or user_name@fully_qualified_domain_name. Whenusing a SID, you may use either the standard string representation ofa SID (S-R-I-S-S) or one of the SDDL string constants. ChefInfra Client does not need to know if a principal is a user or agroup.
A hash that contains advanced rights options. For example, therights to a directory that only applies to the first level ofchildren might look something like:rights :write, 'domain\group_name', :one_level_deep => true.
Specify how permissions are applied to children. Possible values: true to inherit both child directories and files; false to not inherit any child directories or files; :containers_only to inherit only child directories (and not files); :objects_only to recursively inherit files (and not child directories).
Indicates whether a permission is applied to the parent directory. Possible values: true to apply to the parent directory or file and its children; false to not apply only to child directories and files.
By default, a file or directory inherits rights from its parentdirectory. Most of the time this is the preferred behavior, butsometimes it may be necessary to take steps to more specifically controlrights. The inherits property can be used to specifically tell ChefInfra Client to apply (or not apply) inherited rights from its parentdirectory.
Control the phase during which the resource is run on the node. Set to true to run while the resource collection is being built (the compile phase). Set to false to run while Chef Infra Client is configuring the node (the converge phase).
A resource may notify another resource to take action when its statechanges. Specify a 'resource[name]', the :action that resourceshould take, and then the :timer for that action. A resource maynotify more than one resource; use a notifies statement for eachresource to be notified.
A resource may listen to another resource, and then take action if thestate of the resource being listened to changes. Specify a'resource[name]', the :action to be taken, and then the :timer forthat action.
In this case the subscribes property reloads the nginx servicewhenever its certificate file, located under/etc/nginx/ssl/example.crt, is updated. subscribes does not make anychanges to the certificate file itself, it merely listens for a changeto the file, and executes the :reload action for its resource (in thisexample nginx) when a change is detected.
A guard property can be used to evaluate the state of a node during theexecution phase of a Chef Infra Client run. Based on the results of thisevaluation, a guard property is then used to tell Chef Infra Client ifit should continue executing a resource. A guard property accepts eithera string value or a Ruby block value:
A guard property is useful for ensuring that a resource is idempotent byallowing that resource to test for the desired state as it is beingexecuted, and then if the desired state is present, for Chef InfraClient to do nothing.
3a8082e126