PowerShell 获取某目录下所有的文件、文件夹,同时对获取到的文件路径字符串进行替换处理

这篇具有很好参考价值的文章主要介绍了PowerShell 获取某目录下所有的文件、文件夹,同时对获取到的文件路径字符串进行替换处理。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

PowerShell 获取某目录下所有的文件、文件夹,同时对获取到的文件路径字符串进行替换处理


前言:

为了将Windows系统下的Java编译文件与linux服务器上的文件进行比较,故进行此文件路径的获取及路径处理。
在只有文件路径 而没有实际文件的情况下的比较。


代码如下:

## grep
#########################################
#检索文件夹所在路径
$findPath = "C:\wang\PowerShell\test\grep_cd_file\source"
#输出文件所在路径
$grepKekka = "C:\wang\PowerShell\test\grep_cd_file\grepKekka.csv"
$grepKekka1 = "C:\wang\PowerShell\test\grep_cd_file\grepKekka1.csv"
$grepKekka2 = "C:\wang\PowerShell\test\grep_cd_file\grepKekka2.csv"
# 清空输出文件
Clear-Content $grepKekka -Force
Clear-Content $grepKekka1 -Force
Clear-Content $grepKekka2 -Force

# 替换 (\ : escape special characters)
$String1 = 'C:\\wang\\PowerShell\\test\\grep_cd_file\\source\\'
$String2 = '/wang/PowerShell/test/grep_cd_file/source/'

# get the files exclude folders
# -Recurse * 用于从指定路径开始递归地获取所有文件和文件夹。通过使用通配符*,可以获取路径下的所有项。
# where{!$_.PSIsContainer} ,用于找出不是文件夹的文件。它将返回在当前路径下所有不是文件夹的文件。
$fileList = Get-ChildItem $findPath -Recurse * | where{!$_.PSIsContainer}
foreach($file in $fileList) {
    #print the file name
    $file.name | Out-File -Width 5000 -FilePath $grepKekka1 -Append

    # print the file fullname
    $file.fullname | Out-File -Width 5000 -FilePath $grepKekka2 -Append
}

<# PowerShell命令-replace $String1, $String2是用来替换字符串中匹配到的部分。其中$String1是要被替换的部分,$String2是替换后的内容。这个命令可以通过正则表达式进行匹配。
在使用-replace命令时,要注意以下几点:
正则表达式最好放在单引号里面,以避免特殊字符被解释为变量。
使用$和^符号时,要区分待匹配对象是单行还是多行。对多行的文件,$表示行的结尾,^表示行的开始。对单行的模式,.会匹配所有字符,包括换行符。
#>
(Get-Content $grepKekka2) -replace $String1, $String2 -replace '\\', '/' | Set-Content $grepKekka


输出文件(grepKekka.csv):

/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/day02_eesy_01mybatisCRUD.iml
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/pom.xml
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/.idea/.gitignore
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/.idea/compiler.xml
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/.idea/jarRepositories.xml
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/.idea/misc.xml
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/.idea/workspace.xml
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/src/main/java/com/itheima/dao/IUserDao.java
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/src/main/java/com/itheima/domain/QueryVo.java
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/src/main/java/com/itheima/domain/User.java
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/src/main/resources/jdbcConfig.properties
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/src/main/resources/log4j.properties
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/src/main/resources/SqlMapConfig.xml
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/src/main/resources/com/itheima/dao/IUserDao.xml
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/src/test/java/com/itheima/test/MybatisTest.java
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/target/classes/jdbcConfig.properties
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/target/classes/log4j.properties
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/target/classes/SqlMapConfig.xml
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/target/classes/com/itheima/dao/IUserDao.class
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/target/classes/com/itheima/dao/IUserDao.xml
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/target/classes/com/itheima/domain/QueryVo.class
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/target/classes/com/itheima/domain/User.class
/wang/PowerShell/test/grep_cd_file/source/day02_eesy_01mybatisCRUD/target/test-classes/com/itheima/test/MybatisTest.class

输出文件(grepKekka1.csv):

day02_eesy_01mybatisCRUD.iml
pom.xml
.gitignore
compiler.xml
jarRepositories.xml
misc.xml
workspace.xml
IUserDao.java
QueryVo.java
User.java
jdbcConfig.properties
log4j.properties
SqlMapConfig.xml
IUserDao.xml
MybatisTest.java
jdbcConfig.properties
log4j.properties
SqlMapConfig.xml
IUserDao.class
IUserDao.xml
QueryVo.class
User.class
MybatisTest.class

输出文件(grepKekka2.csv):

