duankuangxie9070 2016-11-12 12:55
浏览 45

如何检查文件是否正在使用,即正在对该文件中的数据执行某些命令?

I have to write a program which checks if a particular directory on my folder has any files (of a specific extension), and if it finds any files, it reads them one by one and loads data from them into a database.

This is the rough algorithm in my mind:

  1. Using an infinite while() loop, continuously keep checking if the directory has any files of that particular extension (e.g. check if the directory has any *.xml files). I can use the PHP glob() function.

  2. If yes, then in a foreach loop, read data from each file and load it into the database.

  3. Once a file's data has been loaded, delete it.

My Question:

I will be constantly checking if there any .xml files in the directory. This means that many times I will get a true (meaning/saying "Yes, there are .xml files in the directory") even for the files whose data is BEING loaded.

So once a file has been found in the directory, I need a check which checks if its data is in the process of being loaded into a database. How do I check that?

The process of data-loading is that I extract useful data from the file into a .csv file and then use LOAD DATA INFILE SQL query to load the data into my MySQL database.

  • 写回答

1条回答 默认 最新

  • dotxxh0998 2016-11-12 13:15
    关注

    One solution is to use inotifywait as suggested in this answer: https://stackoverflow.com/a/6767891/2032943 to watch event and then act on them.

    Also if you want to see that the file is already being used by some other command, you can use linux lsof command to check if there is an open handle for the file used by some process:

    lsof | grep <filename>
    

    Note that these commands are specific to linux and will not work on windows.

    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来