I have a directory that contains .exe executable and required input files.
For each individual request, a separate directory (of the format Output_) is created in the same path and the respective output files are directed there.
Now i am in need of purging the older output directories. Can somebody explain how to achieve this in windows.
I went through the forfiles documentation and other stack overflow answers but did not find any information on providing name pattern for folders that need to be deleted although this option is available for file deletion.
I am trying the below code. But i want to specify the folder pattern as well so that i do not delete other files.
forfiles /p "C:\work\Analysis\" /d +7 /c "cmd /c IF #isdir == TRUE rd /S /Q #path"
Here an example in Powershell
$Now = Get-Date
$Hours = "4"
$TargetFolder = **!Folder in which old data exists!**
$LastWrite = $Now.AddHours(-$Hours)
$Folders = get-childitem -path $TargetFolder |
Where {$_.psIsContainer -eq $true} |
Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($Folder in $Folders)
{
write-host "Deleting $Folder" -foregroundcolor "Red"
Remove-Item $TARGETFOLDER\$Folder -recurse -Force
}
Below command did the job for me. /M works with directories as well. Thanks to JJS's answer on https://superuser.com/q/764348
forfiles /P C:\work\Analysis\ /M guid_* /d +5 /C "cmd /c if #isdir==TRUE rmdir /s /q #file"
Related
I have a folder and many subfolders containing videos. One subfolder is named Screenshots and has a folder structure that mirrors the video subfolders. Each video has one jpg screenshot: filename.ext.jpg. Some of those jpg files are no longer needed as the corresponding videos have been deleted.
d:\folder1
- video1.mp4
- video2.mkv
d:\folder2
d:\folder3
d:\screenshots\folder1
- video1.mp4.jpg
- video2.mkv.jpg
d:\screenshots\folder2
d:\screenshots\folder3
I created this to find and delete the outdated jpg's. It saves a list of screenshots with full paths, minus the .jpg, then removes the screenshot folder from the path resulting in a list of paths to the video files.
It is then supposed to return a list of all video files that no longer exist It does that, but also lists many files that are still present.
Looking for a way to fix this, or a smarter way to do the task.
Thank you.
set screenshotlist="%temp%\screenshot_work\screenshots.txt"
set folder=D:\screenshots
mkdir %temp%\screenshot_work
:: get list of screenshots without .jpg extension
for /f "delims=" %%f in ('dir %folder% /s /a-d /b') do echo %%~dpnf >>%screenshotlist%
:: edit screenshots.txt, remove "screenshots\"
call jrepl "screenshots\\" "" /f %screenshotlist% /i /o -
:: check if files exist in D:\
for /f "usebackqdelims=" %%f in (%screenshotlist%) do (if not exist %%f echo %%f needs to be deleted)
rd /s /q %temp%\screenshot_work
pause
The way I have understood your question was that you would like to delete the ".jpg" files within the Screenshots directory that do not have a corresponding video file in the Video directory. A corresponding video file would be the name of the screenshot without the ".jpg" extension.
Open PowerShell ISE
Paste the code into the script pane
Modify $Video_Directory and $Screenshot_Directory
Press Play
$Video_Directory = "C:\temp4\Video"
$Screenshot_Directory = "C:\temp4\Screenshot"
$Date = Get-Date -UFormat "%d %b %Y %H%M%S"
$Transcript_Directory = "C:\POSH_Transcripts"
$Transcript = "$Transcript_Directory\Transcript_$($Date)"
New-Item -ItemType "Directory" -Path $Transcript_Directory
Start-Transcript -Path $Transcript -Append
Get-ChildItem -Path $Screenshot_Directory -File -Recurse -Include "*.jpg" | ForEach-Object {
$Video = $_.Name.Replace(".jpg", "")
If (Test-Path "$Video_Directory\$Video"){
Write-Host -ForegroundColor Green "The video ($($_.Name.Replace(`".jpg`", `"`"))) for the screenshot ($($_.Name)) exists."
}
Else{
Write-Host -ForegroundColor Red "The video for screenshot $($_.Name) does not exist. Removing screenshot."
Remove-Item $_.FullName -Force -Verbose
Write-Host -ForegroundColor Green "$($_.Name) has been removed"
}
}
Stop-Transcript
Start-Process notepad.exe $Transcript
I have multiple zip folders with unique names but the files within the folders all have the same name. I need to rename the text files to match the folder names. I also need those renamed text files to be moved to the working directory.
For example, I have the following:
name1.zip decompresses to name1\AncestryDNA.txt but need to be name1.txt.
name2.zip decompresses to name2\AncestryDNA.txt but need to be name2.txt.
I used 7-Zip 19.00 to unzip the files. I decompressed the files because I also need to remove lines starting with "#" from each decompressed text file for use in another program.
I start with a windows batch file the runs the following on my Data directory that contains my zip files:
echo Unzipping the files...
forfiles /p Data /m *.zip /c "cmd /c 7z x *.zip -o* -y"
echo Moving zip files to storage...
forfiles /p Data /m *.zip /c "cmd /c move #file ..\StorageZipFiles"
echo Trimming all except header from AncestryDNA text files...
forfiles /p Data /s /m *.txt /c "cmd /c sed -i '/#/d' #file"
These work but I can't get the rename function to work.
I tried to rename the files before decompressing...
forfiles /p Data /s /m *.zip /c "cmd /c 7z rn #file #fname\AncestryDNA.txt #fname\#fname.txt"
and...
forfiles /p Data /s /m *.zip /c "cmd /c 7z a rn #file #fname\AncestryDNA.txt #fname\#fname.txt"
and after decompressing...
forfiles /p Data /s /m *.txt /c "cmd /c ren #file #fname.txt"
but I have not been successful with carrying the name of the folder over to the text file within the folder.
When trying to rename the files after decompressing, the command prompt does not produce an error message. It keeps the old file name of AncestryDNA.txt nested within the folders name1, name2, etc.
Another way of renaming the files after decompression is this powershell script.
Get-ChildItem -recurse -path Data *.txt | rename-item -path {$_.fullname} -newname {$($_ | split-path | split-path -leaf ) + ".txt"} -whatif
This has two interleaved powershell pipelines, one gets all the right directories and the second pipeline extracts the name of the folder used to rename the file.
Note that I put a -whatif at the end of this for safety. If the output looks good then remove the -whatif to actually change the filenames. It is still best to test this on test data before using on real data.
I'm new to PowerShell, and am trying to move a few excel spreadsheets into a folder titled "old folder" using the forfiles command. PowerShell says that the files have been moved, but when I check the folder they're not there. They aren't in their original location either.
forfiles /P C:\Users\NewUser\Desktop /M *.xlsx /D +27/05/2019 /C 'cmd /c move #file C:\Users\NewUser\Desktop\"old folder"\'
I tried changing the folder name to a single word "oldfolder" and it worked just fine. What am I doing wrong?
There seems to be a problem with the syntax of your /C parameter. As far as I can tell the argument should be enclosed in double-quotes. And because of the space in the file name that should also be enclosed in double-quotes. Quotes within quotes should be escaped with backslash.
I would suggest this:
forfiles /P C:\Users\NewUser\Desktop /M *.xlsx /D +27/05/2019 /C "cmd /c move #file \"C:\Users\NewUser\Desktop\old folder\""
As forfiles has it's idiosyncrasies even in cmd, I wouldn't drag that along to PowerShell.
IIUR that translates to:
Get-ChildItem 'C:\Users\NewUser\Desktop\*.xlsx' -File |
Where-Object LastWriteTime -ge [DateTime]::Today |
Move-Item -Destination 'C:\Users\NewUser\Desktop\old folder\'
Not sure if you meant that to be /D -27/05/2019 => -lt or -le instead of -ge
Using my below given script, I simply wanted to cut-paste all the .dat files created today from source to destination, where source is a network path.
#echo off
set datetime=%date:~0,2%-%date:~3,2%-%date:~6,4%_%time:~0,2%-%time:~3,2%
mkdir "D:\data\Backup\%datetime%"
net use L: \\10.xx.xx.xxx\shared\files /persistent:no
set source=L: \\10.xx.xx.xxx\shared\files
forfiles /P "%source%" /M *.dat /D +0 /C "cmd /c move #path D:\data\Backup\%datetime%"
net use L: /delete /y
BUT..its throwing ERROR: The directory name is invalid.
I am not getting why FORFILESis not accepting my network path as a source.
Please can somebody help me out here ?
I have no issue in using powershell as well.
In PowerShell:
$src = '\\10.xx.xx.xxx\shared\files'
$dst = "D:\Data\Backup\$(Get-Date -f 'yyyyMMdd')"
mkdir $dst
Get-ChildItem $src -File | Where {$_.LastWriteTime -gt (Get-Date).Date} | Copy-Item $dst
Ok, so I have a large amount of ebooks and other files that I would like to sort into folders by Author name. The way I would normally do this in Linux will not work on my Windows 8 laptop. I've been using Powershell, but if there is a better way in Python or by using a bash script I am willing to use whichever can get this done the easiest way. I need it to work recursively because there are many subfolders, (a lot are sorted by genre). What I have tried so far is:
mkdir 'C:\Users\Amanda\Downloads\BOOKS\Peter Straub'
Copy-Item 'C:\Users\Amanda\Downloads\*Peter Straub*' -recurse `
'C:\Users\Amanda\Downloads\BOOKS\Peter Straub'
Unfortunately for me, that only looks for directories named peter straub. And if i change it to ~\Downloads*Peter Straub*.* it just doesn't work. I tried using the -match and -contains but they failed to work as well. I am relatively new to scripting in windows 8 and would really appreciate anyones help. I would like to implement the read-host -prompt so I don't have to edit the script everytime i run it. And I would like it to actually MOVE the file not just copy but Move-Item doesn't have a recurse option. So could I somehow have the script copy and then delete the original. I tried this also:
$author = (read-host -prompt 'Enter Name')
mkdir 'C:\Users\Amanda\Downloads\BOOKS\$author'
Copy-Item 'C:\Users\Amanda\Downloads\*$author*.*' -Recurse | Remove-Item
But this also did not work. I would love to get something like this to work in an automated batch.
So basically I want to pick an authors name, make a new directory using that authors name, then search and move all files containing that authors name into the new directory.
EDIT::::
So basically I combined the suggestions of the three answers and what I got works PERFECTLY! Here is the .bat that worked:
#echo off &setlocal
set /p "Author=Enter Name: "
md "%author%"
for /f "delims==" %%i in ('dir C:\Users\Amanda\Downloads /b /s^|find "%author%" /i') do (move "%%i" "C:\Users\Amanda\Downloads\BOOKS\%author%")
EDIT AGAIN:
So I tried the revised one to try and get it to accept spaces:
#echo off &setlocal
set /p "Author=Please enter an Author: "
md "C:\Users\Amanda\Downloads\BOOKS\%author%"
for /f "delims==" %%i in ('dir C:\Users\Amanda\Downloads /b /s^|find "%author: =.%" /i') do (move "%%i" "C:\Users\Amanda\Downloads\BOOKS\%author%")
It works correctly in making the directory but it does not move the files.
set /p author=Please enter an Author:
md "C:\Users\Amanda\Downloads\BOOKS\%author%"
for /f "delims==" %%i in ('dir C:\Users\Amanda\Downloads /b /s^|findstr "%author: =*%"') do (move "%%i" "C:\Users\Amanda\Downloads\BOOKS\%author%"
this should work. if not, please comment so I can fix it.
Try this:
$author = (read-host -prompt 'Enter Name')
$basedir = 'C:\Users\Amanda\Downloads'
$destdir = "$basedir\BOOKS\$author"
New-Item $destdir -Type directory
Get-ChildItem $basedir -Recurse | ? {
-not $_.PSIsContainer -and
$_.Name -like "*$author*" -and
$_.Directory.FullName -ne $destdir
} | % {
Copy-Item $_.FullName "$destdir\"
Remove-Item $_.FullName
}
#echo off &setlocal
set /p "author=Enter the author's name: "
cd /d "C:\Users\Amanda\Downloads"
md "books\%author%"
for /r %%a in (*"%author%"*) do move "%%~fa" "books\%author%"