Is there any method in vb6 to get the Subfolder name without using For Each Loop?
Most of thread suggests using For Each with FSO, but what if I want to get the subfolder name of specific Folder? e.g. Folder next to C:\Windows ?
I badly need this method to minimize the process time in my program. Simply escaping the loop isn't enough for me.
If you're asking what I think you are (how to list the subfolders of C:\Windows), the answer is no, not without looping.
The reason is that internally the VB FSO uses FindFirstFile, along with the accompanying FindNextFile and FindClose you'll find linked on that page) to iterate the subfolders of a specified folder. The loop is necessary once FindFirstFile has found the first match in order to call FindNextFile to continue retrieving the folders, and FindClose once the last match has been found and the next iteration fails.
Related
Im not scripting alot so i think this is total beginner stuff so
i have following problem, i want to move (like cut-out and paste) a folder ,that always starts with the same string but never have the same ending, to another folder.
Im working on a Windows Server that runs a Batch. The Batch is copying different Backup-Files to a Directory with a timestamp. This works fine because all the other Files and diretorys that i need to move have static names.
But im running into a problem with a folder/directory that has a dynamic name. The Folder starts always with the GUID {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx} of the Application, so the start fo the directory name is static. But at the end, the Application is adding a underline _ followed by random charaters so the directory looks like following f.E. :
{11111111-1111-1111-1111-11111111111}_ehgwuh238th
This is how my other files with the static names are handled so far:
if exist %path%\%fullstamp% move %path%\Test %path%\%fullstamp%
path contains the path to the directory that includes the files that need to be moved.
fullstamp contains a timestamp which were used to create a new directory at the beginning of the batch.
I tried something like:
move %path%\Test* %path%\%fullstamp%
move %path%\Test*. %path%\%fullstamp%
move %path%\Test*\ %path%\%fullstamp%
But none of these worked out
So like i said probably not a big deal but i still need to ask.
To summarize it:
the directory that always starts/contains with the same string need to be moved to another directoy.
Some directoy names f.E.
Test_31tß0t30
Test_3tggwqgrgwrg
Test_ksmrh82ra
Thank you in advance and sorry for my bad english.
The move command seems to not accept wildcards for source directories, so you will need a for /D loop to resolve the path first:
for /D %%I in ("%ROOT%\Test*") do move "%%~I" "%ROOT%\%fullstamp%"
I also quoted both source and destination paths in order to avoid trouble with whitespaces or other special characters.
The fact that you use a variable %path% makes me think you set it to the root path of your operation, but PATH is a reserved variable used by the system to find executables, so you should not (over-)write it. That is why I used another variable name ROOT in my code suggestion.
How can I recursively delete from a directory all its sub directories with only one file inside, and the file's name contains key word "DEL" and the file was created before 2017?
Thanks.
This is likely not possible, but you can come at least close to it:
The idea is to use find to travel through all the directories. Have a look at the -type flag, so that find picks up directories only.
With the -exec option of find, you pass the name of a program which checks, whether this is a directory to be deleted. I'm not sure whether it is safe to delete it right now (I think it is), or whether it would upset find (in which case you would just print the path of the directory to stdout, so that it can be deleted by the script which is invoking find), so this is something you have to try out.
Thr tricky part is to find whether this is a directory to be deleted. Finding the number of files in a directory can be done in different ways, and it depends on which programming language you use for this "checking program".
Matching the filename is trivial.
What won't work is finding out the creation date in case you are on Linux or Unix, because it is not stored anywhere. The closest you can get is the inode change time. If you are on Windows, you can find out the creation date.
So, is there a possible way to move Test.txt to C:\ProgramData\CsD2\Tools\("Unknown Folder Name")\data\per Using command prompt?
using foxidrives solution for your previous question for detecting the correct directory, then just
move test.txt "%folder%\"
Short answer: yes. Not quite sure what the situation is that has left only the middle part of your path unknown, and the need to use the comnand line, but I have encountered similar cases on Linux and expect the algoirthm can be adapted to Windows commands. It's possible to do this by hand rather than writing a shell script, but it's up to you and your skills.
Permissions matter. Make sure you elevate yours enough to read and write in Tools before continuing.
First, change directory to C:\ProgramData\CsD2\Tools\
Presumably there are many items here. Some may be "hidden," so list the contents of this directory and be sure to include an option to show hidden files and folders. If you can, restrict the search to directories only.
It's tempting to display contents recursively in the above step. It's up to you, but I find it makes the output cluttered without a script to do the rest of the work.
Now it's time to search for the subfolder set that theoretically only exists in your target folder. Suppose Tools contains the directories fldr1, fldr2, and fldr3. Use your command to list a directory's contents with the path "fldr1\data\per", then use "fldr2\data\per", and so on until it doesn't return an error. Per may be empty, but that should look different from the path not found error.
Now you've found the name of your mystery folder. Write it down for future reference.
At thus point, you know the path to Test.txt, and the full path to the destination directory. Do a move command to relocate Test.txt, and you're done. I like to relist the contents of the target directory after to be comfortable that it arrived.
While in a folder with lots of files, one can select many and rename only one. This one will get the name NewName (1) and the rest will follow as NewName (2) etc..
Is there a way to use this algorithm?
I mostly interested in using WinApi methods in general. It is easy to implement this specific algorithm. I don't know how to dig into explorer.exe and see what method it uses but probably it would be something reusable.
I mostly use c# but any language example would be accepted.
Not with a single function call, no. But you can loop through the files one at a time using SHFileOperation() with the FOF_RENAMEONCOLLISION flag to rename each file to the same target filename so Windows will generate its own unique filenames.
As pointed out by Remy Lebeau I came up with the official way to do it.
IFileOperation::RenameItems
Declares a set of items that are to be given a new display name.
All items are given the same name.
...
If more than one of the items in the collection at pUnkItems is
in the same folder, the renamed files are appended with a number in
parentheses to differentiate them, for instance newfile(1).txt,
newfile(2).txt, and newfile(3).txt.
Here is the referenced link.
This also answers my question on where to start to using windows shell api to do stuff. The answer is here.
Yes it is possible see here http://blog.gadodia.net/stupid-windows-trick-mass-renaming/ I used this to oganized and Number files in mass
I work with the rather finnicky Oracle Business Intelligence software and we often have issues that entail, clearing out specific data on users systems, and then synchronizing with the server to pull down the data again. I've got a vbs script that I'm working on that removes key directories, and renames others and stops services etc.
Where I'm stuck is on one specific directory. Using FileSystemObject, what would be the easiest way to remove every single file within a directory with the exception of a single folder?
So, for this specific example, I have C:\OracleBIData\sync\config
Where I want to delete everything inside of the "sync" directory, with the exception of the config directory. Any takers?
Snippet:
Option explicit
Const folderspec = "C:\OracleBIData\sync"
Const excludeFolder = "C:\OracleBIData\sync\config"
deleteSubFolders CreateObject("Scripting.FileSystemObject").GetFolder(folderspec), excludeFolder
Public Sub deleteSubFolders(byRef MyFolder, exclFolder)
Dim sf
For Each sf in MyFolder.SubFolders
If not (lCase(sf.Path) = lCase(exclFolder)) Then
deleteSubFolders sf, exclFolder
sf.Delete
End If
Next
End Sub
It will not delete folders under the excludeFolder.
Brute force is all I can think of.
Walk through the directory item but item and delete it if it is not config. Or if this directory has lots and lots and lots of files, first run through deleting a*., b.*, d*.* 25 times, and then walk through the rest of the items.