我需要在一个旧版本的Windows 10上完美工作的批处理文件,但在一个较新的版本上都不适用
我有此批次比较两个文件的修改日期,这些文件在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过将
/Q
选项与xcopy
一起使用,无需将findstr
传播。因此,您可以为 /f < /code> ifeas使用所选
的此修改:
我添加了一些其他“调试”代码,以帮助您确定每个不同系统版本中发生的情况,并纠正您的假设<代码> 0 表示
%_ file2%
是较新的,而两者都可以注入相同的情况下。By including the
/Q
option withXCOPY
, there is no need to pipe through toFINDSTR
.You could therefore use this modification of your chosen
FOR /F
idea: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.您的问题似乎是通过
xcopy
的响应,通过findstr
进行过滤,要么是1 file(s)
或0文件(s)
,此字符串应用于newer
您的如果语句,则将其解决到
以便您获得语法错误。
要解决,请从for/f 中删除
“ delims =”
,然后将使用包含 space 的默认分列器,然后返回默认的默认分隔板令牌,是1
或0
。唯一真正的难题是它如何在您较早的Win10中起作用。
强制性讲道:
使用
设置“ var = value”
设置字符串值 - 这避免了落后空间引起的问题。不要分配终端\
,space或“
- 从元素中构建路径名 - 违反直觉,它可能会使过程更容易。当您使用点击点击时-Giggle方法执行批处理,如果找到语法 - 脚本或完成脚本,则批处理窗口将关闭。和回家错误,但最好打开'命令提示符'并从 ,并显示任何(错误)
,
窗口将保持打开状态
在那里
“ delims =”
传递我描述的字符串 - 但是 -set/a
(而不是我期望的是,set
)会抓住该数字,然后失败(因此,2&gt; nul
)因此 - 我发现,如果
file2
不存在,则该脚本停止等待响应xcopy
xcopy < /code>'s问题“文件2是文件还是目录?'
这可能是单击批次失败的原因。
所以 - 我建议
Your problem appears to be that the response from
xcopy
, as filtered throughfindstr
will either be1 File(s)
or0 File(s)
and this string is applied tonewer
Your
if
statement then is resolved toSo you get a syntax error.
To resolve, remove the
"delims="
from thefor /f
which will then use the default delimiters which includes Space and return the default first token, which is1
or0
.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 - theset /a
(rather than what I expected,set
) would grab that number, then fail (hence the2>nul
)So - what I found is that if
file2
does not exist, the script stops waiting for a response toxcopy
's question "is file2 a file or directory?`This may be the cause of the failure on clicking the batch.
So - I'd suggest