:)
Let's say in the file path, C:\Users\CurrentUser\Documents\Records, there are several folders.
I want to rename all the files in all the folders by adding their respective folder names in front of their current file names.
For example, folder 1 and folder 2 exists in C:\Users\CurrentUser\Documents\Records. All the files in folder 1 should be renamed with "folder 1 " added in front. And all the files in folder 2 should be renamed with "folder 2 " added in front. So something like the file "Invoice" in folder 1 being renamed to "folder 1 Invoice". Same thing with folder 2.
Is there a way to do this in Powershell? Any help is much appreciated! :)
I think this should do it:
(Get-ChildItem -Path 'C:\Users\CurrentUser\Documents\Records' -File -Recurse) |
Rename-Item -NewName {'{0} {1}' -f $_.Directory.Name, $_.Name} -WhatIf
The -WhatIf switch is a precaution. With that added, the code wil only show in the console what would happen. If you are satisfied with that, remove the -Whatif switch to actually start renaming the files.
Related
For example, I wanted to print a list of folders which contain an .html file. Correct me if I am wrong, but this worked for me:
dir /S *.html > C:\Users\PC\Desktop\with-html.txt
Now I would like to find the folders which do not contain .html files.
How do I go about that?
EDIT:
The folders are structured in a way that only the child folders (last subfolder) have any kind of files. I would like to get a list of directories to those subfolders. So the above command line is giving me:
C:\...\ml\regression\lasso-regression
C:\...\ml\regression\linear-regression
There is not output just C:\...\ml or C:\...\ml\regression.
The folder structure looks like this:
C:\...\ml
classification
regression
lasso-regression
linear-regression
There are about 10 folders in folder ml and no files. There are again about 10 folders in second folder level where C:\...\ml\regression\linear-regression contains an HTML file while C:\...\ml\regression\lasso-regression does not contain a file with file extension .html. Only the folders in last level of the folders tree have files at all.
I'd be grateful getting just the list of the last folders in folders tree not containing a file with file extension .html.
I basically output the above command line into a .csv file, filtered it with MS Excel, and have now a list of folders with .html file(s). I'm basically working with R-markdown files, and it'll be a status report, the folders list with .html files is what I have completed already. So in need now only the opposite folders list.
Not difficult using PowerShell.
Get-ChildItem -Recurse -Directory |
ForEach-Object {
if ((Get-ChildItem -File -Path $_.FullName -Filter '*.html').Length -eq 0) { $_.FullName }
}
If you must run this in a .bat file script, the following might be used.
#powershell.exe -NoLogo -NoProfile -Command ^
"Get-ChildItem -Recurse -Directory |" ^
"ForEach-Object {" ^
"if ((Get-ChildItem -File -Path $_.FullName -Filter '*.html').Length -eq 0) { $_.FullName }" ^
"}"
I have a folder and numerous nested folders are present inside that. Many of the folders are empty and I have to copy an empty.property file in those empty folders. So that the end result will be no folder will be completely empty, either it contains another folder, any other file(s) or this empty.property. I have tried to get all the paths using dir /b /s, but it is returning all the paths, not only the empty one. Can anyone help me to get that very efficiently. Thanks.
You can use powershell to do it several ways with one being:
Get-ChildItem -Path C:\Temp -Directory | Where-Object {$_.GetFileSystemInfos().Count -eq 0} |
ForEach-Object -Process { Copy-Item -Path "My\File\to\copy\Path" -Destination $_.FullName }
Basically checks to see which directory doesnt have no files or folders in it, then pipes it to a foreach to a process a Copy-Item request for whatever file/folder you want it to copy from, to the empty folder.
I'm relatively new to programming so I need some help.
I have 2 directories
C:\test1
C:\test2
So in test1 will get constantly get files.
Which look like this:
testA000_00001.txt0..txt
test00A0_00102.txt1..txt
test00A0_00102_00123.txt45..txt
...
testG000_00999.txt999..txt
testH000_00013.txt0..txt
Since its essential that the files in test1 stay the way that they are I'm gonna need them in test2.
And since test2 needs to be the current version it is needed to be done the moment the files are in test1.
But without the .txt0. - .txt999. part.
testA000_00001.txt
test00A0_00102.txt
test00A0_00102_00123.txt
...
testG000_00999.txt
testH000_00013.txt
Its also essential that these files are only copied once since they aren't gonna stay in test2 for long.
I tried it with xcopy and some other versions of copy but each time it copies the files back into test2 and after I move the files from test2 the files are copied into it again.
(sry cant comment yet)
I dont have exact code (working on it)
What i think would work is to:
Check a text file for things it alr coppied
schedual a task to run every minute or so
move to the folder you want
Use the dir command to take all files in the current directory
Copy them
Write these to a text file
Change The names
loop
When i get the code done i will edit.
Also moving them assoon as they enter the directory is very resource intensive but might be possible if the code is very efficient.
#make a folder
md "C:\test\test3"
#move to test1
cd "C:\test\test1"
# Presets
$txt = ".txt"
$files = dir
#clean up the files list
$files = $files -split "`r`n"
#for each file it found
foreach ($line in $files) {
#move the file from test1 to test 3
Copy-Item "C:\test\test1\$line" -destination "C:\Test\test3"
#take the first 14 chars from the file name
$line_name = $line.Substring(0, 14)
#add a .txt extension
$line_name = $line_name + $txt
#rename the files in test3
Rename-Item -path "C:\test\test3\$line" -NewName "$line_name"
#move the files to test 2 and if they alr exist replace(update) them
Move-Item -Path "C:\test\test3\$line_name" -Destination "C:\Test\test2" -Force
}
Remove-Item –path "C:\test\test3" –recurse -force
This Code is super close to working but i might not be able to finish today.
It only failes to move the file: test00A0_00102_00123.txt45..txt
due to the name not working. I will update the script when it works fully.
I have a folder containing multiple sub folders named like : 12345 - textfoldername
I want to rename all these sub folders by keeping just the first number (12345) and delete all the rest ( - textfoldername).
How can I build the windows script for that.
Thanks for your help !
With powershell, use Get-ChildItem to discover all the subfolders, then use Rename-Item to rename:
Get-ChildItem path\to\root\directory -Directory |Rename-Item -NewName {$_.Name -replace ' - .+$'}
The -replace operator with remove - and anything thereafter in the existing name (or, if - something isn't found, ignore it)
I need to rename a bunch of files at once in Windows PowerShell. I read the HTG article here and it helped a little.
My problem is that It will only rename files in the top of the directory, nothing deeper. For example: There is FOLDER A and inside FOLDERA is a document and FOLDER B. Inside FOLDER B is another document. Both folders and both documents need to be renamed. The way it is working now is that FOLDER A, the document in FOLDER A, and FOLDER B are being renamed, but not the document inside FOLDER B.
My current code is:
Dir | Rename-Item –NewName { $_.name –replace “ “,”_” }
Thanks for the help!
You need to specify the -Recurse parameter on Dir to get it to recurse e.g.:
Dir -recurse | Rename-Item -NewName {$_.Name -replace ' ','_'}
BTW this may run into a problem because you're renaming the folder (FOLDERB) that contains the document first but the item being piped that corresponds to the file in FOLDERB still has the old name. In this case, you want to rename from the bottom up. One very crude but effective (I think) way to do this is to sort the file items on their path length descending e.g.:
Dir -recurse | Sort {$_.FullName.Length} -Desc | Rename-Item {$_.Name -replace ' ','_'}