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
ercole1977Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Yes, you can register a FileSystemWatcher looking for *.ack files:
$basedir = 'D:\XML-Files-Temp\'
$destdir = 'D:\XML-Files\'
$script:ErrorActionPreference = 'Continue'
cls
$fswexe = New-Object System.IO.FileSystemWatcher($basedir, '*.ack')
$action = {
    [String] $fqdn  = $eventArgs.Fullpath
    [String] $fname = $eventArgs.Name 
    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' -action $action -SourceIdentifier 'XML-Watcher'

Open in new window

You need the PowerShell process running this code to stay open all time, e.g. you can create a scheduled task on system startup or login running
  powershell -NoExit -WindowStyle Hidden -file C:\Scripts\XML-Watcher.ps

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ercole1977Author Commented:
thank you qlemo i receive this output

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      XML-Watcher                     NotStarted    False                                ...
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
Webinar: Cyber Crime Becomes Big Business

The rising threat of malware-as-a-service is not one to be overlooked. Malware-as-a-service is growing and easily purchased from a full-service cyber-criminal store in a “Virus Depot” fashion. Join us in our upcoming webinar as we discuss how to best defend against these attacks!

ercole1977Author Commented:
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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
footechCommented:
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.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
That's correct. I read it wrong from my existing script - name includes the file extension.
ercole1977Author Commented:
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!
ercole1977Author Commented:
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.
ercole1977Author Commented:
Ok corrected lines 10 and 11

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

Open in new window


It works!!
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
ercole1977Author Commented:
QLEMO thank you very much for your precious help!
FOOTECH thank you very much too!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.