I'm trying to delete some files where their name start's with "Archive-Security*" and older than 5 days.
I've used the following cmd but it returns "ERROR: No files found with the specified search criteria."
forfiles -m "Archive-Security-*" -d -5 -c "cmd /c del #path"
I am sure you have either solved this or it is no longer a problem, but here would be a way to accomplish the task. This can be used in a .bat file script.
powershell -NoProfile -Command ^
"Get-ChildItem -File -Filter 'Archive-Security-*' |" ^
"ForEach-Object {" ^
"if ((($(Get-Date) - $_.LastAccessTime).Days) -gt 5) {" ^
"Remove-Item -Path $_.FullName -WhatIf" ^
"}" ^
"}"
Of course, using PowerShell directly would be easier.
Get-ChildItem -File -Filter 'Archive-Security-*' |
ForEach-Object {
if ((($(Get-Date) - $_.LastAccessTime).Days) -gt 5) {
Remove-Item -Path $_.FullName -WhatIf
}
}
Related
I want to remove folder that existe in many project in the same workspace folder like below :
Workspace :
/project1/generated
/project2/generated
I tried the code below,but deosn't work :
del /q "workspace\(*)\generated\*"
FOR /D %%p IN ("workspace\(*)\generated\*.*") DO rmdir "%%p" /s /q
Please can someone help ?
Thanks in advance .
If you are deleting the directory (and all files it contains), then the following will work. When you are confident that the correct directories will be deleted, delete the -WhatIf from the Remove-Item command.
Get-ChildItem -Directory -Path 'C:\workspace\project*\generated' | Remove-Item -Recurse -WhatIf
If you must do it from a cmd.exe shell or .bat script, you could:
powershell -NoLogo -NoProfile -Command ^
"Get-ChildItem -Directory -Path 'C:\workspace\project*\generated' | " ^
"Remove-Item -Recurse -WhatIf"
I need to find the path of every folder that contains the words "test" or "Test" or "TEST" in a specific location. The file tree is huge.
This is to remove every folder that contains "test". I've tried using
the findstr function.
findstr /i "test"
I expect to get every folder path that contains "test"
The list of directories can be generated and iterated over using a FOR loop. When you are satisfied that the correct directories will be deleted, remove the echo from the line containing rmdir.
#echo off
pushd \
for /f "delims=" %%a in ('dir /s /b /a:d "*test*"') do (
echo if exist "%%~a" (rmdir /s /q "%%~a")
)
popd
If you wanted to push ahead into Powershell, which Microsoft says is the future, something like this might work. When you are satisfied that the correct directories will be removed, remove the -WhatIf from the Remove-Item cmdlet.
=== Remove-TestDirectories.ps1
$previousdir = ":" # set to impossible filesystem name
Get-ChildItem -Directory -Recurse -Path "C:\" -Filter "*test*" -Force -ErrorAction SilentlyContinue |
Sort-Object -Property FullName |
ForEach-Object {
#"{0} and {1} and {2}" -f #($previousdir, $_.FullName, $_.FullName.StartsWith($previousdir))
if (-not $_.FullName.StartsWith($previousdir)) {
$previousdir = $_.FullName
if (Test-Path -Path $_.FullName) { Remove-Item -Path $_.FullName -Recurse -WhatIf}
}
}
This can be run from a cmd.exe shell.
powershell -NoLogo -NoProfile -File Remove-TestDirectories.ps1
i want to create a txt file, containing absolute paths for my robocopy backup. However, i only want to backup folders which have specific names in it.
This would be my source folder:
\\\fileserver\Projects
which contains folders like
20090-Miller-Georg
20094-Johnson-Susan
20097-Miller-Sarah
20125-Olston-John
20130-Johnson-Alex
....
Now here comes the tricky part: Let's say, i only want to list folders which contain e.g. Miller and Johnson, so my list would be something like this:
\\\fileserver\Projects\20090-Miller-Georg
\\\fileserver\Projects\20094-Johnson-Susan
\\\fileserver\Projects\20097-Miller-Sarah
\\\fileserver\Projects\20130-Johnson-Alex
...
In a batch script, dir does not enable this kind of sorting. Is there another way?
Here is something you can put in the .bat file script. It will produce UNC paths.
SET "BASEDIR=\\fileserver\Projects"
powershell -NoProfile -Command ^
"Get-ChildItem -Directory -Path "%BASEDIR%" |" ^
"ForEach-Object {" ^
"if ($_.Name -cmatch '-Miller-|-Johnson-') {" ^
"$_.FullName" ^
"}" ^
"}"
Of course, this is less cryptic when using PowerShell directly.
$basedir = '\\fileserver\Projects'
Get-ChildItem -Directory -Path $basedir |
ForEach-Object {
if ($_.Name -match '-Miller-|-Johnson-') {
$_.FullName
}
}
I running an xcopy command to transfer from one file to the other.
xcopy /s "c:\users\documents\thisfile.txt" "d:\otherfiles\1.2.1"
I'd like to be able to just copy the file into the most recent folder in the otherfiles directory rather than hard coding it every time a new version folder is created. These are versions numbers and these tend to just increase.
Is this entirely possible?
If you wanted to do this in PowerShell, it is possible. This would require PowerShell 3.0 or higher. It can be done with 2.0, but would require changes. Hopefully, you are on or can upgrade to a modern-day version of PowerShell.
When you are confident that the file will be copied correctly, remove the -WhatIf from the Copy-Item cmdlet.
$fn = 'C:/src/t/xxx.txt'
$destbasedir = 'C:/src/t/lastdir'
Get-ChildItem -Directory -Path $destbasedir |
Sort-Object -Property Name |
Select-Object -Last 1 |
ForEach-Object { Copy-Item -Path $fn -Destination $_.FullName -Whatif }
This could be put into a .bat file script.
SET "FN=C:\src\t\xxx.txt"
SET "DESTBASEDIR=C:\src\t\lastdir"
powershell -NoProfile -Command ^
"Get-ChildItem -Directory -Path %DESTBASEDIR% |" ^
"Sort-Object -Property Name |" ^
"Select-Object -Last 1 |" ^
"ForEach-Object { Copy-Item -Path "%FN%" -Destination "$_.FullName" -Whatif }"
Ok, it is possible to check the versions of the directories, but that will take a bit more code as we cannot simply remove the dots to get a numeric value and compare to the next. The reason being, considering versions 1.2.3 and 1.23 if we remove the dots to make it a matchable numeric value, both these will end up being being 123 therefore each version section would need to be tested.
However, based on your comments to my questions, you create new versions as folders, and therefor it ia sortable by date, so simply run a dir command and sort by created date. It will set the latest folder as the variable you need:
#echo off
for /f "delims=" %%i in ('dir /b /ad /o:d D:\otherfiles') do set "myvar=%%i"
xcopy /s "c:\users\documents\thisfile.txt" "d:\otherfiles\%myvar%"
I'm trying to perform a command on the alphabetically first file in a set of subdirectories using
for /R "parentfolder" %%a in (*.doc) do "%winword%" /mRTF "%%a"
in a batch file. The actual command and macro work fine, but it performs the action on each file in the directories instead of just the ones I want.
Is there a way to set the -on flags in this context?
Any help will be highly appreciated.
Thank you that got me a good bit further.
Currently I'm struggling with opening word and running the Macro through powershell. My code looks like this now:
$word = new-object -comobject word.application
$path = 'C:\Users\---\Desktop\Download\'
$folders = Get-ChildItem -Path $path -recurse | SELECT Attributes, Name | where-Object{ $_.Attributes -eq 'Directory'} | ForEach-Object{$_.name}
foreach ($folder in $folders){
Get-ChildItem -Path $path$folder |
Select-Object -First 1 |
ForEach-Object { notepad $_.FullName }
}
I try to change notepad to word and call the macro but so far no luck
Here is a way to do it in PowerShell. I used notepad.exe, but you can use whatever you want.
Get-ChildItem -Path '../' -Filter '*.doc' |
Select-Object -First 1 |
ForEach-Object { notepad "$_.FullName" }
If you -must- do this from a cmd shell .bat script, you could:
powershell -NoProfile -Command Get-ChildItem -Path '../' -Filter '*.doc' ^| ^
Select-Object -First 1 ^| ^
ForEach-Object { notepad \"$($_.FullName)\" }
HT to mklement0 for the escape.
Based upon your example code and subsequent comments, the below example may run your Word macro on each of the files you require:
SetLocal EnableDelayedExpansion
For /D %%A In ("parentfolder\*") Do (Set "_="
For /F "Delims=" %%B In ('Dir/B/A-D-S-L/O-N "%%A\*.doc" 2^>Nul'
) Do Set "_=%%B"
If Defined _ "%winword%" /mRTF "%%A\!_!")