I have a Excel file with the current filename and new filename, both are the whole path to the files. I would like to write a PowerShell script to change the filename.
I have created a csv file but I don't know how to create this within the for each iteration.
With a CSV file with the data like this:
OldName,NewName
C:\folder\file.txt,new_file.txt
C:\folder\song.mp3,different_song.mp3
This code will rename each file:
$files = Import-Csv "C:\folder\rename_files.csv"
foreach ($file in $files){
Rename-Item -Path $file.OldName -NewName $file.NewName -Verbose
}
Related
I have hundreds of HTML files in tabular format which needs to be converted to CSV. To do it manually, I'd be opening each HTML file in Excel and saving as *.CSV.
How can I automate this through PowerShell?
$FolderPath = 'C:\Users\abcd\Desktop\New folder'
$FilePaths = get-childitem $FolderPath -recurse | where {$_.extension -eq ".html"}
foreach($FilePath in $FilePaths)
{
Export-CSV -Path $FilePath
}
If I understand you correctly you have HTML files that actually have CSV content is that correct? In that case you would only need to rename them from .html to .csv wouldn't you?
Move-Item -Path $filePath -Destination $filePath.Replace(".html", ".csv")
If not what exactly is the format of the "tabular format" in the source files? Can u provide example data from these HTML files?
I am currently in the process of restructuring a Windows server file directory and need to bulk rename over 10,000 files all in different folders but most have duplicate filenames (e.g 0001.pdf, 0002.pdf)
I have written a Powershell script to help me do this but I am having some trouble with the execution.
The script reads a csv file (export of all the files in the folder structure - export.csv) then creates the variables $OldFilename & $NewFileName. The variables are then used to locate the old file location and copy it to a new destination using the Copy-Item command for each row in the CSV.
Code
$csv = import-csv c:\tmp\test.csv
$csv | foreach-object {
$OldFileName =$_.OldFileName
$NewFileName = $_.NewFileName
}
$csv | ForEach-Object {
$OldFileName =$_.OldFileName
$NewFileName = $_.NewFileName
Copy-Item $OldFileName -Destination $NewFileName
}
CSV is formatted as such
OldFileName,NewFileName
"X:\Folder1\0001.pdf","C:\temp\File0001.pdf"
"X:\Folder2\0001.pdf","C:\temp\File0002.pdf"
"X:\Folder3\0001.pdf","C:\temp\File0003.pdf"
Code does not error when run but files do not appear.
A Write-Output displays the correct file path but a Write-Host((Get-Item $file).length/1MB) does not retrieve the files size which leads me to believe there is an issue with the variable!?
Something I need to add?
Many Thanks
Jonny
Any help greatly appreciated.
I have a folder that contains 30+ folders which each have a .txt file that I can search for using:
Get-ChildItem -Filter *.txt -Recurse
I want to read the contents of each .txt file discovered and output the contents int a new .csv file on my desktop that also includes the directory of each .txt file contents being displayed.
The question is twofold,
how to use pipe and powershell commands to read/show all the words in the files.
how to create the csv data that will output both the directory name and the contents of the .txt files.
I can already pipe results to:
c:\desktop\test.csv -Encoding ascii -noTypeInformation
The following script reads all .txt files within a specific directory, stores the full filename and path as well as the files content into an array and saves it as a csv.
$csvOut = #()
Get-ChildItem -LiteralPath C:\temp -Filter *.txt -File -Recurse | foreach {
$fileData = #{
"File"=$_.FullName;
"Content"=(Get-Content -LiteralPath $_.FullName -Raw)
}
$csvOut += (New-Object psobject -Property $fileData)
}
$csvOut | Export-Csv -LiteralPath "C:\temp\csvout.csv" -NoTypeInformation
I'm looking to move and rename a .csv file from one folder to another using PowerShell on a daily basis. The .csv file will have a different file name each day but will always be similar to 'Course Completion_123456_1.csv'
I'm new to Powershell and can work out how to move and rename a file with a known name using the following code;
Move-Item c:\folder1\Course Completion_123456_1.csv c:\folder2\CourseCompletion.csv -force
It's the .csv file with the unknown file name that I can't figure out and I'm hoping someone can help.
Assuming that you have only one .csv file in your source folder.
Try this:
$csv = Get-Item "path to the source csv file\*.csv"
$file = $csv.FullName
Move-Item $file "path to the destination folder\newfilename.csv" -Force
I am new to the powershell scripting and hence need one help in scripting, the script which I am looking for should search for the folders as per the entries in .csv file, please note that it should search for the folders in the drive without knowing the path and move it to the destination.
I did some research and created below script which is taking data from .txt file and moving the data to the destination however it does not work if I just write C:\ at the place of source folder.
Request you to please help me :)
Get-Content C:\abc.txt |
Foreach-Object {
move-item -path "C:\0123\$_" -destination "C:\To Archive\$_"
}
With what you've given, I'd do something like the following:
$File = Import-Csv C:\share\test\files.txt
Foreach($fileName in $File.FileName)
{
Move-Item -path "C:\share\test\OldLocation\$fileName" -Destination "C:\share\test\NewLocation\$fileName"
}
I did this with a .csv file that had one column whose title was FileName. Notable differences from your code include using the Import-Csv cmdlet and specifying the .csv header title in the foreach loop.
If you wanted to do this with a single command:
Import-Csv C:\share\test\files.txt | ForEach-Object {
Move-Item -path "C:\share\test\OldLocation\$($_.[csvHeader])" -Destination "C:\share\test\NewLocation\$($_.[csvHeader])"
}
Where csvHeader is the title of the column in your .csv file.
My .csv file looked like:
FileName
file1.txt
file2.txt
file3.txt