ウィンドウバッチファイル - ファイルパスからディレクトリを削除する

StackOverflow https://stackoverflow.com/questions/19855721

  •  29-07-2022
  •  | 
  •  

質問

コードベースをすべて検索しようとしています jscript ディレクトリ、次のバッチスクリプトを使用して、ディレクトリ内の相対ファイルのリストを取得します...

@echo off

setlocal enableextensions enabledelayedexpansion

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S jscripts') DO (
    CD %%G
    CD dev

    SET "currentDir=!cd!"
    IF NOT "!currentDir:~-1!"=="\" SET "currentDir=!currentDir!\"

    FOR /r %%F IN (*.js) DO (
        SET "relativePath=%%F"
        SET "relativePath=!relativePath:%currentDir%=!"

        ECHO !relativePath!
    )
)

それはすべて、それが到達するまで期待どおりに機能します...

SET "relativePath=!relativePath:%currentDir%=!"

私はこれを書くためにこれを書く必要がある形式を把握することができます...

c:\dir\jscript\dev\file.js

の中へ...

file.js

助けてください!


追加情報

ディレクトリのセットアップは次のとおりです

dir
    jscripts
        dev
            file.js
            file2.js
        live
            file.js
            file2.js


dir2
    jscripts
        dev
            file.js
            file2.js
        live
            file.js
            file2.js

すべてを見つけたいです jscripts ディレクトリ、CDにCDは、すべてのJSファイルのリストを取得します 開発者に対して ディレクトリ

役に立ちましたか?

解決 2

@echo off
setlocal enableextensions enabledelayedexpansion

rem Where to start
pushd "c:\wherever\global2_root\"

rem Search .....\dev directories
for /f "tokens=*" %%d in ('dir /ad /s /b ^| findstr /e "\\dev"') do (
    rem change to that directory
    pushd "%%~fd"
    echo Now in !cd!
    rem process files inside directory
    for %%f in (*.js) do (
        echo %%f
    )
    rem return to previous directory
    popd
)
rem return to initial directory
popd

rem cleanup 
endlocal

他のヒント

フルパスの使用を含む変数からファイル名と拡張機能を抽出するには %~nx0 のように

set G=c:\dir\jscript\file.js
echo %~nxG

以下を引用してください 同様の質問に対するこの答え

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file

The modifiers can be combined to get compound results:
%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only

これは「for /?」からのコピーペーストです。プロンプトのコマンド。それが役に立てば幸い。

関連している

トップ10のDOSバッチのヒント(はい、DOSバッチ...) ショー batchparams.bat (GISTとしてのソースへのリンク):

C:\Temp>batchparams.bat c:\windows\notepad.exe
%~1     =      c:\windows\notepad.exe
%~f1     =      c:\WINDOWS\NOTEPAD.EXE
%~d1     =      c:
%~p1     =      \WINDOWS\
%~n1     =      NOTEPAD
%~x1     =      .EXE
%~s1     =      c:\WINDOWS\NOTEPAD.EXE
%~a1     =      --a------
%~t1     =      08/25/2005 01:50 AM
%~z1     =      17920
%~$PATHATH:1     =
%~dp1     =      c:\WINDOWS\
%~nx1     =      NOTEPAD.EXE
%~dp$PATH:1     =      c:\WINDOWS\
%~ftza1     =      --a------ 08/25/2005 01:50 AM 17920 c:\WINDOWS\NOTEPAD.EXE
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top