[英]What can I do to automatic run a bat file when a new item on a folder is added?
I have a ps1 file which contains the code and a bat file which when i double click runs the code.我有一个包含代码的 ps1 文件和一个 bat 文件,当我双击运行代码时。 The ps1 file renames all files in that folder.
ps1 文件重命名该文件夹中的所有文件。 Here is the code:
这是代码:
get-childitem -path C:\Users\ASUS\Videos\Downloads\*.mp4 | foreach {rename-item $_ $_.name.replace("_"," ")}
How can I run the bat file to automatically rename all files that are added to that folder?如何运行 bat 文件以自动重命名添加到该文件夹的所有文件?
I saw @olaf mentioned FileSystemWatcher and, honestly, it one of the better options for what you're trying to do.我看到@olaf 提到了 FileSystemWatcher,老实说,它是您尝试做的更好的选择之一。
Alternatively, you can use Task Scheduler to run at specific times, dates, actions, etc. There's also no need to point to the bat file, it can run ps1 files just as easy without having to have.bat execute the.ps1.或者,您可以使用任务计划程序在特定时间、日期、动作等运行。也无需指向 bat 文件,它可以像运行 ps1 文件一样简单,而无需让 .bat 执行 .ps1。
Change your ps1 to the following.将您的 ps1 更改为以下内容。 It's passing only the items that were created less than 30 minutes ago;
它只传递不到 30 分钟前创建的项目; can be changed to days, minutes, etc. Then, it's just renaming them(:
可以更改为天,分钟等。然后,它只是重命名它们(:
Get-ChildItem -Path C:\Users\ASUS\Videos\Downloads\*.mp4 | Where-Object {$_.CreationTime -gt (Get-Date).AddMinutes(-30)}| foreach {rename-item $_.FullName $_.name.replace("_"," ")}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.