2012-06-05 159 views
4

我需要能夠測試,看看目錄是否存在於Android設備的SD卡上,然後推送幾個文件到該目錄,如果它確實存在。檢查目錄是否存在使用ADB,並推送文件,如果它

到目前爲止,我有這樣的:

adb %argument% shell if [ -e /sdcard/ ]; then echo "it does exist"; else echo "it does not exist"; fi; 

但我怎麼可以讓我的批處理腳本知道該目錄存在,以便它可以繼續執行文件推到該目錄?

回答

3

以下是我在批處理腳本都做:

set cmd="adb shell ls | find /c "theFile" " 
FOR /F %%K IN (' !cmd! ') DO SET TEST=%%K 
if !TEST! GTR 0 (
    echo the file exists 
) else (
    echo the file does not exist 
) 

有可能是適合的文件名的多個文件,所以我選擇把它考大於0


要測試精確匹配和使用bash在Linux中(reference):

FILENAME_RESULT=$(adb shell ls/| tr -d '\015'|grep '^fileName$') 

if [ -z "$FILENAME_RESULT" ]; 
then 
     echo "No fileName found." 
else 
     echo "fileName found." 
fi 
1

我想你應該列出目錄dir or ls然後使用grep分析出來。如果grep發現目錄腳本做某事。

+0

在windows機器上沒有grep。並且不想下載外部實用程序來實現此目標。 – prolink007

+0

你應該在windows中閱讀find和findstr命令。 – user902691

+0

我已經意識到'find',但我該如何使用find來設置一些標誌,告訴我的下一個命令將'push'文件推送到設備上? – prolink007

0

1)只要使用adb外殼LS /文件路徑> fileifpresent如果「沒有這樣的文件或目錄」目前

2)用grep在本地,然後NO

Else Directory Present 
0

以下是我會做的檢查命令的退出狀態

MyFile="Random.txt" 
WorkingPath="/data/local/tmp/RandomFolder" 

IsDir=`adb shell ls $WorkingPath &> /dev/null ; echo "$?"` 

if [ $IsDir == 0 ] ; then 

    echo "Exist! Copying File To Remote Folder" 

    adb push $MyFile $WorkingPath 

else 

    echo "Folder Don't Exist! Creating Folder To Start Copying File" 

    adb shell mkdir $WorkingPath 

    adb push $MyFile $WorkingPath 

fi 
相關問題