So for my task i have to find all files on my C: partition that start with the word 'printer'and they have to have an extension that contains exactly 3 letters i also have to do the same for the .dll extension only
That is a lot of work for PowerShell (my old opinion). :)
If you do it in the native operating system it is:
dir printer*.??? /s /A-D
or
dir printer*.* /s /A-D
(In both cases from the root as /s denotes subfolders. You could add some other stuff for hidden files or folders but /? will give that to you. /A-D removes directories.)
You can pull that in to PowerShell with: cmd /r dir printer*.??? /s /A-D
Now if you do want to use PowerShell Natively you can read up but it'll be like this:
Get-ChildItem -Path C:\ -Include printer*.??? -File -Recurse -ErrorAction SilentlyContinue
You can swap out *.??? for *.dll in the code to get the results you want.
Related
I'm looking for a command using cmd.exe (Win 10) that will list all files in a folder and its sub-folders, alphabetically, irrespective of the paths, and that will show the filenames only (no paths).
The commands that I'm familiar with (including, for example, "dir ..\samplefolder /b /s /A-D /o:n > filelist.txt") all include the paths in the output, and so are not what I'm looking for.
Thank you.
(for /r "c:\startfolder" %%A in (*) do echo %%~nxA)|sort
(this is batch file syntax; for use directly on the command line, replace every %% with just %)
for /r loops recursively over all (non-hidden) files.
%%~nxA shows name and extension only (if you want just the name without extension, use %%~nA)
See for /? for more information on those modifiers.
If the machine is on the current PowerShell 5 or higher, you could use:
(Get-ChildItem -Recurse -File -Path '..\samplefolder').Name |
Sort-Object |
Out-File -PSPath 'filelist.txt' -Encoding ascii
In a .bat file script.
>"filelist.txt" powershell -NoLogo -NoProfile -Command ^
"(Get-ChildItem -Recurse -File -Path '..\samplefolder').Name | Sort-Object"
If the machine does not have a current PowerShell, it should be upgraded or use:
>"filelist.txt" powershell -NoLogo -NoProfile -Command ^
"(Get-ChildItem -Recurse -Path '..\samplefolder'|" ^
"Where-Object { -not $_.IsContainer}).Name |" ^
"Sort-Object"
So suppose I know there's some file(s) (all with the same extension) in my directory which I want to run only I don't know their name.
I could execute the following to get all the relevant file names (.ext is just an arbitrary extension here)
dir /b /a-d *.ext
And then I could call
start
on all those files.
But I'd like to skip a step and simply pipe the output from dir into start.
I've tried
dir /b /a-d *.ext | start
but all that does is open an empty shell. Any suggestions?
For completeness, PowerShell:
Get-ChildItem *.exe | ForEach-Object { Start-Process $_ }
shorter (with aliases):
gci *.exe | % { start $_ }
you can
dir /b /a-d *.ext > start.bat
This will generate a start.bat file. Then:
call start.bat
By running the file you will run all apps.
I am having some issues with OneNote overpopulating the drive with .onetoc2 files. I need a script or cmd command that deletes these files only if the folder that it's contained in does not have a .one file. I need this run for the entire directory.
I have a delete prompt that deletes all the files but I don't know how to get the conditional aspect of it accomplished.
DEL /S /Q c:\Folders \*.onetoc2
something like this could work in powershell
$folder
if (!(dir $folder *.one)) {
dir $folder *.onetoc2 | % {del $_.FullName -WhatIf}
}
for /f "delims=" %A in ('dir /b "c:\folder\*.onetoc2"') do if not exist "%~dpA%~nA.one" echo del "%A"
Use %%A in batch. Remove the echo statement to allow it to delete.
I have a large volume of files organized in a very hierarchical folder structure. In this structure, the file that I care about is always located in the lowest level of the folders. As such, I'd like to flatten the directory so that it's easier to access the files that I care about. However, I'd like to preserve the 2 higher levels (Person & Project) of the folder structure.
Here's an example of the EXISTING folder directory:
Directory
Tom
Project 1
Subfolder Level A
FileA
FileB
Project 2
Subfolder Level C
FileC
FileD
Jerry
Project 1
Subfolder Level E
FileE
Here's an example of the DESIRED folder directory:
Directory
Tom
Project 1
FileA
FileB
Project 2
FileC
FileD
Jerry
Project 1
FileE
I have tried doing something like this, however this flattens all of the files into a single directory:
for /r %f in (*) do #copy "%f" .
However, this produces:
Directory
FileA
FileB
FileC
FileD
FileE
I'd appreciate any guidance that you can offer. Thanks a lot!
Here is a Powershell approach. It gets a list of the folders at the level that you want. Then it moves all the sub files up to that level. it will also remove the sub folders.
$Rootfolder = Dir directory\*\* -Directory
ForEach($folder in $Rootfolder)
{
Dir $folder.fullname -Recurse -File | Copy-Item -Destination $folder.fullname
Dir $folder.fullname -Recurse -Directory | Remove-Item -Force -Recurse -WhatIf
}
If you want it to delete, remove the -WhatIf from the last line.
Throw a couple of extra for loops around the one that works then.
e.g. First change to the name folder, then the project folder, looping through both levels.
for /D %n in (*) do (
pushd %n
for /D %p in (*) do (
pushd %p
for /r %f in (*) do #copy "%f" .
popd
)
popd
)
If you put this in a bat file, remember to replace % with %%
#echo off
pushd yourDirectory
for /d %%A in (*) do for /d %%B in ("%%A\*") do for /d %%C in ("%%B\*") do (
pushd "%%C"
for /r %%F in (*) do move /y "%%F" "%%B" >nul
popd
rd /q /s "%%C"
)
popd
%%A contains something like "yourDirectory\Tom"
%%B contains something like "yourDirectory\Tom\Project 1"
%%C contains something like "yourDirectory\Tom\Project 1\subdirectory1"
%%F contains a file to move, to any depth
At first I thought I could eliminate PUSHD/POPD and use
for /r "%%C" %%F in (*) do ... THIS DOES NOT WORK
But that doesn't work - the value after /R cannot use a FOR variable or delayed expansion because of how the command is parsed.
I tweaked #ScottC's answer and used the following code:
for /D %%n in (*) do (
pushd %%n
for /D %%p in (*) do (
pushd %%p
for /r %%f in (*.ppt) do (
move "%%f" "[ROOT_PATH_THAT_I_WANT]\%%n\%%p".
)
popd
)
popd
)
I ran this solution as a .bat file, which is why I used %% instead of %.
%%n = name (aka C:\Directory\Name)
%%p = project (aka C:\Directory\Name\Project)
%%f = file to be moved (recursively drilling through the folders and moving them up to the project level)
Ultimately, I wasn't able to get #dbenham's suggestion of deleting the empty folders to work, so I ended up using this utility: http://www.jonasjohn.de/red.htm. So far it seems pretty intuitive and like it's taking care of the problem without much effort from me :)
Thanks for the help everybody!
I am trying to run this command via powershell on every computer on my network.
I am running into a problem with Temporary Internet Files, providing too many false positives.
Can anyone suggest a way to improve this command?
dir C:\Users\ /S /B | findstr /i "".t.st."" > "C:\test.txt"
One suggestion, was
dir C:\Users\ /S /B | findstr /i "".t.st."" > "C:\test.txt"
dir C:\Users\ /S /B | findstr /i "".t.st."" | findstr ""Temporary Internet Files"" > "C:\test2.txt"
fc C:\test.txt C:\test2.txt > C:\results.txt
But running through tests, it didn't give me the results I was looking for. I still had duplications. Or it would say the files are too different.
Thanks!
If you are running this through PowerShell then why not use PowerShell commands? Your examples are using the legacy DIR command. You could use Get-ChildItem instead. You can use -Exclude to skip a folder. You could use -filter or -include to find the files you want. Worse case is to get the files and then pipe to where object perhaps using a regex pattern to filter out the files you want. I can't tell from your commands what files you are looking for.