我需要在一个旧版本的Windows 10上完美工作的批处理文件,但在一个较新的版本上都不适用

发布于 2025-02-13 19:42:50 字数 611 浏览 3 评论 0原文

我有此批次比较两个文件的修改日期,这些文件在Windows 10 '10 .0.0.10240'上效果很好,但在Windows 10 '10 '10 .0.17134.1304'的版本中根本没有,所以我缺少什么这里? Windows 10的较新版本是否需要安装任何其他软件?

批处理文件如下:

@echo off
Set _File1="D:\Steam\steamapps\common\dota 2 beta\game\dota\bin\win64\client.dll"
Set _File2="D:\Programs\client.dll"
For /F "Delims=" %%I In ('xcopy /DHYL %_File1% %_File2% ^|Findstr /I "File"') Do set /a _Newer=%%I 2>Nul
If %_Newer%==1 (Set _Newer=%_File1%) Else (Set _Newer=%_File2%)
Echo The newest file is %_Newer%
Pause

在“ Windows 10.0.10240”上完美工作,但无法在'Windows 10.0.0.17134.1304'上启动(仅在一秒钟内打开,然后立即关闭)

I have this batch to compare the modified dates of two files that works great on an old version of Windows 10 '10.0.10240' but not at all on a slightly newer version of Windows 10 '10.0.17134.1304', so what am I missing here? Does the slightly newer version of Windows 10 require the installation of any additional software?

The batch file is as follows:

@echo off
Set _File1="D:\Steam\steamapps\common\dota 2 beta\game\dota\bin\win64\client.dll"
Set _File2="D:\Programs\client.dll"
For /F "Delims=" %%I In ('xcopy /DHYL %_File1% %_File2% ^|Findstr /I "File"') Do set /a _Newer=%%I 2>Nul
If %_Newer%==1 (Set _Newer=%_File1%) Else (Set _Newer=%_File2%)
Echo The newest file is %_Newer%
Pause

works perfectly on 'Windows 10.0.10240' but fails to start on 'Windows 10.0.17134.1304' (only opens for a fraction of a second and then immediately closes)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤星 2025-02-20 19:42:50

通过将/Q选项与xcopy一起使用,无需将findstr传播。

因此,您可以为 /f < /code> ifeas使用所选的此修改:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "_File1=D:\Steam\steamapps\common\dota 2 beta\game\dota\bin\win64\client.dll"
Set "_File2=D:\Programs\client.dll"
For %%G In ("%_File1%" "%_File2%") Do If Not Exist %%G (Echo A file is missing.& GoTo :EndIt) 
Set "_Newer="
For /F %%G In ('%SystemRoot%\System32\xcopy.exe "%_File1%" "%_File2%" /DHLQY 2^>NUL') Do If %%G Equ 1 (Set "_Newer=%_File1%") Else Set "_Newer=%_File2% or neither"
If Not Defined _Newer (Echo An error occurred.& GoTo :EndIt)
Echo %_Newer% is the newer file.

:EndIt
Pause
EndLocal
GoTo :EOF

我添加了一些其他“调试”代码,以帮助您确定每个不同系统版本中发生的情况,并纠正您的假设<代码> 0 表示%_ file2%是较新的,而两者都可以注入相同的情况下。

By including the /Q option with XCOPY, there is no need to pipe through to FINDSTR.

You could therefore use this modification of your chosen FOR /F idea:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "_File1=D:\Steam\steamapps\common\dota 2 beta\game\dota\bin\win64\client.dll"
Set "_File2=D:\Programs\client.dll"
For %%G In ("%_File1%" "%_File2%") Do If Not Exist %%G (Echo A file is missing.& GoTo :EndIt) 
Set "_Newer="
For /F %%G In ('%SystemRoot%\System32\xcopy.exe "%_File1%" "%_File2%" /DHLQY 2^>NUL') Do If %%G Equ 1 (Set "_Newer=%_File1%") Else Set "_Newer=%_File2% or neither"
If Not Defined _Newer (Echo An error occurred.& GoTo :EndIt)
Echo %_Newer% is the newer file.

:EndIt
Pause
EndLocal
GoTo :EOF

I have added some additional 'debugging' code to help you to determine what happens on each different system version, and to correct your assumption that 0 means that %_File2% is newer, when both may be datestamped the same.

请帮我爱他 2025-02-20 19:42:50

您的问题似乎是通过xcopy的响应,通过findstr进行过滤,要么是1 file(s)0文件(s),此字符串应用于newer

您的如果语句,则将其解决到

if 1 File(s)==1 ....

以便您获得语法错误。

要解决,请从for/f 中删除“ delims =”,然后将使用包含 space 的默认分列器,然后返回默认的默认分隔板令牌,是10

唯一真正的难题是它如何在您较早的Win10中起作用。

强制性讲道:

使用设置“ var = value”设置字符串值 - 这避免了落后空间引起的问题。不要分配终端\,space或 - 从元素中构建路径名 - 违反直觉,它可能会使过程更容易。

当您使用点击点击时-Giggle方法执行批处理,如果找到语法 - 脚本或完成脚本,则批处理窗口将关闭。和回家错误,但最好打开'命令提示符'并从 ,并显示任何(错误)

窗口将保持打开状态

在那里 “ delims =”传递我描述的字符串 - 但是 - set/a(而不是我期望的是,set)会抓住该数字,然后失败(因此,2&gt; nul

因此 - 我发现,如果file2不存在,则该脚本停止等待响应xcopy xcopy < /code>'s问题“文件2是文件还是目录?'

这可能是单击批次失败的原因。

所以 - 我建议

set "_newer="
if exist %_file1% set "newer=1"&if exist %_file2% for ...
if defined _newer (if _%_newer%==1....) else Echo %_file1% not found

Your problem appears to be that the response from xcopy, as filtered through findstr will either be 1 File(s) or 0 File(s) and this string is applied to newer

Your if statement then is resolved to

if 1 File(s)==1 ....

So you get a syntax error.

To resolve, remove the "delims=" from the for /f which will then use the default delimiters which includes Space and return the default first token, which is 1 or 0.

Only real puzzle is how it ever worked in your earlier Win10.

Obligatory sermon:

Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal \, Space or " - build pathnames from the elements - counterintuitively, it is likely to make the process easier.

When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You can put a pause after statements and home in on the error, but better to open a 'command prompt' and run your batch from there so that the window remains open and any (error) messages will be displayed.

--- Revision ---

After a little experimentation, I looked a little closer at the code.

The "delims=" delivers the strings I described - BUT - the set /a (rather than what I expected, set) would grab that number, then fail (hence the 2>nul)

So - what I found is that if file2 does not exist, the script stops waiting for a response to xcopy's question "is file2 a file or directory?`

This may be the cause of the failure on clicking the batch.

So - I'd suggest

set "_newer="
if exist %_file1% set "newer=1"&if exist %_file2% for ...
if defined _newer (if _%_newer%==1....) else Echo %_file1% not found
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文