主頁(yè) > 知識(shí)庫(kù) > Windows批處理更改當(dāng)前工作路徑的BAT

Windows批處理更改當(dāng)前工作路徑的BAT

熱門(mén)標(biāo)簽:智能手機(jī) 鐵路電話系統(tǒng) 銀行業(yè)務(wù) 服務(wù)器配置 呼叫中心市場(chǎng)需求 檢查注冊(cè)表項(xiàng) 網(wǎng)站文章發(fā)布 美圖手機(jī)

獲取文件夾下所有文件信息并保存到當(dāng)前目錄下test.txt中的cmd命令:

dir /s /b *.* > test.txt

保存為test.bat文件,然后雙擊test.bat后就會(huì)在該文件夾目錄下生產(chǎn)test.txt,里面會(huì)包含所有文件的路徑信息。
打開(kāi)任務(wù)計(jì)劃程序中,創(chuàng)建新的基本任務(wù),安裝步驟創(chuàng)建并把啟動(dòng)程序設(shè)置成test.bat
右鍵點(diǎn)擊該任務(wù)運(yùn)行,看是否能成功運(yùn)行test.bat
此處就出現(xiàn)問(wèn)題了:顯示該計(jì)劃任務(wù)已經(jīng)執(zhí)行完成,但是你會(huì)發(fā)現(xiàn)在剛剛文件夾的路徑下并沒(méi)有生成test.txt這個(gè)文件。
然后嘗試修改test.txt的路徑信息,用絕對(duì)路徑,然后再次運(yùn)行計(jì)劃任務(wù)

dir /s /b *.* > D:\test\test.txt

依然有問(wèn)題:雖然test.txt文件確實(shí)生成了,但是里面的文件信息并不是當(dāng)前文件夾下的,而是windows\system32下的文件信息,比如:C:\WINDOWS\system32\0409等等
問(wèn)題點(diǎn)在于當(dāng)前工作路徑,系統(tǒng)計(jì)劃任務(wù)時(shí)候默認(rèn)的當(dāng)前工作路徑是C:\WINDOWS\system32,所以顯示的是C:\WINDOWS\system32下面的文件信息
修改當(dāng)前工作路徑:在bat文件中加入一行,先把bat所在的目錄設(shè)置成當(dāng)前工作路徑

cd /d %~dp0
dir /s /b *.* > test.txt 

這樣一來(lái)就完美解決了此問(wèn)題,計(jì)劃任務(wù)能被完美執(zhí)行下來(lái)去獲取當(dāng)前文件夾下所有文件信息。

如何使用批處理文件更改當(dāng)前工作目錄

I need some help in writing a batch file. I have a path stored in a variable root as follows:

set root=D:\Work\Root

Then I am changing my working directory to this root as follows:

cd %root%

When I execute this batch file from anywhere on the D drive this is done successfully. But when I execute the same batch file from some other drive, cd %root% doesn't work.

Is there a way I can get the drive letter from the root variable? I can then change the current directory to this drive first and then cd %root% shall work.

標(biāo)簽:滄州 河南 紅河 新疆 樂(lè)山 長(zhǎng)治 沈陽(yáng) 上海

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Windows批處理更改當(dāng)前工作路徑的BAT》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266