I want to know if this is possible with PowerShell.
I have folders that have file within them
Some folders have only TIF files
Some folders have both TIF and TXT files
I want to see if PowerShell can look in each folder and when it finds a directory that has both TIF and TXT files, move that directory to another location.
I believe I have the base to move a folder but need to see if I can wrap that ability to move based on folder content.
$srcfolder = "C:\Work\Test2"
$finalfolder = "c:\work\TestReview"
$combine = "C:\Work\Scripts\Tools\ImageMagick\convert.exe "
$arg1 = " -compress zip "
if (!(Test-Path -path $finalfolder)) {
Move-Item -Path $srcfolder -Destination $finalfolder -force
}
Related
I have a bit of a random task I have created for myself. I basically have a git repo in which there is a file structure and within a specific folder, I have several subfolders and nested in those folders are 3 config folders which have the same name. I am trying to create a powershell script thatll comb through the "Target Folder", copy the "Folder 1", "Folder 2", and "Folder 3", but only copy the contents of the 3 "Config Folder"s, maintaining that file structure, but only copying whats needed. Ideally, after that process, id love to rename these files with part of the name of the folder name to help differentiate. I do have plans to integrate a second part of the script to parse through those config files and export to an excel doc, but not sure how much I need that at the moment. The intended output is below, played around with a few misc file structure commands, but have not found much to help me achieve the below result.
File Structure:
Repo
TARGET FOLDER
DATA
FOLDER1
CONFIGFOLDER
MISC
FOLDER2
CONFIGFOLDER
MISC
FOLDER3
CONFIGFOLDER
ETC
Hoping to end up with
Export Folder
TARGET FOLDER
FOLDER1
CONFIGFOLDER
List of files with "FOLDER1_ogfilename.yaml"
FOLDER2
CONFIGFOLDER
List of files with "FOLDER2_ogfilename.yaml"
FOLDER3
CONFIGFOLDER
List of files with "FOLDER3_ogfilename.yaml"
I have created the following item to attempt this, and it copies the file structure, but it creates a folder for each .yaml file within that folder.
$sourceDir = "C:\Users\hhh\appdev\hhh\data\environments"
$targetDir = "C:\Users\hhh\appdev\targetfolder"
Get-ChildItem $sourceDir -Recurse | % {
$dest = $targetDir + $_.FullName.SubString($sourceDir.Length)
If (!($dest.Contains('research,qa,production,global')) -and !(Test-Path $dest))
{
mkdir $dest
}
Copy-Item $_.FullName -Destination $dest -Force
}
There are issues with your code.
you need to add switch -File to the Get-ChildItem cmdlet to have it look for files, not the directories inside $sourceDir
use Join-Path to construct your destination folder path. By adding the two strings together like you do, you will be missing a backslash
use the files DirectoryName property instead of its FullName when taking the substring from it, otherwise the $dest variable will also include the file name (creating folders for every file)
apparently you wish to not copy files from folders having certain keywords in their path name, so you need to put the copy command inside the test, not below it
Try:
$sourceDir = "C:\Users\hhh\appdev\hhh\data\environments"
$targetDir = "C:\Users\hhh\appdev\targetfolder"
Get-ChildItem $sourceDir -File -Recurse | ForEach-Object {
# use the files DirectoryName, not the FullName property, otherwise the path will include the file name as well
$dest = Join-Path -Path $targetDir -ChildPath $_.DirectoryName.SubString($sourceDir.Length)
# exclude paths containing these words
if ($dest -notmatch 'research|qa|production|global') {
# create the new folder if it does not already exist
$null = New-Item -Path $dest -ItemType Directory -Force
$_ | Copy-Item -Destination $dest -Force
}
}
I am writing a script that will loop through a folder I have, and display the name of the files. The only problem I am facing, is there are many folders, inside of folders. Example(Inside Test Upload folder, are three more files and two more folders. Inside those two folders are 3 files, and another folder that contains a single file) I am unsure how to continue the loop, until all files and every folder has been read.
$Files = Get-ChildItem "C:\Users\HelloWorld\Documents\Test Upload"
#write-host $Files
GetFolderContents($Files)
function GetFolderContents($Test){
#$Sub = Get-ChildItem "C:\Users\HelloWorld\Documents\Test Upload\$Test"
foreach($files in $Test){
#check if folder or file
if (! $files.PSIsContainer)
{
write-host "File"
}
else{
write-host "Folder"
#need to now loop through this folder and get its contents
}
}
}
Use the -Recurse switch parameter with Get-ChildItem to have it recursively enumerate the whole directory structure:
$Files = Get-ChildItem "C:\Users\HelloWorld\Documents\Test Upload" -Recurse
I've managed to create folders based on file names, and moving them in the corresponding folders. But i just cant figure out how to move them in sub folders of the corresponding folders.
Example:
IMG1 Create folder IMG1 and move file to \IMG1\Image\
IMG2 Create folder IMG1 and move file to \IMG2\Image\
IMG3 Create folder IMG1 and move file to \IMG2\Image\
Thanks in advance!
Are you looking for something like
$PathToSearch = "C:\Users\mspow\Downloads\Test"
$Files = Get-ChildItem -Path $PathToSearch -File
foreach ($file in $Files)
{
New-Item -ItemType Directory -Force -Path ($PathToSearch + "\" + $file.BaseName + "\Image")
Move-Item -Path $file.FullName -Destination ($PathToSearch + "\" + $file.BaseName + "\Image")
}
We use Get-ChildItem to get a list of all files in the directory. Then for each of those files, first we create a new subdirectory in that folder using the Base Name of the file- the name without the extension. Then we move the original file into the new subdirectory.
I have the below 5 folders on the desktop.
Each of these folders has a sub-folder word and the word sub-folder contains file document.xml. For example, This is the path of the file
(C:\Users\u0119342\Desktop\LEG_DOWNLOAD\BACK UP DOCX\document\Australian Citizenship (Transitionals and Consequentials) Act 2007\word\document.xml).
I would like to move the document.xml out of the sub-folder word to the parent folder "Australian Citizenship (Transitionals and Consequentials) Act 2007" and rename it as "Australian Citizenship (Transitionals and Consequentials) Act 2007".
I want to do the same process for all the document.xml files in the rest of the four folders.
Please advise me if there is a way to do this with power shell or batch script.
Thanks,
Venkat
I believe this will do your job. Give it a try.
$Root = "C:\Users\u0119342\Desktop\LEG_DOWNLOAD\BACK UP DOCX\document"
$Folders = Get-ChildItem -Path $Root
Foreach($Fld in $Folders)
{
If(Test-Path "$($Fld.FullName)\word\document.xml")
{
# Move the file document.xml and rename it
Move-Item -Path "$($Fld.FullName)\word\document.xml" -Destination "$($Fld.FullName)\$($Fld.Name).xml"
#Deletes Word folder
Remove-Item "$($Fld.FullName)\word"
}
}
My folder structure looks like this:
SourceFolder
├─file1.txt
├─file1.doc
└─Subfolder1
├─file2.txt
├─file2.doc
└─SubSubFolder
├─file3.txt
└─doc3.txt
This script copies all *.txt files from folders, whose (folder) names contains the eng, to a destination folder. Only the files inside the folder.
$dest = "C:\Users\username\Desktop\Final"
$source = "C:\Users\username\Desktop\Test1"
Get-ChildItem $source -Filter "*.txt" -Recurse |
Where-Object { $_.DirectoryName -match "eng" } |
ForEach-Object { Copy-Item $_.fullname $dest }
In my situation the folders are in .rar format and I want the script to search .rar folders and copy the *.txt files from folder eng to destination. Is that possible with PowerShell?
Rar is a proprietary archive format. Your .rar items aren't folders but compressed archives of that format. PowerShell can't transparently handle such archives, and I'm not aware of a third-party provider that would add this functionality.
Basically, no, what you're asking isn't possible.
What you can do is extract files from the archive with tools like 7-zip, e.g.:
7z.exe e your.rar *.txt -r
For extracting only files from a nested subfolder inside the archive prepend the extract pattern with that path:
7z.exe e your.rar "nested\subfolder\*.txt" -r