I have an excel file that is used to perform certain operations on the database & that sheet needs to be copied right when the update is made. I have the script that does the update on the database but before that, I'm using the Copy-Item command to save the copy of the source excel file with a new name. But I'm getting a generic error like:
"Error/ getting/reading excel file \\shared\\documents\\myData.xlsx"
Is that a permission issue or am I having an incorrect syntax. Or should I use robocopy command in powershell? Here's the chunk of powershell code that I have written so far:
#source file location
$fileLocation = "\\shared\documents\myData.xlsx"
#getting current date
$currentDate = Get-Date -Format "yyyy-MM-dd HH:mm"
#destination location
$backUpLocation = "\\shared\backup\myData_" + $currentDate + ".xlsx"
#copying file
Copy-Item $fileLocation $backUpLocation
Any better solution is appreciated. Thanks in Advance.
Shoot! There was a silly mistake a file name cannot have ':' while the $currDate I was fetching is having a colon that's breaking. Copy-Item works best as it also allows renaming the file in the single command itself.
Related
I am building out a batch file that takes a zip archive which is uploaded nightly and unzips the contents and puts it in a specific folder.
Currently, I am generating a temporary folder named "tmpExtract". Once the files are extracted, I want to rename "tmpExtract" with yesterday's date. Preferably in MM-DD-YYYY format.
I was able to locate a PowerShell command to get yesterdays date, however, I am stuck at the part where I can rename the directory to yesterdays date.
PowerShell $date = Get-Date; $date=$date.AddDays(-1); $date.ToString('yyyy-MM-dd')
The full directory that I am using:
Y:\\Archive\TestFolder\Recordings\tmpExtract\
The full directory that I am looking for once the rename has completed: Y:\\Archive\TestFolder\Recordings\06-09-2021
I am open to either PowerShell or Batch/CMD. Whichever works best!
Based on Theo's response I found the following code to work best
powershell Get-Item -Path 'Y:\Archive\TestFolder\Recordings\tmpExtract' | powershell Rename-Item -path 'Y:\Archive\TestFolder\Recordings\tmpExtract' -NewName ('{0:dd-MM-yyyy}' -f (Get-Date).AddDays(-1))
I needed to add powershell and -Path 'Y:\Archive\TestFolder\Recordings\tmpExtract' to the second part of the string. After testing it works as intended.
Also, I was unaware that the second '' was not needed for a mapped network path.
Could you please help me with this script? I am writing in PowerShell a script in order to monitor data of one .csv file into PRTG, basically script goes to a remote server make a SFTP connection and brings it to PRTG server.
Then I just look for the data, the issue is that I have is to select and specific file, for example in the remote server there is a new file with and specific date generated every hour and some values are without a pattern.
I already tested and script works except, to look for specific file, cause if I put static name file works
These are examples of .csv files that i need
structure is name + date (Y-m-d-H) + 8 characters +.csv:
RG_400_Total_by_Hour-20210606021446-174.csv
RG_400_Total_by_Hour-20210606031630-456.csv
RG_400_Total_by_Hour-20210606041445-288.csv
RG_400_Total_by_Hour-20210606051513-761.csv
RG_400_Total_by_Hour-20210606061446-877.csv
RG_400_Total_by_Hour-20210606071446-454.csv
RG_400_Total_by_Hour-20210606081446-838.csv
RG_400_Total_by_Hour-20210606091505-171.csv
RG_400_Total_by_Hour-20210606101447-220.csv
This is the part where I completely stuck.
How can I use a wildcard in this variable that I'm trying:
$file=$remotepath+"/RG_400_Total_by_Hour-"+$date+""+$WILDCARD???+""
Here is a part of the script
$date= get-date -UFormat "%Y%m%d%H"
$t= Look for wildcart to use
$folder=$date
$hora= [int] (get-date (get-date).addhours(-2) -UFormat "%H")
$ayer = (get-date (get-date).addDays(-1) -UFormat "%Y%m%d")
if ($hora -eq 22) {$folder=$ayer}
if ($hora -eq 23) {$folder=$ayer}
$remotepath= $remotedir
$remotedir="/usr/local/sandvine/var/nds/cache/csv"
$file=$remotepath+"/RG_400_Total_by_Hour-"+$b+""+$t+""
$file2=$localpath+"/RG_400_Total_by_Hour-"+$b+""+$t+""
Get-SFTPFile -SFTPsession $global:Session -remotefile $file -localpath $localpath
Remove-SFTPSession -sftpsession $global:Session | Out-Null
I really appreciate any comment, thanks guys.
Complete PowerShell newbie so please forgive me as I might mess up what I am asking.
I have a file called debug.log, I need to rename it and move it.
I know I can do that with Move-Item, but I would like to append the Hostname to the front of the file name and the date stamp to the end.
For example:
In: C:\Temp\debug.log
Out: \\\sharename\MyHostdebug20201006.log
The files will go through a log parse after they get copied to the share, and the HostName and Date stamp are import to my tracking of which logs get processed. I have to do this for 8 separate servers on a monthly basis. I plan on setting this up as a Windows Schedule Task when it is completed.
Using Get-Date and, as mentioned by #Scepticalist above, $env:ComputerName you can create a string variable which you can pass into your Move-Item as the destination:
$timestamp = get-date -Format yyyyMMdd
$newPath = "\\sharename\" + $env:computername + "debug" + $timestamp + ".log"
I am working out a scenario using PowerShell.
As of now, I am trying to workout a scenario where I have a batch job which upon successful execution first creates a date folder for that particular day and then creates a .CSV file under. The folder structure than look like below.
\\Server\SourceA\Processed\20200120\TestA.CSV
when the job runs next day, it will create another folder and file like below.
\\Server\SourceA\Processed\20200121\TestB.CSV
This is how there have been so many folders already created in past.
My PS script is good to run daily after the batch job is completed. I read a date, append to path and it copies file from source to destination folder. But I want to enable my PS script to read all previous date folders created under
\\Server\SourceA\Processed\
Another tricky part is - under the date folder there is few other sub folders. I.e.
\\Server\SourceA\Processed\20191010\Log
\\Server\SourceA\Processed\20191010\Charlie
\\Server\SourceA\Processed\20191010\Alpha
\\Server\SourceA\Processed\20191010\Delta
among them, I only need to read files from Log folder only.
Hence, my actual source path becomes like
\\Server\SourceA\Processed\20191010\Log\TestA.CSV
Here is my script (which is static right now and unable to read past existing date folders).
$fullSourceFileName = "\\Server\SourceA\Processed\"
$date = Get-Date -format "yyyyMMdd"
$fullSourceFileName = "$($fullSourceFileName)$($date)\Log
$destination = "\\Server\DestA\Processed\"
$destination = "$($destination)$($date)\"
get-childitem -path $fullSourceFileName -recurse | copy-item -destination "$($destinationFolder)$($date)\"
Your help is highly appreciated.
I did not know I can use foreach loop in Powershell.
So, here is the answer to read all dynamic date folder under my given path.
I hope this helps the community.
$fullSourceFileName = "\\Server\SourceA\Processed\"
$DirToRead = "\Log\"
$dates = Get-ChildItem -Path $fullSourceFileName -Directory
$destination = "\\Server\DestA\Processed\"
foreach ($date in $dates){
$ActualPath = "$($fullSourceFileName)$($date)$($DirToRead)"
if(!(Test-Path $ActualPath))
{
Write-Output $($ActualPath)$(" source path does not exist")
}
else
{
get-childitem -path $ActualPath -recurse | copy-item -destination "$($destinationFolder)$($date)\"
}
$ActualPath = ""
}
I would like to create a powershell which must rename a file by adding the current date then move it to another folder (ie.e to archive it). the script should be automatically executed every day at a specific start time (10:OO PM)
I wonder if someone could help me to do that.
Thanks in advance !
this is not how stackoverflow works but i had this part ready
$destpath = 'C:\TEMP\XMLTRANS\Backup\'+(get-date -Format
yyyyMMddHHmm)+'.xml' $sourcepath = 'C:\TEMP\XMLTRANS\FILE.xml'
Move-Item -Path $sourcepath -Destination $destpath
you can run this script behind a scheduler or Cron
hope it help !
This script will rename a file with the addition of a date/time stamp and move to an another folder.
A mention to #alex above as I used his script and made a slight change. I wanted to keep the original file name but add the date/time stamp. Thank you Alex, very simple script for a newbie like me :)
$fileName = 'My Test File_'
$destpath = 'G:\ArchiveFolder\'+ $filename + (get-date -Format yyyyMMddHHmm)+'.sql'
$sourcepath = 'G:\OriginalFolder\My Test File.sql'
Move-Item -Path $sourcepath -Destination $destpath
Hope it helps?