窗口的批处理文件 - 从一个文件路径删除目录 [英] Window batch file - removing the directory from a file path

查看:136
本文介绍了窗口的批处理文件 - 从一个文件路径删除目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图搜索我的code碱基对所有的的JScript 目录,然后得到使用下面的批处理脚本这些目录中的相对文件的列表...

 关闭@echoSETLOCAL ENABLEEXTENSIONS enabledelayedexpansionFOR / F令牌= *%% G IN('DIR / B / AD / S jscripts')DO(
    CD %%摹
    CD开发    SETcurrentDir =!CD!
    如果不是currentDir:!〜-1==\\设置currentDir = currentDir \\!    FOR / R %% f由于(* .js文件)DO(
        SETrelativePath = %% F
        SETrelativePath = relativePath:%!currentDir%=!        ECHO!relativePath!
    )

这一切都按预期工作,直到它到达...

  SETrelativePath = relativePath:%!currentDir%=!

我可以找出我需要写什么格式转...

  C:\\目录\\ JScript中\\ dev的\\ file.js

到...

  file.js

请帮帮忙!


其他信息

目录设置如下

  DIR
    jscripts
        开发
            file.js
            file2.js
        生活
            file.js
            file2.js
DIR2
    jscripts
        开发
            file.js
            file2.js
        生活
            file.js
            file2.js

我要找到所有的 jscripts 目录,CD放进去,让所有的JS文件的列表相对于开发目录


解决方案

 关闭@echo
SETLOCAL ENABLEEXTENSIONS enabledelayedexpansionREM从哪里开始
PUSHDC:\\地方\\ global2_root \\REM搜索..... \\ dev的目录
在/ F令牌= *%% D('DIR / AD / S / B ^ | FINDSTR / E\\\\开发')做(
    物权变更到该目录
    PUSHD%%〜FD
    现在呼应!CD!
    目录里面REM流程文件
    在(* .js文件)%% f执行(
        回声%%˚F
    )
    REM返回previous目录
    POPD

REM回到初始目录
POPDREM清理
ENDLOCAL

I'm trying to search my code base for all jscript directories and then get a list of relative files within those directories using the following batch script...

@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!
    )
)

It all works as expected until it gets to...

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

I can figure out what format I need to write this in to turn...

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

into...

file.js

Please help!


Additional information

The directory setup is as follows

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


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

I want find all jscripts directories, CD into them, get a list of all JS files relative to the dev directory

解决方案

@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

这篇关于窗口的批处理文件 - 从一个文件路径删除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