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
$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
}<# 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服务器上的文件进行比较,故进行此文件路径的获取及路径处理。
在只有文件路径 而没有实际文件的情况下的比较。