I have a folder (let's call it mainFolder) which holds hundreds of more folders inside. In each of these hundreds of folders I have two png images and a text file. I want to move (or copy) all the text files into a folder (let's call this targetFolder). How can I do this in Powershell or Batch?
If you want to move all items from source (including subfolders) to the destination.
This should do it:
$Source = "C:\Source"
$Destination = "C:\Destation"
Get-ChildItem -Path $Source -Filter "*.txt" -Recurse | Move-Item -Destination $Destination
You can add -Whatif to the end of the Get-ChildItem (after $Destination) in order to test it before execution
If you want to copy only you can replace the Get-ChildItem line with :
Copy-Item -Path $Source -Recurse -Include "*.txt" -Destination $Destination
Something like this, as an example if you want to copy all txt files from the source, recursively in all sub directories:
for /R "c:\source folder" %%f in (*.txt) do copy "%%f" "c:\destination folder\"
to actually move the files, simply use the command instead of copy, perhaps consider the /Y
If however your destination folder were to be inside of your source folder you could simply ignore it.
for /R "c:\source folder" %%f in (*.txt) do (
if not "%~dpf"=="c:\source folder\" copy "%%f" "c:\destination folder\"
)
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
How can I use windows bat or powershell to mass-copy files from child-dirs to the root/main directory? eg,
c:\dir\1
c:\dir\2
c:\dir\3
etc
to
c:\dir
without having to copy-paste them all manually.
In a batch file:
for /d %%d in ("C:\Dir\*") do for %%f in ("%%d\*") do copy "%%f" "%%d\.."
Interactively:
for /d %d in ("C:\Dir\*") do for %f in ("%d\*") do copy "%f" "%d\.."
Hint: for /d iterates over directories. Use for /? for more details.
If it's only the files you want to move then a quick a dirty method would be something like:
$InputFolder = "C:\Dir"
$FilesToMove = Get-ChildItem -LiteralPath $InputFolder -Recurse | where { ! $_.PSIsContainer }
foreach ($File in $FilesToMove)
{
Move-Item -LiteralPath $File.PSPath -Destination $InputFolder -Force
}
So if for example you have:
C:\Dir\MyDir1\File.txt it will move it to c:\dir\file.txt
However, no folders will be moved.
eg say c:\Dir\MyDir2\AnotherDir\ contains File1.txt and Fle2.txt only the .txt files will get moved, c:\Dir\MyDir2\AnotherDir\ would remain intact but without any files.
This question kind of look like this one here
Windows-cmd-add-suffix-to-all-files-in-folder, but it has some additional elements I'm not able to overcome.
I have a main folder, containing many subfolders, with a lot of .xlsb files, and I wish to add a postfix " 2018" to eaxh file, e.g. after renaming a file with name "abc.xlsb" will be called "abc 2018.xlsb". I'm having problem with finding a code, making a script in the command line that will do this for all subfolders.
For now, this is what I have for one folder:
rename *.xlsb "* 2018.xlsb"
,but the resulting name for, e.g., "a.xlsb" comes out "a.xlsb 2018.xlsb".
Also, how do I do this for all folders?
EDIT: I found a command to rename for all subfolders:
for /r %x in (*.xlsb) do ren "%x" *.xlsb
,but
for /r %x in (*.xlsb) do ren "%x" "* 2018.xlsb"
gives, e.g. for a file "abc.xlsb" --> "abc.xlsb 2018.xlsb"
This is pretty straightforward in PowerShell. Use the BaseName and Extension members separately. When you are confident that the files will be renamed correctly, remove the -WhatIf from the Rename-Item cmdlet.
Get-ChildItem -File -Recurse -Filter '*.xlsd' |
ForEach-Object {
$newname = $_.BaseName + ' 2018' + $_.Extension
Rename-Item -Path $_.FullName -NewName $newname -WhatIf
}
If you must run this from a cmd.exe shell, then put the PowerShell code above into a filename such as renappend.ps1. Then run it as shown.
powershell -NoLogo -NoProfile -File renappend.ps1
I want to make lots of files 0 bytes on windows.
The command copy /y nul test.vtx was working. I need to change their size without changing the file names.
How can i use copy command to automatically detect the filenames and use them to erase its contents? Would be a bath file helpful?
Thanks.
With break> i don't know if it is possible anyway you can iterate :
for %G in (*.vtx) do (copy /Y nul "%G")
Including subfolders :
for /R %G in (*.vtx) do (copy /Y nul "%G")
Another way using PowerShell. When you are satisfied that the correct files will be overwritten, remove the -WhatIf from the Set-Content statement.
Get-ChildItem -Path C:\src\t\adir -Filter *.vtx |
ForEach-Object { Set-Content -Path $_ -Value $null -WhatIf}
Usage :: ROBOCOPY source destination [file [file]...] [options]
source :: Source Directory (drive:\path or \\server\share\path).
destination :: Destination Dir (drive:\path or \\server\share\path).
file :: File(s) to copy (names/wildcards: default is "*.*").
/CREATE :: CREATE directory tree and zero-length files only.
I see you specified "copy" but I assumed you or others might accept robocopy too. My guess would be:
ROBOCOPY C:\vtxfolder1 C:\vtxfolder2 *.vtx /CREATE
Expanding upon this example: Copying a file to multiple folders in the same directory I want to copy an XML file to several different project directories under the same root folder, so I have tried this:
for /d %a in (C:\Root\Output\*\bin\x64\Release) do copy /y C:\Root\OtherProject\MyXml.xml %a\
Where the wildcard would be a different project name in my solution directory.
When I run this command, there is no error, but the XML is not copied, so my question is can you use wildcards like this in Windows command line or alternatively is there a better way to tackle this kind of operation from within command prompt?
Just split it:
FOR /D %1 IN (c:\root\output\*) DO (
COPY /Y c:\root\otherproject\myxml.xml %1\bin\x64\release
)
Obviously this works if all subdirectories of c:\root\output must be included (they have a bin\x64\release subdirectory.) If it's not true then you have to include an extra check:
FOR /D %1 IN (c:\root\output\*) DO (
IF EXIST "%1\bin\x64\release" (
COPY /Y c:\root\otherproject\myxml.xml "%1\bin\x64\release"
)
)
Of course you may feel this is to much code for such simple task, well then maybe it's time to switch to PowerShell, Get-ChildItem supports wildcards used the way you want:
Get-ChildItem c:\root\output\*\bin\x64\release `
| ForEach-Object -Process `
{ Copy-Item -Force c:\root\otherproject\myxml.xml $_ }
If you love short syntax:
gci c:\root\output\*\bin\x64\release | % { cp -Force c:\root\otherproject\myxml.xml $_ }