如何将Unix shell脚本修改为Windows批处理文件?

问题描述:

我需要将以下Unix脚本转换为在Windows上运行。我假设cd命令将更改为拨打direcho命令更改为拨打print如何将Unix shell脚本修改为Windows批处理文件?

脚本检查给定文件的目录,如果该文件不存在,则脚本循环/休眠,然后再次检查。

我主要是努力如何最好地转换变量和循环命令,任何提示/提示将不胜感激?

cd $DIRECTORY_NAME 

echo "You are currently in $(pwd) directory" 

numfiles=0 

while [ $numfiles -lt "1" ] 

do 

    export filename=`ls SWIX_BAU_success` 
    export numfiles=$(echo $filename | wc -w) 
    echo "$numfiles file found" 

if [ $numfiles -ge "1" ] 

then 

    echo "$filename file found" 
    exit 0 

else 

    echo "Incorrect number of files found, 1 file expected" 
    numfiles=0 
    sleep 300 

fi 

done 
+1

SS64有两个像样的参考文献[Windows批处理('cmd')(https://ss64.com/nt)和['bash'(https://ss64.com/bash/) 。在进行转换之前,您可能需要检查它们;您可能还想考虑使用PowerShell而不是Windows批处理。 –

+0

请阅读[为什么“有人可以帮我吗?”不是一个真正的问题?](https://meta.*.com/q/284236/354577) – Chris

我只是在撤销这个问题时发布这个答案。
假设SWIX_BAU_success是只有一个文件的(子)目录。

@Echo off 

cd /D "%DIRECTORY_NAME%" 
echo You are currently in directory: %CD% 
Set numfiles=0 

For /f "tokens=1,* delims=:" %%A in (
    'Dir /B "SWIX_BAU_success" ^| findstr /n^' 
) DO Set numfiles=%%a&Set filename=%%B 

If %numfiles% equ 1 (
    echo %filename% found 
    exit /B 0 
) else (
    echo "Incorrect number of files found, 1 file expected" 
    set numfiles=0 
    Timeout /T 300 
) 
+0

感谢您的答复,这看起来不错,我会很快测试它。关于你的假设,'SWIX_BAU_success'是文件名而不是目录,是的,只有一个文件。 包含该文件的目录是您拥有'cd/D“的目录%DIRECTORY_NAME%”' – Harp

+0

如果它是一个不同的文件名,只能存在一次,这会简化为“if exists”SWIX_BAU_success“(..)Else (...)' – LotPings