批处理文件给出“系统找不到指定的文件”,但命令在 cmd 中运行良好

use*_*447 6 windows batch command-line windows-8

我正在尝试运行一个简单的批处理文件:

@echo off      <-- don't print this line or any of the preceeding lines to the console window.
pushd "K:\"    <-- in the quoted directory
for %%j in (*) <-- for every file in the directory
do
if %%~zj       <-- if the size of the file
lss 37000      <-- is less than 37k
del %%j        <-- delete the file
popd           <-- go back to original directory.
Run Code Online (Sandbox Code Playgroud)

我开始在@echo offand处出错pushd,但如果我尝试pushdcmd.exe它运行得很好。我确定我错过了一些简单的东西。

有任何想法吗?

Mar*_*len 7

当您使用程序启动批处理文件并且批处理文件没有cmd.exe /c首先调用时,就会发生这种情况。没有pushd.exe,它是cmd.exe. 但是当您的程序调用您的批处理文件时,它会直接启动它 - 没有cmd.exe. (是的,这很奇怪。)

解决方案:无论你从哪里运行它,在它之前 cmd.exe /c

因此,如果您正在运行,deletelittlefiles.bat请将其更改为cmd.exe /c deletelittlefiles.bat.

来源:我是一个软件测试团队的实验室经理,我们的测试工具可以但不必在cmd.exe.