首页 \ 问答 \ 在目录中查找最新文件,然后选择具有相同DateTime的所有文件(Find newest file in directory, then select all files with same DateTime)

在目录中查找最新文件,然后选择具有相同DateTime的所有文件(Find newest file in directory, then select all files with same DateTime)

使用此命令,我可以在目录中搜索最新文件:

gci C:\temp | sort LastWriteTime -descending | select -first 1

我的目录相当大,包含60'000 + txt文件。 运行此命令需要:

PS C:\xy> measure-command {gci C:\temp | sort LastWriteTime -descending | select -first 1}


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 13
Milliseconds      : 465
Ticks             : 134657703
TotalDays         : 0.000155853822916667
TotalHours        : 0.00374049175
TotalMinutes      : 0.224429505
TotalSeconds      : 13.4657703
TotalMilliseconds : 13465.7703

如您所见,此命令需要很长时间才能完成。

我的任务是获取具有与命令返回的文件相同的LastWriteTime属性的所有文件(到分钟)。

我试过这样的事情:

$file = gci C:\temp -OutVariable files | sort LastWriteTime -descending | 
        select -first 1 | % { $_.LastWriteTime }
$myfiles = $files | ? {$_.LastWriteTime -like $file}

当我比较找到LastWriteTime的$file和应该包含具有相同LastWriteTime的所有文件的$myfiles$myfiles总是只包含一个文件 - 这是因为每个对象的秒数不同:

PS C:\xy> $file

Montag, 21. November 2016 13:10:08

如何通过比较lastwritetime属性找到我需要的文件?


With this command, I can search for the newest file in a directory:

gci C:\temp | sort LastWriteTime -descending | select -first 1

My directory is rather large, containing 60'000+ txt files. Running this command takes:

PS C:\xy> measure-command {gci C:\temp | sort LastWriteTime -descending | select -first 1}


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 13
Milliseconds      : 465
Ticks             : 134657703
TotalDays         : 0.000155853822916667
TotalHours        : 0.00374049175
TotalMinutes      : 0.224429505
TotalSeconds      : 13.4657703
TotalMilliseconds : 13465.7703

As you can see, this command takes a long time to finish.

My task is to get all the files which have the same LastWriteTime Property as the file which the command returns (to the minute).

I tried something like this:

$file = gci C:\temp -OutVariable files | sort LastWriteTime -descending | 
        select -first 1 | % { $_.LastWriteTime }
$myfiles = $files | ? {$_.LastWriteTime -like $file}

when I compare $file which is the found LastWriteTime, and $myfiles which should contain all files with the same LastWriteTime, $myfiles always includes only one file - this is because the seconds are different for each object:

PS C:\xy> $file

Montag, 21. November 2016 13:10:08

How can I find the files I need by comparing their lastwritetime properties?


原文:https://stackoverflow.com/questions/40720034
更新时间:2022-02-01 12:02

最满意答案

如果要使用完全相同的配置实例(类Props),可以将其实例绑定为具有提供程序绑定单例 。 这当然不是唯一的解决方案,但对我来说这是有道理的。

这是一个例子:

定义提供者:

public class PropsProvider implements Provider<Props>
{
    @Override
    public Props get()
    {
        ...read and return Props here...
    }
}

在单例范围内使用提供程序绑定:

bind(Props.class).toProvider(PropsProvider.class).in(Singleton.class);

注入您的配置:

@Inject
public Connection(Props props) {
    this.props = props;
}

您可以阅读文档:

单身人士最适合:

  • 有状态对象,例如配置或计数器
  • 构造或查找昂贵的对象
  • 占用资源的对象,例如数据库连接池。

也许您的配置对象与第一个和第二个条件匹配。 我会避免从模块中读取配置。 在这看到原因。

我在一些单元测试案例中使用按需注入,我想在被测组件中注入模拟依赖项并使用字段注入(这就是为什么我试图避免现场注入:-))我宁愿不使用InjectMocks由于某些原因。

这是一个示例:

零件:

class SomeComponent
{
    @Inject
    Dependency dep;

    void doWork()
    {
        //use dep here
    }
}

测试本身:

@RunWith(MockitoJUnitRunner.class)
public class SomeComponentTest
{
    @Mock
    private Dependency mockDependency;

    private SomeComponent componentToTest;

    @Before
    public void setUp() throws Exception
    {
        componentToTest = new SomeComponent();

        Injector injector = Guice.createInjector(new AbstractNamingModule()
        {
            @Override
            protected void configure()
            {
                bind(Dependency.class).toInstance(mockDependency);
            }
        });

        injector.injectMembers(componentToTest);
    }

    @Test
    public void test()
    {
         //test the component and/or proper interaction with the dependency
    }

}

If you want to use the very same configuration instance (of class Props) you could bind its instance as a singleton with a provider binding. This is of course not the only solution, but it makes sense for me.

Here is an example:

Define a provider:

public class PropsProvider implements Provider<Props>
{
    @Override
    public Props get()
    {
        ...read and return Props here...
    }
}

