We have a program that take specific XML files and imports them, then changes the file from .xml to .tmp. Once it turns to .tmp we can delete the file.
Occasionally what happens is the XML file doesn’t import properly, in which case it gets renamed from .xml to .bad and the file has to be manually adjusted (could be an improper xml file or whatever). What we’ve discovered is that roughly 90% of the time if you rename the file back to .xml it imports fine.
I’ve created a batch script to run every couple of minutes that automatically deletes the .tmp files. What I also want to do is add something to the script that renames .bad file to .xml in order to let the program try and import it again. That will cover the 90%.
In order to cover the last 10%, I’d like to set it up so that after about 10 minutes, it no longer tries to rename the file .bad back to .xml (based on the creation date).
So far, what I have is the script to delete the .tmp files:
forfiles -p "C:\path\to\xml" -m *.tmp -c "cmd /c del @PATH"
And I have the script to rename .bad to .xml:
forfiles -p "C:\path\to\xml" -m *.bad -c "cmd /c ren *.bad *.xml"
How do I tell the 2nd command to do it only for files whose creation date (not last modified date) was less than 10 minutes ago?
Updated:
Here’s the full solution (thanks to jon Z for pointing me in the right direction):
powershell.exe -command "get-childitem 'path\to\xml' -filter *.tmp | Remove-Item"
powershell.exe -command "get-childitem 'path\to\xml' -filter *.bad | where-object {$_.creationtime -gt (get-date).addminutes(-8)} | foreach-object {move-item $_.fullname ($_.fullname-replace '.bad','.xml')}"
If using powershell instead of cmd is an option, you could do the following:
to delete the files:
to rename the .bad to .xml