Hi
I want to automate the process of Powershell where I write a code
import-csv C:\Users\Desktop\abcd\Transfers\xyz_Transfers_20230515.csv | export-csv C:\Users\Desktop\abcd\Transfers\xyz_Transfers_20230515_1.csv -NoTypeInformation -Encoding UTF8
I need to run this manually for a large number of files where every time the date is changing to a new one based on one less than current date and on monday last friday date.
I am writing this code to automate the process:
# Get the current date
$currentDate = Get-Date
# Determine the date based on the logic
if ($currentDate.DayOfWeek -eq 'Monday') {
$targetDate = $currentDate.AddDays(-3)
} else {
$targetDate = $currentDate.AddDays(-1)
}
# Format the target date as 'yyyyMMdd'
$targetDateString = $targetDate.ToString('yyyyMMdd')
# Define the source and destination paths
$sourcePath = "C:\Users\Desktop\abcd\Transfers\xyz_Transfers_$targetDateString.csv"
$destinationPath = "C:\Users\Desktop\abcd\Transfers\xyz_Transfers_$targetDateString_1.csv"
# Import the CSV file and export with a modified filename
Import-Csv $sourcePath |
Export-Csv $destinationPath -NoTypeInformation -Encoding UTF8
Write-Host "CSV file processing completed for $targetDateString."
This creates a new files with the previous date but I want to create a file with date as the one in my source file. Say my source file name is ABC_XYZ_Transfer_20230531, then it should generate the new file as ABC_XYZ_Transfer_20230531_1 and not ABC_XYZ_Transfer_20230602 which is the one generating if I run the code. Also, when I open the file generated it just gives me the code but not the data in the original destination file. Why is that so? Sorry if misunderstood current date meant the date as per the one in the source file not the real "current" date.
I want to automate the process of Powershell where I write a code
import-csv C:\Users\Desktop\abcd\Transfers\xyz_Transfers_20230515.csv | export-csv C:\Users\Desktop\abcd\Transfers\xyz_Transfers_20230515_1.csv -NoTypeInformation -Encoding UTF8
I need to run this manually for a large number of files where every time the date is changing to a new one based on one less than current date and on monday last friday date.
I am writing this code to automate the process:
# Get the current date
$currentDate = Get-Date
# Determine the date based on the logic
if ($currentDate.DayOfWeek -eq 'Monday') {
$targetDate = $currentDate.AddDays(-3)
} else {
$targetDate = $currentDate.AddDays(-1)
}
# Format the target date as 'yyyyMMdd'
$targetDateString = $targetDate.ToString('yyyyMMdd')
# Define the source and destination paths
$sourcePath = "C:\Users\Desktop\abcd\Transfers\xyz_Transfers_$targetDateString.csv"
$destinationPath = "C:\Users\Desktop\abcd\Transfers\xyz_Transfers_$targetDateString_1.csv"
# Import the CSV file and export with a modified filename
Import-Csv $sourcePath |
Export-Csv $destinationPath -NoTypeInformation -Encoding UTF8
Write-Host "CSV file processing completed for $targetDateString."
This creates a new files with the previous date but I want to create a file with date as the one in my source file. Say my source file name is ABC_XYZ_Transfer_20230531, then it should generate the new file as ABC_XYZ_Transfer_20230531_1 and not ABC_XYZ_Transfer_20230602 which is the one generating if I run the code. Also, when I open the file generated it just gives me the code but not the data in the original destination file. Why is that so? Sorry if misunderstood current date meant the date as per the one in the source file not the real "current" date.