I am able to batch rename files in a working directory by using:
Dir | %{Rename-Item $_ -NewName ("0{0}.wav" -f $nr++)}
However I want the file rename to start at something other than zero. Say 0500, and rename sequentially in order.
Dir | %{Rename-Item $_ -NewName ("0{500}.wav" -f $nr++)}
returns error.
How can I tell rename to start at a number other than 0?
You can initialize the counter beforehand to 500. Also, you don't need to use a ForEach-Object loop (%) for this, because the NewName parameter can take a scriptblock.
Important here is that you need to put the Get-ChildItem part in between brackets to let that finish before renaming the items, otherwise you may end up trying to rename files that have already been renamed.
$nr = 500
(Get-ChildItem -Path 'D:\Test' -Filter '*.wav' -File) | Rename-Item -NewName { '{0:D4}{1}' -f ($script:nr++), $_.Extension }
Related
I have a long list of folders. Most of the folders follow the "name_#name" format. I have some that don't follow that structure. I want to move all the folders (and the sub-folders/files within) that DON'T have "_" in the folder name.
For example:
test_#12352
moose_#4532
horse_#84462
cow24
fish3
Moved:
cow24
fish3
I think Move files when they contain a specific word? could be modified to make it work...just not sure how. I'm used to just using GUI, this is my first time using PowerShell
When I tried using that code in that link it didn't work with my situation
What you want to do is just filter the list before you move any files
So you can use the following to pick up all the files you want
$Files = Get-childItem -Path $Path -File
You can then filter it down. My favourite way is to pipe the variable into Where-Object and play around with the individual properties and match types. Since you don't want to include the _ we can use a -notmatch "_" to exclude those values
$Files = Get-ChildItem -Path $Path -File | Where-object{$_.Name -notmatch "_"}
And finally, you can move the files
$Files | move-item -path $_.FullName -Destination $Destination
Or as a one liner
Get-ChildItem -Path $Path -File | Where-object{$_.Name -notmatch "_"} | move-item -path $_.FullName -Destination $Destination
*Please note I haven't really tested this code. So test it out yourself before you run it
I work for a law firm and we have a process that spits out text files with specific information on them in a specific pattern.
Let's say it's this:
1111.xxxxx_2222
I need to create a folder based on the characters that match the 'x' section and move the text files that have the same matching characters in the 'x' section into that folder, repeatedly. There's a couple hundred we have to go through a day so it gets a little tedious doing them by hand.
I've tried this:
$Source = 'Source folder'
(Get-ChildItem -Path $Source -File) | ForEach-Object {
$destination = Join-Path -Path $Source -ChildPath $_.BaseName
if(!(Test-Path $destination)){
New-Item -ItemType Directory -Path $destination | Out-Null
}
$_ | Move-Item -Destination $destination
}
I left the source folder off because it is a network location that I can edit.
This script is not specific enough. It does sort them based on their entire name and it only moves them to a folder of that same name. It cannot run again because the name may be created already which will not work in my case. I'm VERY NEW to power shell and can code at a basic level so any help is appreciated!
You can use a delay-bind script block:
$Source = 'Source folder'
Get-ChildItem -Path $Source -File | Move-Item -WhatIf -Destination {
# Derive the target dir. from the input filename.
$dirPath = Join-Path $Source ($_.BaseName -split '[._]')[1]
# Make sure that the target dir. exists. Note that the
# -Force switch ensures that no error occurs if the dir. already exists.
$null = New-Item -Type Directory -Force $dirPath
$dirPath # Output the target directory path.
}
Note the use of the -WhatIf common parameter, which previews the move operations, but note that only the moving of the files is previewed; the creation of the target directories still happens, because the delay-bind script block must be run even with WhatIf present, so as to be able to display the would-be destination path.
Once the preview signals that the moving would work as intended, remove -WhatIf from the Move-Item call.
($_.BaseName -split '[._]')[1] is what extracts the xxxxx from a file (base) name such as 111.xxxxx_2222, using -split, the regex-based string splitting operator
I am trying to make a script in powershell to delete all folders in C:\Temp which contains a *.sr_processed file.
I already have this but this only deletes the file and not the folder it was in.
Get-ChildItem -Path C:\Temp -Include *.sr_processed -File -Recurse | foreach { $_.Delete()}
Your are telling it to delete the file. To delete the folder do something like:
Get-ChildItem -Path C:\Temp -Include *.sr_processed -File -Recurse | foreach { Remove-Item –path $_.Directory.Fullname}
If you have multiple .sr_processed files in a folder it might attempt to delete it more than once. And generally deleting a folder you are globbing is bad practice. So a better idea would be to gather up the folders in a list/hash and delete them at the end.
That would look something like:
# declare array
$foldersToDelete = #()
# fill array with folder names
Get-ChildItem -Path C:\Temp -Include *.sr_processed -File -Recurse | foreach { $foldersToDelete += $_.Directory.Fullname}
# sort and make unique
$foldersToDelete = $foldersToDelete | Sort-Object | Get-Unique
# delete folders
$foldersToDelete | Remove-Item –path $_
This is typed from memory, so you might want to adjust it.
I need to move set of specific files having different extension to another folder.
I have following filtered files in the directory.
file1.txt
file2.xml
file3.dll
I have kept the above files in the variable $files and I need to move each of files to another folder.
Below is the code I tried.
foreach ($fileType in $files) {
Get-ChildItem -Path C:\Files -Filter "$fileType*." -Recurse |
Move-Item -Destination C:\Dest
}
I am getting following error
Get-ChildItem : Illegal characters in path.
At line:1 char:38
+ ... lude_files){Get-ChildItem -Path C:\Files
Appreciate if anyone can help on this?
An easy way to do this
ls C:\files | Foreach {
Move-Item -Path C:\files\$filetype -Destination C:\dest
}
If all the files you are after share a common 'starts-with' name like file as in your example, the below should do what you want. It uses the -Include parameter where you can add an array of (in this case) extensions to look for.
Get-ChildItem -Path 'C:\Files' -Filter 'file*' -Include '*.txt','*.xml','*.dll' -Recurse |
Move-Item -Destination 'C:\Dest'
Note: the -Include parameter only works when also used together with the -Recurse switch, OR by appending \* after the path (like in C:\Files\*)
I'm trying to make a copy of all the existing files in a folder to another folder. but I have problems with the names that have that blank
folder
-- new file.txt---->the problem
-- file2.txt
-- file3.csv
i apply this mybatch.bat
set FECHA=%date%
set FECHA=%FECHA:/=%
set FILE=D:\BACKUPS
for %%i in (*) do (
copy %cd%\%%i %FILE%\${%%~ni// /_}_%DATE%%%~xi
)
try to replace the blanks with "_" the following code, in a route
${%%~ni// /_}
but this does not run, it just comes out as string
D:\BACKUPS\${%%~ni// /_}_090519.txt
i want this
D:\BACKUPS\new_file_090519.txt
Given the PowerShell tag, here is a solution. When you are satisfied that the copy will be done correctly, remove the -WhatIf from the Copy-Item cmdlet.
Get-ChildItem -File -Path 'C:/src/t/sv' |
ForEach-Object {
Copy-Item -Path $_.FullName `
-Destination "C:/src/t/sv2/$($_.BaseName -replace ' ','_')_$(Get-Date -format 'ddMMyy')$($_.Extension)" -WhatIf
}