ercole1977
asked on
If a file is present move another file in another folder
in a particular folder i have files created with random name for example:
file1.xml file2.xml
when these files are succesfully created, a corrispettive .ack file is created.
So i will have
file1.xml file1.ack file2.xml file2.ack
What i have to do:
Move a .xml file only if the corrispettive .ack is created.
The difficult part: file names are random and i have no control over them.
There is a way to create a .bat or a powershell to check and move with these requirements run at scheduled times?
Many thanks for your help
file1.xml file2.xml
when these files are succesfully created, a corrispettive .ack file is created.
So i will have
file1.xml file1.ack file2.xml file2.ack
What i have to do:
Move a .xml file only if the corrispettive .ack is created.
The difficult part: file names are random and i have no control over them.
There is a way to create a .bat or a powershell to check and move with these requirements run at scheduled times?
Many thanks for your help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That's ok. The action itself does not display anything, this is just the PowerShell job running in the background, and when triggered performing the action as coded.
ASKER
Qlemo many thanks!
There is a problem....the .ack file in the source dir is deleted BUT the xml is not moved in dest dir...
It deletes all the ack files
There is a problem....the .ack file in the source dir is deleted BUT the xml is not moved in dest dir...
It deletes all the ack files
Do you see any error message in the PS console? And are you certain the .ack file is the last file written?
For debugging, insert this before line 10:
For debugging, insert this before line 10:
write-host "$basedir$fname.xml"
and re-execute the script. After this, each run should display the full XML file name.
I think you want to make a change to line 8 like
BTW, don't give me any points for this.
[String] $fname = $eventArgs.Name -replace "\.ack$"
BTW, don't give me any points for this.
That's correct. I read it wrong from my existing script - name includes the file extension.
ASKER
Perfect!
Only one thing...how should i change the code to perform the requested move action BUT not delete the .ack files?
Thank you very much!
Only one thing...how should i change the code to perform the requested move action BUT not delete the .ack files?
Thank you very much!
ASKER
Sorry my fault....now i have:
But it continues to delete the ack but doesnt move the xml.
$basedir = 'c:\ori'
$destdir = 'c:\dest'
$script:ErrorActionPreference = 'Continue'
cls
$fswexe = New-Object System.IO.FileSystemWatcher($basedir, '*.ack')
$action = {
[String] $fqdn = $eventArgs.Fullpath
[String] $fname = $eventArgs.Name -replace "\.ack$"
Start-Sleep 1 # to make sure the file has been closed
move-item "$basedir$fname.xml" $destdir
remove-item $fqdn
}
Unregister-Event 'XML-Watcher'
Remove-Job 'XML-Watcher' -ea SilentlyContinue
Register-ObjectEvent $fswexe -EventName 'Created
But it continues to delete the ack but doesnt move the xml.
ASKER
Ok corrected lines 10 and 11
It works!!
write-host "$basedir\$fname.xml"
move-item "$basedir\$fname.xml" $destdir
It works!!
My example path vars ended in a backslash, yours do not ;-)
The remove-item removes the .ack file. I don't know if you removed that line yet.
The remove-item removes the .ack file. I don't know if you removed that line yet.
ASKER
QLEMO thank you very much for your precious help!
FOOTECH thank you very much too!
FOOTECH thank you very much too!
ASKER
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 XML-Watcher NotStarted False ...