C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\day02_eesy_01mybatisCRUD.iml
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\pom.xml
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\.idea\.gitignore
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\.idea\compiler.xml
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\.idea\jarRepositories.xml
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\.idea\misc.xml
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\.idea\workspace.xml
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\src\main\java\com\itheima\dao\IUserDao.java
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\src\main\java\com\itheima\domain\QueryVo.java
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\src\main\java\com\itheima\domain\User.java
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\src\main\resources\jdbcConfig.properties
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\src\main\resources\log4j.properties
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\src\main\resources\SqlMapConfig.xml
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\src\main\resources\com\itheima\dao\IUserDao.xml
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\src\test\java\com\itheima\test\MybatisTest.java
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\target\classes\jdbcConfig.properties
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\target\classes\log4j.properties
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\target\classes\SqlMapConfig.xml
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\target\classes\com\itheima\dao\IUserDao.class
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\target\classes\com\itheima\dao\IUserDao.xml
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\target\classes\com\itheima\domain\QueryVo.class
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\target\classes\com\itheima\domain\User.class
C:\wang\PowerShell\test\grep_cd_file\source\day02_eesy_01mybatisCRUD\target\test-classes\com\itheima\test\MybatisTest.class


结语:

为了将Windows系统下的Java编译文件与linux服务器上的文件进行比较,故进行此文件路径的获取及路径处理。
在只有文件路径 而没有实际文件的情况下的比较。文章来源地址https://www.toymoban.com/news/detail-643562.html


到了这里,关于PowerShell 获取某目录下所有的文件、文件夹,同时对获取到的文件路径字符串进行替换处理的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • python获取文件夹下所有文件

    python获取文件夹下所有文件,大家肯定都会想到os.listdir了,但是这种方法只能够获取到当前文件夹中的所有文件/文件夹,如果我想获得文件夹-文件夹-文件呢? 我的需求是只要文件夹中所有非文件夹,换句话来说对于我们的需求只有文件夹和非文件夹两种情况,那么直接联

    2024年02月12日
    浏览(36)
  • java获取文件夹下所有的文件

    对于一个 嵌套 的目录,获取目录下所有的文件,可以使用以下两种方式: 1. 通过递归获取 File类提供了如下两个方法: file.list():返回目录下文件和子目录名;(不会递归) file.listFiles():返回目录下文件和子目录File对象;(不会递归) 例子中List只是存放了文件夹下所有的

    2024年02月07日
    浏览(41)
  • java获取文件夹下所有文件名

      在进行 Java编程的过程中,我们会经常使用到文件夹下的所有文件名。有时候可能不太熟悉 Java编程的小伙伴们会发现,在代码中没有获取到所有的文件名,那么这个时候我们应该怎么去获取到这些文件呢?在进行 Java编程的过程中,我们会经常使用到 Java里面的 JDBC数据库连

    2024年02月10日
    浏览(47)
  • java获取某个文件夹下的所有文件

    目录 一.前言 二.获取文件夹下的文件路径 在我们平时编写开发文档的时候, 我们会获取到项目文件中的所有子文件来展示我们的源代码所储存的位置, 获取我们项目下的所有文件路径,  这时我们会如何用Java代码来获取我们项目下的所有文件呢, 今天我们来比编写一下代码 在

    2024年02月12日
    浏览(47)
  • 【Python】获取指定目录下的文件夹和文件

    我们经常会有对文件做批量处理的需求,获取指定目录下的文件夹和文件(有时需要获取所有文件,即子目录下的文件也需要获取)。Python 中扫描目录有两种方法: os.listdir() os.walk() 建立项目框架如下: 其中, test:项目文件夹名称,含有 aa子文件夹 和 main.py aa:文件夹,含

    2024年02月17日
    浏览(42)
  • 如何用python获取单个文件 或 文件夹中所有文件的行数

    本例展示获取单个txt文件中的行数: 本例展示获取labels下的所有txt文件内容的总行数: 函数解释 os.walk()是一种遍历目录数的函数,机理类似深度优先搜索和层次搜索策略,其返回的是 root dirs files 返回值解释 root :代表当前遍历的目录路径,string类型 dirs :代表root路径下的

    2024年02月09日
    浏览(49)
  • 3、HDFS的使用(读写、上传、下载、遍历、查找文件、整个目录拷贝、只拷贝文件、列出文件夹下文件、删除文件及目录、获取文件及文件夹属性等)-java

    1、hadoop3.1.4简单介绍及部署、简单验证 2、HDFS操作 - shell客户端 3、HDFS的使用(读写、上传、下载、遍历、查找文件、整个目录拷贝、只拷贝文件、列出文件夹下文件、删除文件及目录、获取文件及文件夹属性等)-java 4、HDFS-java操作类HDFSUtil及junit测试(HDFS的常见操作以及H

    2024年02月16日
    浏览(35)
  • Python小技巧【1】——获取指定文件夹下的所有文件【glob模块】

    glob模块 是Python标准库中一个重要的模块,主要用来 查找符合特定规则的目录和文件,并将搜索的到的结果返回到一个列表中。  1、常用函数glob() 返回符合匹配条件的所有文件的路径。  2、重要参数recursive recursive参数 代表是否递归调用,与特殊通配符 ** 一同使用,默认为

    2024年02月04日
    浏览(37)
  • Python os.listdir方法(获取文件夹目录下的内容)

    os.listdir 方法。参数为文件夹路径, 可以返回文件夹下的所有子文件、文件名称, 但不能返回子文件夹下的文件

    2024年02月14日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包