Use a provider binding in singleton scope:

bind(Props.class).toProvider(PropsProvider.class).in(Singleton.class);

Inject your configuration:

@Inject
public Connection(Props props) {
    this.props = props;
}

You may read in the documentation:

Singletons are most useful for:

  • stateful objects, such as configuration or counters
  • objects that are expensive to construct or lookup
  • objects that tie up resources, such as a database connection pool.

Maybe your configuration object matches the first and the second criteria. I would avoid reading the configuration from within the module. See why here.

I've used on-demand injection in a few unit test cases where I wanted to inject mock dependencies in the component under test and field injection was used (that's why I try to avoid field injections :-) ) AND I preferred not to use InjectMocks for certain reasons.

Here is a sample:

Component:

class SomeComponent
{
    @Inject
    Dependency dep;

    void doWork()
    {
        //use dep here
    }
}

The test itself:

@RunWith(MockitoJUnitRunner.class)
public class SomeComponentTest
{
    @Mock
    private Dependency mockDependency;

    private SomeComponent componentToTest;

    @Before
    public void setUp() throws Exception
    {
        componentToTest = new SomeComponent();

        Injector injector = Guice.createInjector(new AbstractNamingModule()
        {
            @Override
            protected void configure()
            {
                bind(Dependency.class).toInstance(mockDependency);
            }
        });

        injector.injectMembers(componentToTest);
    }

    @Test
    public void test()
    {
         //test the component and/or proper interaction with the dependency
    }

}

相关问答

更多
  • 我认为首选的Guice模式是这样的: public class HolderPatter { static class Bar { @Inject Bar(BarDependency dependency) {} } static class Baz { @Inject Baz(BazDependency dependency) {} } static class BarHolder { @Inject(optional=true) Bar value = n ...
  • 是。 它是。 (defn foo [x] (require 'clojure.string) ((resolve 'clojure-string/split) x #"\s")) Yes. It is. (defn foo [x] (require 'clojure.string) ((resolve 'clojure-string/split) x #"\s"))
  • 您是否尝试在每个构造函数中注入Provider而不是实际实例? 如果你不需要构造函数代码中的其他实例,那么只需将Provider存储到final字段,稍后再使用该字段(通过调用get() )。 Did you try injecting a Provider in each constructor instead of the actual instances? If you don't need the other instance in the code of constructor, th ...
  • 通常,是:注入Provider以便稍后获取您的对象,并且不会在必要时直接保留您的注入器。 理想情况下,您的引导程序应该就是:它应该实例化您的Injector并获得某种整个应用程序实例 。 这一点尤为重要,因为Guice有助于测试,因此最大化Guice应用程序的一部分是一个好主意。 public class YourApplication { public static void main(String[] args) { // Only three lines exist outside th ...
  • 你是对的,这种注射只能通过使用工厂来解决。 如果你有Foo(A a, B b)注入A并且B在运行时传递,你将需要一个Factory FooFactory.createFoo(B b) ,它在内部保存对A的引用。 幸运的是,Guice有@Assisted Injection的概念。 您必须提供工厂的界面,但可以将实现的“魔力”留给Guice。 它有很好的文档记录: https : //github.com/google/guice/wiki/AssistedInject 您需要额外依赖com.google.i ...
  • 如果你真的想使用AJAX(如问题中所述),你也可以这样做。 首先,您通常使用XMLHttpRequest下载javascript,下载完成后,您可以eval()或将其插入生成的标记中。 function xhr_load(url, callback) { xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if (xhr.status == 200 ...
  • 如果要使用完全相同的配置实例(类Props),可以将其实例绑定为具有提供程序绑定的单例 。 这当然不是唯一的解决方案,但对我来说这是有道理的。 这是一个例子: 定义提供者: public class PropsProvider implements Provider { @Override public Props get() { ...read and return Props here... } } 在单例范围内使用提供程序绑定: bind ...
  • WPF ListBox使用VirtualizingStackPanel作为其项目的布局控件。 您可以将VirtualizingStackPanel设置为仅根据需要使用以下XAML加载项: WPF ListBox's use a VirtualizingStackPanel as the layout control for its i ...
  • 看看Binder doc: http://google-guice.googlecode.com/git/javadoc/com/google/inject/Binder.html requireExplicitBindings()可能就是你所需要的。 通过使用模块中的语句,禁用自动绑定,仅注入通过bind()或provides类配置的类。 Apparently, the only way to do this is to write a provider method for each prohibite ...
  • 将此绑定放入模块时: bind(IManager.class).to(Manager.class); Guice将尝试创建Manager类的新实例。 它查找使用@Inject注释的一个(但只有一个)构造函数或作为非私有的后备零参数构造函数。 这是Guice将使用的构造函数: @Inject public Manager(Person person) { this.person=person; } 现在遵循相同的规则,Guice会尝试使用适当的构造函数来实例化Person ,它会卡在这里: @Inj ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)