2015-04-05 138 views
0

我有一個有趣的案例有一個批處理文件,它做以下的事情打轉轉:批處理文件識別

  1. 移動廣口瓶中,目錄
  2. 將所有的jar文件目錄的字符串(類路徑)
  3. 運行的jar

我得到的問題是,它沒有找到,同時建立字符串的新罐,但它發現現有的罐子,所以如果我運行兩次都沒問題。

要檢測的罐子,我使用的是這樣的:

set cp= 

for %%a in ("jars/*.jar") do call :concat jars/%%a 

:concat 

set cp=%cp%;%1 

如果任何人有一個解釋或解決方案,我想聽聽吧!

+1

我無法使用提供的代碼複製您的問題。具體來說,'%cp%'變量被設置爲它應該是的。 – SomethingDark 2015-04-05 12:55:23

+0

可能你需要'exit/b 0'在':concat'之前? – npocmaka 2015-04-05 13:24:09

回答

0

我發現這個問題 - 副本是來自全國各地的網絡,所以這需要時間。當我添加timeout 4 > NUL或類似的東西時,一切都很好。

0

請參閱!var! VS%VAR%

& seperates commands on a line. 

&& executes this command only if previous command's errorlevel is 0. 

|| (not used above) executes this command only if previous command's errorlevel is NOT 0 

> output to a file 

>> append output to a file 

< input from a file 

| output of one command into the input of another command 

^ escapes any of the above, including itself, if needed to be passed to a program 

" parameters with spaces must be enclosed in quotes 

+ used with copy to concatinate files. E.G. copy file1+file2 newfile 

, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,, 

%variablename% a inbuilt or user set environmental variable 

!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command 

%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name. 

%* (%*) the entire command line. 

%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file. 


. 
-- 
+0

你會建議我在哪裏使用它?我試過這個:'for %% a in(!lib_loc!)do call:concat jars/%% a',但它不起作用 – orirab 2015-04-05 12:20:34

+0

@orirab - 它會在':concat'中。 'set cp =!cp!;%1' – SomethingDark 2015-04-05 12:43:44

+0

@SomethingDark - 沒有工作。此外,這個缺陷似乎並不在concat本身中,而是在它被調用的次數和參數上 – orirab 2015-04-05 12:47:00

0
set "cp=" 

setlocal enableDelayedExpansion 
for %%a in ("jars/*.jar") do set "cp=!cp!;"%%~a"" 
endlocal &&(
    set "cp=%cp%" 
) 
echo %cp%