May 7, 2021 at 10:25 am
Hi all,
I have a task that costs me a lot of time
Maybe it is possible to automate this.
I have a directory (Files) with a varying number of sub directories
These subDirs are removed from time to time (back up), but I need to know what the filenames originally were.
The dir looks something like:
Files
subDir One
subsubDirOne
File ...
File ...
File ...
subsubDirTwo
File ...
File ...
subsubDirThree
File ...
File ...
File ...
File ...
subDir Two
subsubDirA
File ...
File ...
subsubsubDir ..
File ...
File ...
File ...
subsubDirB
File ...
subsubDirC
File ...
File ...
subsubDirD
File ...
subsubDirE
File ...
subsubDirF
File ...
File ...
subDir Three
etcetc
Basically it is a dir (files) with several subdirs that may of may not have other subdirs.
The number of sub dirs vary from time to time
Is it possible to create code that goes through every subdir (names dirs include blank spaces),
runs the code "Get-ChildItem -Recurse | % { $_.FullName } | Sort-Object"
and save that in the subDir... as "Contents.txt"
Thanks you in advance
Hein
May 7, 2021 at 2:26 pm
It looks like you have the code already. Just append "> Contents.txt" to the end of it.
If you don't want powershell, bat file can do it too with "dir /s > Contents.txt".
Toss either the powershell or bat file into a windows task and you should be done. If you want to keep a running history, change the > to a >>. A single > means "overwrite the contents in the file". Putting 2 of them (>>) means "append to file".
The above is all just my opinion on what you should do.
As with all advice you find on a random internet forum - you shouldn't blindly follow it. Always test on a test server to see if there is negative side effects before making changes to live!
I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.
May 7, 2021 at 5:56 pm
Thank you Brian for your reply
I am very new to Powershell and I am trying to learn it
I understand your suggestion (hopefully ?)
Except: how do I go from sub-directory to sub-directory in the (main) directory files
I would like to have each a file in each directory, with the contents of that directory
So the number of files named contents.txt matches the number of sub directorie, each in its own directory
Thanks you, Hein
May 7, 2021 at 7:09 pm
OH... you want 1 contents.txt per directory. That gets a bit more complicated, but still pretty easy in Powershell. Basically, you will need to start by getting the directory list and storing that in a variable. Then you will need a loop to go through each line of that variable. So something along the lines of:
$directoryList = Get-ChildItem -Directory -Recurse | % { $_.FullName } | Sort-Object
foreach ($directory in $directoryList) {
Get-ChildItem -File -Path $directory | % { $_.FullName } | Sort-Object > $directory\Contents.txt
}
This will put a file called "contents.txt" in each directory and subdirectory listing the FILES only. If you want files and folders, simply remove the "-File" part. If you want each Contents.txt to contain the current path and deeper, add -Recurse onto the Get-ChildItem arguments.
Does the above solve your problem or at least point you in the right direction? Again, that ">" can be replaced with a ">>" if you wish to append to contents.txt rather than overwrite it each time.
The above is all just my opinion on what you should do.
As with all advice you find on a random internet forum - you shouldn't blindly follow it. Always test on a test server to see if there is negative side effects before making changes to live!
I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.
May 8, 2021 at 11:45 am
Hello Brian,
I have made a directory (D:\TestDir) with some subDirs in it, as well as a number of files (aptly named file...).
I ran your code but keep getting errors that I cannot correct
Here's the code, together with some issues
PS C:\Windows\system32> cd D:\TestDir
$directoryList = Get-ChildItem -Directory | % { $_.FullName } | Sort-Object
foreach ($directory in $directoryList) {
Get-ChildItem -File -Path $directory | % { $_.FullName } | Sort-Object > $directory\Contents.txt
}
Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.
At line:4 char:42
+ $directoryList = Get-ChildItem -Directory <<<< | % { $_.FullName } | Sort-Object
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Get-ChildItem : A parameter cannot be found that matches parameter name 'File'.
At line:6 char:24
+ Get-ChildItem -File <<<< -Path $directory | % { $_.FullName } | Sort-Object > $directory\Contents.txt
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
I have tried a nunber of things, all went wrong. Much was my surprise to see in all subDirs a file Contents.txt but that was empy except for the line "D:\TestDir\subDir 4Vier\Contents.txt"
Can you give me an idea what I should change
Sorry for the maybe obvious question, but this is all new to me
Thanks Hein
May 8, 2021 at 2:39 pm
This was removed by the editor as SPAM
May 8, 2021 at 3:12 pm
Try this change:
$directoryList = Get-ChildItem -Directory | % { $_.FullName } | Sort-Object
foreach ($directory in $directoryList) {
$dirContents = Get-ChildItem -File -Path $directory | % { $_.FullName } | Sort-Object;
if ($dirContents -ne $null) {
$dirContents | Out-File "$($directory)\Contents.txt";
}
}
I put a check in for a directory that is empty - if no files then the file won't be created. Code has been tested and works as expected on my system. If you are still receiving errors - can you issue the command: $PSVersionTable and supply the results?
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
May 8, 2021 at 4:34 pm
Hi Jeffry,
Thanks for your reply
I used the command:
cd d:\testdir
$directoryList = Get-ChildItem -Directory | % { $_.FullName } | Sort-Object
foreach ($directory in $directoryList) {
$dirContents = Get-ChildItem -File -Path $directory | % { $_.FullName } | Sort-Object;
if ($dirContents -ne $null) {
$dirContents | Out-File "$($directory)\Contents.txt";
}
}
That gave me:
Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.
At line:4 char:42
+ $directoryList = Get-ChildItem -Directory <<<< | % { $_.FullName } | Sort-Object
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Get-ChildItem : A parameter cannot be found that matches parameter name 'File'.
At line:7 char:39
+ $dirContents = Get-ChildItem -File <<<< -Path $directory | % { $_.FullName } | Sort-Object;
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Apparently the system doesn't recognize the -directory and -file parameters
Running $PSVersionTable gave me
Name Value
---- -----
CLRVersion 2.0.50727.8806
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
FYI: I am (still) running a windows 7 system. I want to get a new computer but due to Covid-19 I am stuck to my house (health issues)
Do you think that my version is not "equipped" for this code / version of this code?
Anyway, thank you very much for your reply
Hein
PS
Is it possible to type without all these big/large whote lines?
May 8, 2021 at 4:46 pm
You are on a very old version? You need to upgrade to the latest version
https://www.microsoft.com/en-us/download/details.aspx?id=54616
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
May 8, 2021 at 4:54 pm
Thank you Jeffrey
I will download and install the files and will get back to you to inform you about the results
King regards
Hein
May 9, 2021 at 4:47 pm
Hi Jeffry
I have downloaded the files but installing them gives me problems
Or the files stops when starting to initialyse, or I get the message "The update is not applicable to your computer"
I keep working to get them installed but I may need some time, sorry
Kind regards
Hein
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply