Link to home
Start Free TrialLog in
Avatar of ercole1977
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
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ercole1977
ercole1977

ASKER

thank you qlemo i receive this output

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      XML-Watcher                     NotStarted    False                                ...
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.
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
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:
write-host "$basedir$fname.xml"

Open in new window

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
[String] $fname = $eventArgs.Name -replace "\.ack$"

Open in new window


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.
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!
Sorry my fault....now i have:

$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

Open in new window


But it continues to delete the ack but doesnt move the xml.
Ok corrected lines 10 and 11

write-host "$basedir\$fname.xml"
  move-item "$basedir\$fname.xml" $destdir

Open in new window


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.
QLEMO thank you very much for your precious help!
FOOTECH thank you very much too!