ask a question
quick access
- homepage of the forum
- Search forum users
- Frequent questions
Explore related topics
- Remove from my forums
Answered by:
Archived Forums 901-920
>
Windows PowerShell
Ask
-
Sign in to vote
I'm looking for a PS script to parse, copy, move, and log the results of files to a directory that I'll run as a task.
I'm currently learning some basic PS scripting, but I'm obviously not there yet.While I was able to get some basics below, I seem to be having a hard time finding and parsing files.And I haven't gotten to the registration part yet. I would routinely update this to change this if necessary.
Any suggestions or examples of how to do this would be great.
The files might look like this in D:\dir1start
Fechai_123_xxxxxxxxxxxx.zipChange of date and time
Fechai_234_xxxxxxxxxxxx.zipChange of date and time
Fechai_345_xxxxxxxxxxxx.zipChange of date and time
Fechai_456_xxxxxxxxxxxx.zipChange of date and time
(Video) How to Extract Information From Notepad to Excel : MS Excel Tipsdirectories
D:\dir1start
\\Server1\dir
\\Server2\dir
\\Server3\dir etcetera etcetera.
D:\dir1Archived
Historic
D:\Registros\FileCopy_DATE_TIME.log
(csv format log of files copied, moved, path successful or failed)
the script would be
- Acquire me D:\dir1starta "archivo_123_xxx.zip"[more than 1 hour]
copy to \\server1\dirmi \\Server2\dir
- Acquire me D:\dir1start a "archivo_234_xxx.zip"[more than 1 hour]
copy to \\server1\dirmi \\Server2\dirmi \\server3\dir - Acquire me D:\dir1start a "Archivo_345_xxx.zip"[more than 1 hour]
copy to \\server1\dirmi \\server3\dir - Acquire me D:\dir1start a "Archivo_456_xxx.zip"[more than 1 hour]
Copy to \\Server2\dirmi \\server3\dir - Find ALL archive dates [more than 30 days minimum]ir a D:\dir1Archivo
- Detailed log and CSV output results to D:\Logs\FileCopy_DATE_TIME.log
Gracias
- Edited by C_T_Gecko Wednesday, June 26, 2013 9:58 PM
- Acquire me D:\dir1starta "archivo_123_xxx.zip"[more than 1 hour]
Respondedor
-
Sign in to vote
Here's a little function that will help you with registration:
Función Write-Log($LogText){$LogEntry=@"$((Get-Date).ToString("dd-MM-yyyy HH:mm:ss")) > $($LogText)"@$LogEntry$LogEntry | Add-Content 'c:\temp\testlog.txt'}Write-Log "Gestartet"
Post what problems you have and we advise you.
See the Get-ChildItem cmdlet in Powershell Help for your file filtering needs. Try -include with wildcards and -recurse
Get-Help Get-ChildItem -Completo
Inspired by Carlsberg.
- Edited by RiffyRiot Wednesday, June 26, 2013 11:28 PM to edit
- Marked as answer by Yan Li_ Wednesday, July 3, 2013 5:34 am
Wednesday, June 26, 2013 11:20 PM
-
Sign in to vote
The directory listing would be something like an array that could be read from a file or statically added to the beginning of the file, something like
$DirsToCheck = @('D:\dir1start','\\Server1\dir') etc...
So the script would be like this,
function Write-Log($LogText){
$RegistrationEntry="@"
$((Obtener-Fecha).ToString("dd-MM-aaaa HH:mm:ss")) > $($LogText)
"@
$registry entry
$LogEintrag | Add content 'c:\temp\testlog.txt'
}$DTLastHour = (Get-Date).AddHours(-1)
$DTOver30Days = (Get-Date).AddDays(-30)foreach ($DirToCheck em $DirsToCheck) {
$OldFilesInDir = Get-ChilItem $DirToCheck -Include "*.zip" -Recurse | Uno de {$_.LastWriteTime -le "$DTOver30Days"}
$NewFilesInDir = Get-ChilItem $DirToCheck -Include "*.zip" -Recurse | Uno de {$_.LastWriteTime -ge "$DTLastHour"}foreach ($OldFile en $OldFilesInDir) {
Move-Item -Path $OldFile -Destino D:\dir1Arquivo\
Write-Host "$($OldFile) film to D:\dir1Archive\"
Write-Log "Movido $($OldFile) para D:\dir1Archive\"
}
foreach ($NewFileIn $NewFilesInDir) {
Copy-Item -Path $OldFile -Destination\\server1\dirMove-Item -Path $OldFile -Destino\\server2\dir
Write-Host "New file $NewFile"
Write record "New file $NewFile"
}
}(Video) How to remove Plagiarism from Article- Edited by Jimmy James_ Thursday, June 27, 2013 12:14 PM To edit..
- Marked as answer by C_T_Gecko Friday, July 5, 2013 9:53 PM
Thursday, June 27, 2013 12:11 AM
All the answers
-
Sign in to vote
Here's a little function that will help you with registration:
Función Write-Log($LogText){$LogEntry=@"$((Get-Date).ToString("dd-MM-yyyy HH:mm:ss")) > $($LogText)"@$LogEntry$LogEntry | Add-Content 'c:\temp\testlog.txt'}Write-Log "Gestartet"
Post what problems you have and we advise you.
See the Get-ChildItem cmdlet in Powershell Help for your file filtering needs. Try -include with wildcards and -recurse
Get-Help Get-ChildItem -Completo
Inspired by Carlsberg.
- Edited by RiffyRiot Wednesday, June 26, 2013 11:28 PM to edit
- Marked as answer by Yan Li_ Wednesday, July 3, 2013 5:34 am
Wednesday, June 26, 2013 11:20 PM
-
Sign in to vote
The directory listing would be something like an array that could be read from a file or statically added to the beginning of the file, something like
$DirsToCheck = @('D:\dir1start','\\Server1\dir') etc...
So the script would be like this,
function Write-Log($LogText){
$RegistrationEntry="@"
$((Obtener-Fecha).ToString("dd-MM-aaaa HH:mm:ss")) > $($LogText)
"@
$registry entry
$LogEintrag | Add content 'c:\temp\testlog.txt'
}$DTLastHour = (Get-Date).AddHours(-1)
$DTOver30Days = (Get-Date).AddDays(-30)foreach ($DirToCheck em $DirsToCheck) {
$OldFilesInDir = Get-ChilItem $DirToCheck -Include "*.zip" -Recurse | Uno de {$_.LastWriteTime -le "$DTOver30Days"}
$NewFilesInDir = Get-ChilItem $DirToCheck -Include "*.zip" -Recurse | Uno de {$_.LastWriteTime -ge "$DTLastHour"}foreach ($OldFile en $OldFilesInDir) {
Move-Item -Path $OldFile -Destino D:\dir1Arquivo\
Write-Host "$($OldFile) film to D:\dir1Archive\"
Write-Log "Movido $($OldFile) para D:\dir1Archive\"
}
foreach ($NewFileIn $NewFilesInDir) {
Copy-Item -Path $OldFile -Destination\\server1\dirMove-Item -Path $OldFile -Destino\\server2\dir
Write-Host "New file $NewFile"
Write record "New file $NewFile"
}
}(Video) How to Pull Data from Another Sheet based on Criteria in Excel & Google Sheets?- Edited by Jimmy James_ Thursday, June 27, 2013 12:14 PM To edit..
- Marked as answer by C_T_Gecko Friday, July 5, 2013 9:53 PM
Thursday, June 27, 2013 12:11 AM
-
Sign in to vote
Not bad, Jimmy... the write log function already uses write and log inputs for the host, so you don't need to include these instructions.
Inspired by Carlsberg.
Thursday, June 27, 2013 12:16 PM
-
Sign in to vote
ok, sorry for the delay. The provided examples were great. I messed it up a bit, but parse-copy-copy-move works great. If there is a more preferred or correct method, please let me know.
The only additional functionality I need to add is more robust logging of the routine.
If any of you know of a way
1. Record the From and To location of each copy or move
2. Report the success, failure or failure of the copy move
3. Circular log or an archived log that is reset each day. (The size of the singular log file would quickly get out of control.)$DirsToCheck = @('D:\Server1\TestStart') Función Write-Log($LogText){$LogEntry=@"$((Get-Date).ToString("dd-MM-aaaa HH:mm:ss" )) > $($TextoRegistro)"@$EntradaRegistro$EntradaRegistro | Add-Content 'D:\FileCopyLogs\FilesProcessed.txt' } $DTLastHour = (Get-Date).AddMinutes(-60) $DTOver30Days = (Get-Date).AddDays(-30) foreach ($DirToCheck em $DirToCheck) {$OldFilesInDir = Get-Childitem $DirToCheck -Include "*.zip" -Recurse | Onde {$_.LastWriteTime -le "$DTOver30Days"} $NewABC = Get-Childitem $DirToCheck -Include "*ABC*.zip" -Recurse | Onde {$_.LastWriteTime -ge "$DTLastHour"}$NewDEF = Get-Childitem $DirToCheck -Include "*DEF*.zip" -Recurse | Onde {$_.LastWriteTime -ge "$DTLastHour"}$NewGHI = Get-Childitem $DirToCheck -Include "*GHI*.zip" -Recurse | Onde {$_.LastWriteTime -ge "$DTLastHour"}$NewJKL = Get-Childitem $DirToCheck -Include "*JKL*.zip" -Recurse | Onde {$_.LastWriteTime -ge "$DTLastHour"}$NewMNO = Get-Childitem $DirToCheck -Include "*MNO*.zip" -Recurse | Onde {$_.LastWriteTime -ge "$DTLastHour"}$NewPQR = Get-Childitem $DirToCheck -Include "*PQR*.zip" -Recurse | Onde {$_.LastWriteTime -ge "$DTLastHour"}$NewSTU = Get-Childitem $DirToCheck -Include "*STU*.zip" -Recurse | Onde {$_.LastWriteTime -ge "$DTLastHour"} $NewNAVFMTM = Get-Childitem $DirToCheck -Include "*NAVFMTM*.zip" -Recurse | Onde {$_.LastWriteTime -ge "$DTLastHour"} #Move y arquiva todos los archivos de 30 días antes de cada ($OldFile em $OldFilesInDir) {Move-Item -Path $OldFile -Destination C:\testArchive\Write- Host "Movido Arquivado $OldFile to C:\testArchive\"Write-Log "Movido $OldFile archivado a C:\testArchive\"}#Copy ABC Data Filesforeach ($NewFile in $NewABC) {Copy-Item -Path $NewFile - Destino \\FileShare1Copy -Item -Path $NewFile -Destino \\FileShare2Copy-Item -Path $NewFile -Destino \\FileShare3Copy-Item -Path $NewFile -Destino \\FileShare4Write-Host "Novo arquivo $NewFile"Write-Log "Copiar ABC $NuevoArchivo" } #Copiar archivos de datos DEF para cada uno ($NuevoArchivo en $NuevoDEF) {Copiar-Elemento -Ruta $NuevoArchivo -Destino \\ArchivoCompartido1#Copiar-Elemento -Ruta $NuevoArchivo -Destino \\ArchivoCompartido2#Copiar-Elemento -Ruta $ NewFile -Destination \\FileShare3#Copy-Item -Path $NewFile -Destination \\FileShare4Write-Host "New File $NewFile"Write-Log "Copy DEF $NewFile"}#Copy GHI Data Filesforreach ($NewFile in $NewGHI) { #Copy-Item -Ruta $NuevoArchivo -Destino \\FileShare1Copy-Item -Ruta $NuevoArchivo -Destino \\FileShare2#Copiar-Item -Ruta $NuevoArchivo -Destino \\FileShare3#Copiar-Item -Ruta $NuevoArchivo -Destino \\FileShare4Write -Host "Novo Arquivo $NewFile"Write-Log "Copy GHI $NewFile"}#Copy JKL Data Filesforreach ($NewFile in $NewJKL) {#Copy-Item -Path $NewFile -Destination \\FileShare1#Copy-Item -Path $NewFile -Destino \\FileShare2Copy-Item -Path $NewFile -Destination \\FileShare3#Copy-Item -Path $NewFile -Destination \\FileShare4Write-Host "Nuevo archivo $NewFile"Write-Log "Copiar JKL $NewFile"}# Copiar archivos de datos de MNO para alcanzar ($NewFile en $NewMNO) {#Copy-Item -Path $NewFile -Destination \\FileShare1Copy-Item -Path $NewFile -Destination \\FileShare2#Copy-Item -Path $NewFile -Destino \\FileShare3Copy- Item -Path $ NewFile -Destination \\FileShare4Write-Host "New File $NewFile"Write-Log "Copy MNO $NewFile"}#Copy PQR Data Filesforeach ($NewFile in $NewPQR) {Copy-Item -Path $NewFile -Destino \\FileShare1# Copy-Item -Path $NewFile -Destino \\FileShare2#Copy-Item -Path $NewFile -Destino \\FileShare3Copy-Item -Path $NewFile -Destino \\FileShare4Write-Host "Novo arquivo $NewFile"Write- Log "Copiar PQR $NewFile"}#Copiar archivos de datos de STU para alcanzar ($NewFile en $NewSTU) {Copiar elemento -Ruta $Nuevoarchivo -Destino \\Archivo compartido1#Copiar elemento -Ruta $Nuevoarchivo -Destino \\Archivo compartido2#Copiar elemento -Path $NewFile -Destino \\FileShare3Copy-Item -Path $NewFile -Destination \\FileShare4Write-Host "Novo arquivo $NewFile"Write-Log "Copiar STU $NewFile"}}
Friday, July 5, 2013 9:41 PM
-
Sign in to vote
It's just a matter of sending whatever you want to write to the write-log function...
Before/after each copy command you can: write-log "Copying $($Newfile) to\\share1"
To have a log for each day, add some logic to the write log function:
Función Write-Log($LogText){$LogEntry=@"$((Get-Date).ToString("dd-MM-yyyy HH:mm:ss")) > $($LogText)"@$logname=" D:\FileCopyLogs\FilesProcessed_$((Get-Date).ToString("dd-MM-aaaa")).txt"$LogEntry$LogEntry | Agregar conteúdo $logname }
To log errors, you can do the following:
copy c:\temp\boo.txt -Destination C:\ProgramData\boo.txtif($?){Write-Log "Copy successful"}else{Write-Log "The copy failed because $($error[0] ) . " }
the $? is a PowerShell automatic variable that returns true or false based on the success of the last command.
$error is a PowerShell automatic variable that contains a history of reported errors. $error[0] is the last error reported.
You would need this logic after each copy command.
Inspired by Carlsberg.
- Edited by RiffyRiot Saturday, Jul 6, 2013 2:23 AM m. Change mm (minutes) to MM for the month
Saturday, July 6, 2013 2:18 am
(Video) How To Transfer Data From The Fluke 435 To The Power Log Software