Recursively search for each item in txt file in multiple folders
I want to be able to do optimized search for each subfolder that not matches my files in my txt file.
Example of txt file:
1.2.0.0_009
1.2.0.0_008
1.2.0.0_007
1.2.0.0_006
1.2.0.0_005
1.2.0.0_004
1.2.0.0_003
1.2.0.0_002
1.2.0.0_001
1.1.0.0_106
1.1.0.0_105
1.1.0.0_104
1.1.0.0_103
1.1.0.0_102
1.1.0.0_101
1.1.0.0_100
1.0.0.0_089
1.1.0.0_099
1.0.0.0_088
1.0.0.0_087
1.0.0.0_086
1.0.0.0_085
1.0.0.0_084
1.0.0.0_083
1.0.0.0_082
1.0.0.0_081
1.0.0.0_080
1.0.0.0_079
1.0.0.0_078
1.0.0.0_077
1.0.0.0_076
1.0.0.0_075
1.0.0.0_074
1.0.0.0_073
1.0.0.0_072
1.0.0.0_071
1.0.0.0_070
1.0.0.0_069
1.0.0.0_068
1.0.0.0_067
1.0.0.0_066
1.0.0.0_065
1.0.0.0_064
1.0.0.0_063
1.0.0.0_062
1.0.0.0_061
1.0.0.0_060
1.0.0.0_052
1.0.0.0_051
1.0.0.0_050
1.0.0.0_049
1.0.0.0_048
1.0.0.0_047
1.0.0.0_046
1.0.0.0_045
1.0.0.0_044
1.0.0.0_043
1.0.0.0_042
1.0.0.0_041
1.0.0.0_040
1.0.0.0_039
1.0.0.0_038
1.0.0.0_037
1.0.0.0_036_1
1.0.0.0_036
0.0.0.1_89
0.0.0.1_88
0.0.0.1_87
0.0.0.1_86
0.0.0.1_85_1
0.0.0.1_85
0.0.0.1_84
0.0.0.1_83
0.0.0.1_82
0.0.0.1_81
0.0.0.1_80_1
0.0.0.1_80
0.0.0.1_79_1
0.0.0.1_79
0.0.0.1_78
0.0.0.1_77
0.0.0.1_76
0.0.0.1_75
0.0.0.1_74
0.0.0.1_73
0.0.0.1_72
0.0.0.1_71
0.0.0.1_70
0.0.0.1_69
0.0.0.1_68
0.0.0.1_67
0.0.0.1_66
0.0.0.1_65
0.0.0.1_64
0.0.0.1_63
0.0.0.1_62
0.0.0.1_61
0.0.0.1_60
0.0.0.1_59
0.0.0.1_58
0.0.0.1_57
0.0.0.1_56
0.0.0.1_55
0.0.0.1_54
0.0.0.1_53
0.0.0.1_52_1
0.0.0.1_52
0.0.0.1_51
0.0.0.1_50
0.0.0.1_49
0.0.0.1_48
0.0.0.1_47_1
0.0.0.1_47
0.0.0.1_46
0.0.0.1_45
0.0.0.1_44_1
0.0.0.1_44
0.0.0.1_43_1
0.0.0.1_43
0.0.0.1_42
0.0.0.1_41
0.0.0.1_40
0.0.0.1_39
0.0.0.1_38
0.0.0.1_37
0.0.0.1_36
0.0.0.1_35
0.0.0.1_34
0.0.0.1_33
0.0.0.1_32
0.0.0.1_31_1
0.0.0.1_31
0.0.0.1_30
0.0.0.1_29
0.0.0.1_28
0.0.0.1_27
0.0.0.1_26
0.0.0.1_25
0.0.0.1_24
0.0.0.1_23
0.0.0.1_22
0.0.0.1_21
0.0.0.1_20
0.0.0.1_19
0.0.0.1_18
0.0.0.1_17
0.0.0.1_168
0.0.0.1_167
0.0.0.1_166
0.0.0.1_165
0.0.0.1_164
0.0.0.1_163
0.0.0.1_162
0.0.0.1_161
Now the first digits represnts folder (for e.g. 1.2.0.0 is a folder) which contains files (009,008, etc...). But also that folder contains and other files that are not contained in my txt file. I want to compare this txt file with each folder in my MainFolder:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 11/22/2018 1:04 PM 0.0.0.1
d----- 11/22/2018 1:04 PM 1.0.0.0
d----- 11/22/2018 1:04 PM 1.1.0.0
d----- 11/22/2018 1:04 PM 1.2.0.0
-a---- 11/13/2018 11:54 AM 70656 DbUp.dll
-a---- 11/13/2018 11:54 AM 230912 DbUp.pdb
and copy only those which are not contained in txt file.
My Approach:
[string]$SourceTxtDbFile = "C:file.txt"
$delimitedFile = Get-Content $SourceTxtDbFile | ConvertFrom-String -Delimiter "_" -PropertyNames DbVersion, ScriptNumber
#foreach file in $delimitedFile
foreach ($file in $file_list){
# foreach file in the folders
foreach($dir in (Get-ChildItem $source -Recurse)){
# if the file name is in diretocry listed
if($file -eq $dir.name){
# copy only once, if the document name already exists, skip
if(-not(test-path "$destination$file")){
# copy the file
Copy-Item $dir.fullname -Destination $destination -Verbose
}
}
}
}
But I assume that it can be more optimized code.
UPDATE:
By the courtesy of @LotPings
Here is the working code (edited by myself):
$uatScripts = (Get-Content $uatFile).Trim() # there were trailing spaces
Push-Location $source
Get-ChildItem ** -File | ForEach-Object {
$LookUp = "{0}" -f $_.BaseName
If ($uatScripts -notcontains $LookUp) {
Copy-Item $_.FullName -Destination $destination
}
}
powershell
|
show 2 more comments
I want to be able to do optimized search for each subfolder that not matches my files in my txt file.
Example of txt file:
1.2.0.0_009
1.2.0.0_008
1.2.0.0_007
1.2.0.0_006
1.2.0.0_005
1.2.0.0_004
1.2.0.0_003
1.2.0.0_002
1.2.0.0_001
1.1.0.0_106
1.1.0.0_105
1.1.0.0_104
1.1.0.0_103
1.1.0.0_102
1.1.0.0_101
1.1.0.0_100
1.0.0.0_089
1.1.0.0_099
1.0.0.0_088
1.0.0.0_087
1.0.0.0_086
1.0.0.0_085
1.0.0.0_084
1.0.0.0_083
1.0.0.0_082
1.0.0.0_081
1.0.0.0_080
1.0.0.0_079
1.0.0.0_078
1.0.0.0_077
1.0.0.0_076
1.0.0.0_075
1.0.0.0_074
1.0.0.0_073
1.0.0.0_072
1.0.0.0_071
1.0.0.0_070
1.0.0.0_069
1.0.0.0_068
1.0.0.0_067
1.0.0.0_066
1.0.0.0_065
1.0.0.0_064
1.0.0.0_063
1.0.0.0_062
1.0.0.0_061
1.0.0.0_060
1.0.0.0_052
1.0.0.0_051
1.0.0.0_050
1.0.0.0_049
1.0.0.0_048
1.0.0.0_047
1.0.0.0_046
1.0.0.0_045
1.0.0.0_044
1.0.0.0_043
1.0.0.0_042
1.0.0.0_041
1.0.0.0_040
1.0.0.0_039
1.0.0.0_038
1.0.0.0_037
1.0.0.0_036_1
1.0.0.0_036
0.0.0.1_89
0.0.0.1_88
0.0.0.1_87
0.0.0.1_86
0.0.0.1_85_1
0.0.0.1_85
0.0.0.1_84
0.0.0.1_83
0.0.0.1_82
0.0.0.1_81
0.0.0.1_80_1
0.0.0.1_80
0.0.0.1_79_1
0.0.0.1_79
0.0.0.1_78
0.0.0.1_77
0.0.0.1_76
0.0.0.1_75
0.0.0.1_74
0.0.0.1_73
0.0.0.1_72
0.0.0.1_71
0.0.0.1_70
0.0.0.1_69
0.0.0.1_68
0.0.0.1_67
0.0.0.1_66
0.0.0.1_65
0.0.0.1_64
0.0.0.1_63
0.0.0.1_62
0.0.0.1_61
0.0.0.1_60
0.0.0.1_59
0.0.0.1_58
0.0.0.1_57
0.0.0.1_56
0.0.0.1_55
0.0.0.1_54
0.0.0.1_53
0.0.0.1_52_1
0.0.0.1_52
0.0.0.1_51
0.0.0.1_50
0.0.0.1_49
0.0.0.1_48
0.0.0.1_47_1
0.0.0.1_47
0.0.0.1_46
0.0.0.1_45
0.0.0.1_44_1
0.0.0.1_44
0.0.0.1_43_1
0.0.0.1_43
0.0.0.1_42
0.0.0.1_41
0.0.0.1_40
0.0.0.1_39
0.0.0.1_38
0.0.0.1_37
0.0.0.1_36
0.0.0.1_35
0.0.0.1_34
0.0.0.1_33
0.0.0.1_32
0.0.0.1_31_1
0.0.0.1_31
0.0.0.1_30
0.0.0.1_29
0.0.0.1_28
0.0.0.1_27
0.0.0.1_26
0.0.0.1_25
0.0.0.1_24
0.0.0.1_23
0.0.0.1_22
0.0.0.1_21
0.0.0.1_20
0.0.0.1_19
0.0.0.1_18
0.0.0.1_17
0.0.0.1_168
0.0.0.1_167
0.0.0.1_166
0.0.0.1_165
0.0.0.1_164
0.0.0.1_163
0.0.0.1_162
0.0.0.1_161
Now the first digits represnts folder (for e.g. 1.2.0.0 is a folder) which contains files (009,008, etc...). But also that folder contains and other files that are not contained in my txt file. I want to compare this txt file with each folder in my MainFolder:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 11/22/2018 1:04 PM 0.0.0.1
d----- 11/22/2018 1:04 PM 1.0.0.0
d----- 11/22/2018 1:04 PM 1.1.0.0
d----- 11/22/2018 1:04 PM 1.2.0.0
-a---- 11/13/2018 11:54 AM 70656 DbUp.dll
-a---- 11/13/2018 11:54 AM 230912 DbUp.pdb
and copy only those which are not contained in txt file.
My Approach:
[string]$SourceTxtDbFile = "C:file.txt"
$delimitedFile = Get-Content $SourceTxtDbFile | ConvertFrom-String -Delimiter "_" -PropertyNames DbVersion, ScriptNumber
#foreach file in $delimitedFile
foreach ($file in $file_list){
# foreach file in the folders
foreach($dir in (Get-ChildItem $source -Recurse)){
# if the file name is in diretocry listed
if($file -eq $dir.name){
# copy only once, if the document name already exists, skip
if(-not(test-path "$destination$file")){
# copy the file
Copy-Item $dir.fullname -Destination $destination -Verbose
}
}
}
}
But I assume that it can be more optimized code.
UPDATE:
By the courtesy of @LotPings
Here is the working code (edited by myself):
$uatScripts = (Get-Content $uatFile).Trim() # there were trailing spaces
Push-Location $source
Get-ChildItem ** -File | ForEach-Object {
$LookUp = "{0}" -f $_.BaseName
If ($uatScripts -notcontains $LookUp) {
Copy-Item $_.FullName -Destination $destination
}
}
powershell
Basically, I want to check all my folders do they have some file that isn't in the txt file. And if they do, to copy-item somewhere in new folder
– Stefan0309
Nov 22 at 12:37
1
If you want to improve your allready working code, try to post it in the CodeReview Page: codereview.stackexchange.com
– Paxz
Nov 22 at 12:38
@Paxz thanks, I know, but I am not sure if there any bugs. Thanks anyway.
– Stefan0309
Nov 22 at 12:43
Are all the files placed directly under the0.0.0.x
folders or are there subfolder inside the main folders??
– Mathias R. Jessen
Nov 22 at 13:51
@MathiasR.Jessen there are multiple folders inside main foilder. please refer to my question, you will see tha tree directory of the main folder. Inside of each x.x.x.x folders there are only sql files, so, there no folders of the depth of 2
– Stefan0309
Nov 22 at 13:55
|
show 2 more comments
I want to be able to do optimized search for each subfolder that not matches my files in my txt file.
Example of txt file:
1.2.0.0_009
1.2.0.0_008
1.2.0.0_007
1.2.0.0_006
1.2.0.0_005
1.2.0.0_004
1.2.0.0_003
1.2.0.0_002
1.2.0.0_001
1.1.0.0_106
1.1.0.0_105
1.1.0.0_104
1.1.0.0_103
1.1.0.0_102
1.1.0.0_101
1.1.0.0_100
1.0.0.0_089
1.1.0.0_099
1.0.0.0_088
1.0.0.0_087
1.0.0.0_086
1.0.0.0_085
1.0.0.0_084
1.0.0.0_083
1.0.0.0_082
1.0.0.0_081
1.0.0.0_080
1.0.0.0_079
1.0.0.0_078
1.0.0.0_077
1.0.0.0_076
1.0.0.0_075
1.0.0.0_074
1.0.0.0_073
1.0.0.0_072
1.0.0.0_071
1.0.0.0_070
1.0.0.0_069
1.0.0.0_068
1.0.0.0_067
1.0.0.0_066
1.0.0.0_065
1.0.0.0_064
1.0.0.0_063
1.0.0.0_062
1.0.0.0_061
1.0.0.0_060
1.0.0.0_052
1.0.0.0_051
1.0.0.0_050
1.0.0.0_049
1.0.0.0_048
1.0.0.0_047
1.0.0.0_046
1.0.0.0_045
1.0.0.0_044
1.0.0.0_043
1.0.0.0_042
1.0.0.0_041
1.0.0.0_040
1.0.0.0_039
1.0.0.0_038
1.0.0.0_037
1.0.0.0_036_1
1.0.0.0_036
0.0.0.1_89
0.0.0.1_88
0.0.0.1_87
0.0.0.1_86
0.0.0.1_85_1
0.0.0.1_85
0.0.0.1_84
0.0.0.1_83
0.0.0.1_82
0.0.0.1_81
0.0.0.1_80_1
0.0.0.1_80
0.0.0.1_79_1
0.0.0.1_79
0.0.0.1_78
0.0.0.1_77
0.0.0.1_76
0.0.0.1_75
0.0.0.1_74
0.0.0.1_73
0.0.0.1_72
0.0.0.1_71
0.0.0.1_70
0.0.0.1_69
0.0.0.1_68
0.0.0.1_67
0.0.0.1_66
0.0.0.1_65
0.0.0.1_64
0.0.0.1_63
0.0.0.1_62
0.0.0.1_61
0.0.0.1_60
0.0.0.1_59
0.0.0.1_58
0.0.0.1_57
0.0.0.1_56
0.0.0.1_55
0.0.0.1_54
0.0.0.1_53
0.0.0.1_52_1
0.0.0.1_52
0.0.0.1_51
0.0.0.1_50
0.0.0.1_49
0.0.0.1_48
0.0.0.1_47_1
0.0.0.1_47
0.0.0.1_46
0.0.0.1_45
0.0.0.1_44_1
0.0.0.1_44
0.0.0.1_43_1
0.0.0.1_43
0.0.0.1_42
0.0.0.1_41
0.0.0.1_40
0.0.0.1_39
0.0.0.1_38
0.0.0.1_37
0.0.0.1_36
0.0.0.1_35
0.0.0.1_34
0.0.0.1_33
0.0.0.1_32
0.0.0.1_31_1
0.0.0.1_31
0.0.0.1_30
0.0.0.1_29
0.0.0.1_28
0.0.0.1_27
0.0.0.1_26
0.0.0.1_25
0.0.0.1_24
0.0.0.1_23
0.0.0.1_22
0.0.0.1_21
0.0.0.1_20
0.0.0.1_19
0.0.0.1_18
0.0.0.1_17
0.0.0.1_168
0.0.0.1_167
0.0.0.1_166
0.0.0.1_165
0.0.0.1_164
0.0.0.1_163
0.0.0.1_162
0.0.0.1_161
Now the first digits represnts folder (for e.g. 1.2.0.0 is a folder) which contains files (009,008, etc...). But also that folder contains and other files that are not contained in my txt file. I want to compare this txt file with each folder in my MainFolder:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 11/22/2018 1:04 PM 0.0.0.1
d----- 11/22/2018 1:04 PM 1.0.0.0
d----- 11/22/2018 1:04 PM 1.1.0.0
d----- 11/22/2018 1:04 PM 1.2.0.0
-a---- 11/13/2018 11:54 AM 70656 DbUp.dll
-a---- 11/13/2018 11:54 AM 230912 DbUp.pdb
and copy only those which are not contained in txt file.
My Approach:
[string]$SourceTxtDbFile = "C:file.txt"
$delimitedFile = Get-Content $SourceTxtDbFile | ConvertFrom-String -Delimiter "_" -PropertyNames DbVersion, ScriptNumber
#foreach file in $delimitedFile
foreach ($file in $file_list){
# foreach file in the folders
foreach($dir in (Get-ChildItem $source -Recurse)){
# if the file name is in diretocry listed
if($file -eq $dir.name){
# copy only once, if the document name already exists, skip
if(-not(test-path "$destination$file")){
# copy the file
Copy-Item $dir.fullname -Destination $destination -Verbose
}
}
}
}
But I assume that it can be more optimized code.
UPDATE:
By the courtesy of @LotPings
Here is the working code (edited by myself):
$uatScripts = (Get-Content $uatFile).Trim() # there were trailing spaces
Push-Location $source
Get-ChildItem ** -File | ForEach-Object {
$LookUp = "{0}" -f $_.BaseName
If ($uatScripts -notcontains $LookUp) {
Copy-Item $_.FullName -Destination $destination
}
}
powershell
I want to be able to do optimized search for each subfolder that not matches my files in my txt file.
Example of txt file:
1.2.0.0_009
1.2.0.0_008
1.2.0.0_007
1.2.0.0_006
1.2.0.0_005
1.2.0.0_004
1.2.0.0_003
1.2.0.0_002
1.2.0.0_001
1.1.0.0_106
1.1.0.0_105
1.1.0.0_104
1.1.0.0_103
1.1.0.0_102
1.1.0.0_101
1.1.0.0_100
1.0.0.0_089
1.1.0.0_099
1.0.0.0_088
1.0.0.0_087
1.0.0.0_086
1.0.0.0_085
1.0.0.0_084
1.0.0.0_083
1.0.0.0_082
1.0.0.0_081
1.0.0.0_080
1.0.0.0_079
1.0.0.0_078
1.0.0.0_077
1.0.0.0_076
1.0.0.0_075
1.0.0.0_074
1.0.0.0_073
1.0.0.0_072
1.0.0.0_071
1.0.0.0_070
1.0.0.0_069
1.0.0.0_068
1.0.0.0_067
1.0.0.0_066
1.0.0.0_065
1.0.0.0_064
1.0.0.0_063
1.0.0.0_062
1.0.0.0_061
1.0.0.0_060
1.0.0.0_052
1.0.0.0_051
1.0.0.0_050
1.0.0.0_049
1.0.0.0_048
1.0.0.0_047
1.0.0.0_046
1.0.0.0_045
1.0.0.0_044
1.0.0.0_043
1.0.0.0_042
1.0.0.0_041
1.0.0.0_040
1.0.0.0_039
1.0.0.0_038
1.0.0.0_037
1.0.0.0_036_1
1.0.0.0_036
0.0.0.1_89
0.0.0.1_88
0.0.0.1_87
0.0.0.1_86
0.0.0.1_85_1
0.0.0.1_85
0.0.0.1_84
0.0.0.1_83
0.0.0.1_82
0.0.0.1_81
0.0.0.1_80_1
0.0.0.1_80
0.0.0.1_79_1
0.0.0.1_79
0.0.0.1_78
0.0.0.1_77
0.0.0.1_76
0.0.0.1_75
0.0.0.1_74
0.0.0.1_73
0.0.0.1_72
0.0.0.1_71
0.0.0.1_70
0.0.0.1_69
0.0.0.1_68
0.0.0.1_67
0.0.0.1_66
0.0.0.1_65
0.0.0.1_64
0.0.0.1_63
0.0.0.1_62
0.0.0.1_61
0.0.0.1_60
0.0.0.1_59
0.0.0.1_58
0.0.0.1_57
0.0.0.1_56
0.0.0.1_55
0.0.0.1_54
0.0.0.1_53
0.0.0.1_52_1
0.0.0.1_52
0.0.0.1_51
0.0.0.1_50
0.0.0.1_49
0.0.0.1_48
0.0.0.1_47_1
0.0.0.1_47
0.0.0.1_46
0.0.0.1_45
0.0.0.1_44_1
0.0.0.1_44
0.0.0.1_43_1
0.0.0.1_43
0.0.0.1_42
0.0.0.1_41
0.0.0.1_40
0.0.0.1_39
0.0.0.1_38
0.0.0.1_37
0.0.0.1_36
0.0.0.1_35
0.0.0.1_34
0.0.0.1_33
0.0.0.1_32
0.0.0.1_31_1
0.0.0.1_31
0.0.0.1_30
0.0.0.1_29
0.0.0.1_28
0.0.0.1_27
0.0.0.1_26
0.0.0.1_25
0.0.0.1_24
0.0.0.1_23
0.0.0.1_22
0.0.0.1_21
0.0.0.1_20
0.0.0.1_19
0.0.0.1_18
0.0.0.1_17
0.0.0.1_168
0.0.0.1_167
0.0.0.1_166
0.0.0.1_165
0.0.0.1_164
0.0.0.1_163
0.0.0.1_162
0.0.0.1_161
Now the first digits represnts folder (for e.g. 1.2.0.0 is a folder) which contains files (009,008, etc...). But also that folder contains and other files that are not contained in my txt file. I want to compare this txt file with each folder in my MainFolder:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 11/22/2018 1:04 PM 0.0.0.1
d----- 11/22/2018 1:04 PM 1.0.0.0
d----- 11/22/2018 1:04 PM 1.1.0.0
d----- 11/22/2018 1:04 PM 1.2.0.0
-a---- 11/13/2018 11:54 AM 70656 DbUp.dll
-a---- 11/13/2018 11:54 AM 230912 DbUp.pdb
and copy only those which are not contained in txt file.
My Approach:
[string]$SourceTxtDbFile = "C:file.txt"
$delimitedFile = Get-Content $SourceTxtDbFile | ConvertFrom-String -Delimiter "_" -PropertyNames DbVersion, ScriptNumber
#foreach file in $delimitedFile
foreach ($file in $file_list){
# foreach file in the folders
foreach($dir in (Get-ChildItem $source -Recurse)){
# if the file name is in diretocry listed
if($file -eq $dir.name){
# copy only once, if the document name already exists, skip
if(-not(test-path "$destination$file")){
# copy the file
Copy-Item $dir.fullname -Destination $destination -Verbose
}
}
}
}
But I assume that it can be more optimized code.
UPDATE:
By the courtesy of @LotPings
Here is the working code (edited by myself):
$uatScripts = (Get-Content $uatFile).Trim() # there were trailing spaces
Push-Location $source
Get-ChildItem ** -File | ForEach-Object {
$LookUp = "{0}" -f $_.BaseName
If ($uatScripts -notcontains $LookUp) {
Copy-Item $_.FullName -Destination $destination
}
}
powershell
powershell
edited Nov 22 at 16:56
asked Nov 22 at 12:35
Stefan0309
343422
343422
Basically, I want to check all my folders do they have some file that isn't in the txt file. And if they do, to copy-item somewhere in new folder
– Stefan0309
Nov 22 at 12:37
1
If you want to improve your allready working code, try to post it in the CodeReview Page: codereview.stackexchange.com
– Paxz
Nov 22 at 12:38
@Paxz thanks, I know, but I am not sure if there any bugs. Thanks anyway.
– Stefan0309
Nov 22 at 12:43
Are all the files placed directly under the0.0.0.x
folders or are there subfolder inside the main folders??
– Mathias R. Jessen
Nov 22 at 13:51
@MathiasR.Jessen there are multiple folders inside main foilder. please refer to my question, you will see tha tree directory of the main folder. Inside of each x.x.x.x folders there are only sql files, so, there no folders of the depth of 2
– Stefan0309
Nov 22 at 13:55
|
show 2 more comments
Basically, I want to check all my folders do they have some file that isn't in the txt file. And if they do, to copy-item somewhere in new folder
– Stefan0309
Nov 22 at 12:37
1
If you want to improve your allready working code, try to post it in the CodeReview Page: codereview.stackexchange.com
– Paxz
Nov 22 at 12:38
@Paxz thanks, I know, but I am not sure if there any bugs. Thanks anyway.
– Stefan0309
Nov 22 at 12:43
Are all the files placed directly under the0.0.0.x
folders or are there subfolder inside the main folders??
– Mathias R. Jessen
Nov 22 at 13:51
@MathiasR.Jessen there are multiple folders inside main foilder. please refer to my question, you will see tha tree directory of the main folder. Inside of each x.x.x.x folders there are only sql files, so, there no folders of the depth of 2
– Stefan0309
Nov 22 at 13:55
Basically, I want to check all my folders do they have some file that isn't in the txt file. And if they do, to copy-item somewhere in new folder
– Stefan0309
Nov 22 at 12:37
Basically, I want to check all my folders do they have some file that isn't in the txt file. And if they do, to copy-item somewhere in new folder
– Stefan0309
Nov 22 at 12:37
1
1
If you want to improve your allready working code, try to post it in the CodeReview Page: codereview.stackexchange.com
– Paxz
Nov 22 at 12:38
If you want to improve your allready working code, try to post it in the CodeReview Page: codereview.stackexchange.com
– Paxz
Nov 22 at 12:38
@Paxz thanks, I know, but I am not sure if there any bugs. Thanks anyway.
– Stefan0309
Nov 22 at 12:43
@Paxz thanks, I know, but I am not sure if there any bugs. Thanks anyway.
– Stefan0309
Nov 22 at 12:43
Are all the files placed directly under the
0.0.0.x
folders or are there subfolder inside the main folders??– Mathias R. Jessen
Nov 22 at 13:51
Are all the files placed directly under the
0.0.0.x
folders or are there subfolder inside the main folders??– Mathias R. Jessen
Nov 22 at 13:51
@MathiasR.Jessen there are multiple folders inside main foilder. please refer to my question, you will see tha tree directory of the main folder. Inside of each x.x.x.x folders there are only sql files, so, there no folders of the depth of 2
– Stefan0309
Nov 22 at 13:55
@MathiasR.Jessen there are multiple folders inside main foilder. please refer to my question, you will see tha tree directory of the main folder. Inside of each x.x.x.x folders there are only sql files, so, there no folders of the depth of 2
– Stefan0309
Nov 22 at 13:55
|
show 2 more comments
1 Answer
1
active
oldest
votes
IMO your script takes a wrong approach,
- I'd keep the content of
$SourceTxtDbFile
as is and - build corresponding entries from directory and file name
(where it is unclear if the files have an extension)
EDIT: With this shorted file.txt
0.0.0.1_86
0.0.0.1_85_1
0.0.0.1_85
0.0.0.1_163
0.0.0.1_162
0.0.0.1_161
and this tree on my ramdrive a:
> tree a: /f
Auflistung der Ordnerpfade für Volume RamDisk
Volumeseriennummer : 5566-7788
A:
│ file.txt
│
└───0.0.0.1
161
162
163
164
165
82
83
84
85
85_1
This slightly changed script
## Q:Test20181122SO_53431173.ps1
$source = "A:"
Push-Location $Source
$destination = "C:Test"
$SourceTxtDbFile = ".file.txt"
$DbVScript = (Get-Content $SourceTxtDbFile).Trim() # there were trailing spaces
Get-ChildItem ** -File | ForEach-Object{
$LookUp = "{0}_{1}" -f $_.Directory.Name,$_.Name
If ($DbVScript -notcontains $LookUp){
Copy-Item $_.FullName -Destination $destination -Verbose -WhatIf
} else {
"File {0} is in `$DbVScript: {1}" -f $_.FullName,$LookUp
}
}
Returns the following output.
(If the output looks OK, remove the trailing -WhatIf
)
> Q:Test20181122SO_53431173.ps1
File A:.0.0.1161 is in $DbVScript: 0.0.0.1_161
File A:.0.0.1162 is in $DbVScript: 0.0.0.1_162
File A:.0.0.1163 is in $DbVScript: 0.0.0.1_163
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.1164 Ziel: C:Test164".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.1165 Ziel: C:Test165".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.182 Ziel: C:Test82".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.183 Ziel: C:Test83".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.184 Ziel: C:Test84".
File A:.0.0.185 is in $DbVScript: 0.0.0.1_85
File A:.0.0.185_1 is in $DbVScript: 0.0.0.1_85_1
Thanks. But not good enough. Yout $LookUp variable is doubled like this: "0.0.0.1_0.0.0.1_01" and then when you do a matching it's not good comparison. Also I think that I need to check LookUp in DBVScript not DBVScript in LookUp :)
– Stefan0309
Nov 22 at 16:14
1
That doesn't happen here with my own test. What PowerShell version do you use? The order is correct; you probabply mean-notin
then it's the reverse orderIf ($LookUp -notin $DbVScript)
– LotPings
Nov 22 at 16:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431173%2frecursively-search-for-each-item-in-txt-file-in-multiple-folders%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
IMO your script takes a wrong approach,
- I'd keep the content of
$SourceTxtDbFile
as is and - build corresponding entries from directory and file name
(where it is unclear if the files have an extension)
EDIT: With this shorted file.txt
0.0.0.1_86
0.0.0.1_85_1
0.0.0.1_85
0.0.0.1_163
0.0.0.1_162
0.0.0.1_161
and this tree on my ramdrive a:
> tree a: /f
Auflistung der Ordnerpfade für Volume RamDisk
Volumeseriennummer : 5566-7788
A:
│ file.txt
│
└───0.0.0.1
161
162
163
164
165
82
83
84
85
85_1
This slightly changed script
## Q:Test20181122SO_53431173.ps1
$source = "A:"
Push-Location $Source
$destination = "C:Test"
$SourceTxtDbFile = ".file.txt"
$DbVScript = (Get-Content $SourceTxtDbFile).Trim() # there were trailing spaces
Get-ChildItem ** -File | ForEach-Object{
$LookUp = "{0}_{1}" -f $_.Directory.Name,$_.Name
If ($DbVScript -notcontains $LookUp){
Copy-Item $_.FullName -Destination $destination -Verbose -WhatIf
} else {
"File {0} is in `$DbVScript: {1}" -f $_.FullName,$LookUp
}
}
Returns the following output.
(If the output looks OK, remove the trailing -WhatIf
)
> Q:Test20181122SO_53431173.ps1
File A:.0.0.1161 is in $DbVScript: 0.0.0.1_161
File A:.0.0.1162 is in $DbVScript: 0.0.0.1_162
File A:.0.0.1163 is in $DbVScript: 0.0.0.1_163
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.1164 Ziel: C:Test164".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.1165 Ziel: C:Test165".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.182 Ziel: C:Test82".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.183 Ziel: C:Test83".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.184 Ziel: C:Test84".
File A:.0.0.185 is in $DbVScript: 0.0.0.1_85
File A:.0.0.185_1 is in $DbVScript: 0.0.0.1_85_1
Thanks. But not good enough. Yout $LookUp variable is doubled like this: "0.0.0.1_0.0.0.1_01" and then when you do a matching it's not good comparison. Also I think that I need to check LookUp in DBVScript not DBVScript in LookUp :)
– Stefan0309
Nov 22 at 16:14
1
That doesn't happen here with my own test. What PowerShell version do you use? The order is correct; you probabply mean-notin
then it's the reverse orderIf ($LookUp -notin $DbVScript)
– LotPings
Nov 22 at 16:42
add a comment |
IMO your script takes a wrong approach,
- I'd keep the content of
$SourceTxtDbFile
as is and - build corresponding entries from directory and file name
(where it is unclear if the files have an extension)
EDIT: With this shorted file.txt
0.0.0.1_86
0.0.0.1_85_1
0.0.0.1_85
0.0.0.1_163
0.0.0.1_162
0.0.0.1_161
and this tree on my ramdrive a:
> tree a: /f
Auflistung der Ordnerpfade für Volume RamDisk
Volumeseriennummer : 5566-7788
A:
│ file.txt
│
└───0.0.0.1
161
162
163
164
165
82
83
84
85
85_1
This slightly changed script
## Q:Test20181122SO_53431173.ps1
$source = "A:"
Push-Location $Source
$destination = "C:Test"
$SourceTxtDbFile = ".file.txt"
$DbVScript = (Get-Content $SourceTxtDbFile).Trim() # there were trailing spaces
Get-ChildItem ** -File | ForEach-Object{
$LookUp = "{0}_{1}" -f $_.Directory.Name,$_.Name
If ($DbVScript -notcontains $LookUp){
Copy-Item $_.FullName -Destination $destination -Verbose -WhatIf
} else {
"File {0} is in `$DbVScript: {1}" -f $_.FullName,$LookUp
}
}
Returns the following output.
(If the output looks OK, remove the trailing -WhatIf
)
> Q:Test20181122SO_53431173.ps1
File A:.0.0.1161 is in $DbVScript: 0.0.0.1_161
File A:.0.0.1162 is in $DbVScript: 0.0.0.1_162
File A:.0.0.1163 is in $DbVScript: 0.0.0.1_163
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.1164 Ziel: C:Test164".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.1165 Ziel: C:Test165".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.182 Ziel: C:Test82".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.183 Ziel: C:Test83".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.184 Ziel: C:Test84".
File A:.0.0.185 is in $DbVScript: 0.0.0.1_85
File A:.0.0.185_1 is in $DbVScript: 0.0.0.1_85_1
Thanks. But not good enough. Yout $LookUp variable is doubled like this: "0.0.0.1_0.0.0.1_01" and then when you do a matching it's not good comparison. Also I think that I need to check LookUp in DBVScript not DBVScript in LookUp :)
– Stefan0309
Nov 22 at 16:14
1
That doesn't happen here with my own test. What PowerShell version do you use? The order is correct; you probabply mean-notin
then it's the reverse orderIf ($LookUp -notin $DbVScript)
– LotPings
Nov 22 at 16:42
add a comment |
IMO your script takes a wrong approach,
- I'd keep the content of
$SourceTxtDbFile
as is and - build corresponding entries from directory and file name
(where it is unclear if the files have an extension)
EDIT: With this shorted file.txt
0.0.0.1_86
0.0.0.1_85_1
0.0.0.1_85
0.0.0.1_163
0.0.0.1_162
0.0.0.1_161
and this tree on my ramdrive a:
> tree a: /f
Auflistung der Ordnerpfade für Volume RamDisk
Volumeseriennummer : 5566-7788
A:
│ file.txt
│
└───0.0.0.1
161
162
163
164
165
82
83
84
85
85_1
This slightly changed script
## Q:Test20181122SO_53431173.ps1
$source = "A:"
Push-Location $Source
$destination = "C:Test"
$SourceTxtDbFile = ".file.txt"
$DbVScript = (Get-Content $SourceTxtDbFile).Trim() # there were trailing spaces
Get-ChildItem ** -File | ForEach-Object{
$LookUp = "{0}_{1}" -f $_.Directory.Name,$_.Name
If ($DbVScript -notcontains $LookUp){
Copy-Item $_.FullName -Destination $destination -Verbose -WhatIf
} else {
"File {0} is in `$DbVScript: {1}" -f $_.FullName,$LookUp
}
}
Returns the following output.
(If the output looks OK, remove the trailing -WhatIf
)
> Q:Test20181122SO_53431173.ps1
File A:.0.0.1161 is in $DbVScript: 0.0.0.1_161
File A:.0.0.1162 is in $DbVScript: 0.0.0.1_162
File A:.0.0.1163 is in $DbVScript: 0.0.0.1_163
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.1164 Ziel: C:Test164".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.1165 Ziel: C:Test165".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.182 Ziel: C:Test82".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.183 Ziel: C:Test83".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.184 Ziel: C:Test84".
File A:.0.0.185 is in $DbVScript: 0.0.0.1_85
File A:.0.0.185_1 is in $DbVScript: 0.0.0.1_85_1
IMO your script takes a wrong approach,
- I'd keep the content of
$SourceTxtDbFile
as is and - build corresponding entries from directory and file name
(where it is unclear if the files have an extension)
EDIT: With this shorted file.txt
0.0.0.1_86
0.0.0.1_85_1
0.0.0.1_85
0.0.0.1_163
0.0.0.1_162
0.0.0.1_161
and this tree on my ramdrive a:
> tree a: /f
Auflistung der Ordnerpfade für Volume RamDisk
Volumeseriennummer : 5566-7788
A:
│ file.txt
│
└───0.0.0.1
161
162
163
164
165
82
83
84
85
85_1
This slightly changed script
## Q:Test20181122SO_53431173.ps1
$source = "A:"
Push-Location $Source
$destination = "C:Test"
$SourceTxtDbFile = ".file.txt"
$DbVScript = (Get-Content $SourceTxtDbFile).Trim() # there were trailing spaces
Get-ChildItem ** -File | ForEach-Object{
$LookUp = "{0}_{1}" -f $_.Directory.Name,$_.Name
If ($DbVScript -notcontains $LookUp){
Copy-Item $_.FullName -Destination $destination -Verbose -WhatIf
} else {
"File {0} is in `$DbVScript: {1}" -f $_.FullName,$LookUp
}
}
Returns the following output.
(If the output looks OK, remove the trailing -WhatIf
)
> Q:Test20181122SO_53431173.ps1
File A:.0.0.1161 is in $DbVScript: 0.0.0.1_161
File A:.0.0.1162 is in $DbVScript: 0.0.0.1_162
File A:.0.0.1163 is in $DbVScript: 0.0.0.1_163
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.1164 Ziel: C:Test164".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.1165 Ziel: C:Test165".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.182 Ziel: C:Test82".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.183 Ziel: C:Test83".
WhatIf: Ausführen des Vorgangs "Datei kopieren" für das Ziel "Element: A:.0.0.184 Ziel: C:Test84".
File A:.0.0.185 is in $DbVScript: 0.0.0.1_85
File A:.0.0.185_1 is in $DbVScript: 0.0.0.1_85_1
edited Nov 22 at 16:56
answered Nov 22 at 15:36
LotPings
17.3k61532
17.3k61532
Thanks. But not good enough. Yout $LookUp variable is doubled like this: "0.0.0.1_0.0.0.1_01" and then when you do a matching it's not good comparison. Also I think that I need to check LookUp in DBVScript not DBVScript in LookUp :)
– Stefan0309
Nov 22 at 16:14
1
That doesn't happen here with my own test. What PowerShell version do you use? The order is correct; you probabply mean-notin
then it's the reverse orderIf ($LookUp -notin $DbVScript)
– LotPings
Nov 22 at 16:42
add a comment |
Thanks. But not good enough. Yout $LookUp variable is doubled like this: "0.0.0.1_0.0.0.1_01" and then when you do a matching it's not good comparison. Also I think that I need to check LookUp in DBVScript not DBVScript in LookUp :)
– Stefan0309
Nov 22 at 16:14
1
That doesn't happen here with my own test. What PowerShell version do you use? The order is correct; you probabply mean-notin
then it's the reverse orderIf ($LookUp -notin $DbVScript)
– LotPings
Nov 22 at 16:42
Thanks. But not good enough. Yout $LookUp variable is doubled like this: "0.0.0.1_0.0.0.1_01" and then when you do a matching it's not good comparison. Also I think that I need to check LookUp in DBVScript not DBVScript in LookUp :)
– Stefan0309
Nov 22 at 16:14
Thanks. But not good enough. Yout $LookUp variable is doubled like this: "0.0.0.1_0.0.0.1_01" and then when you do a matching it's not good comparison. Also I think that I need to check LookUp in DBVScript not DBVScript in LookUp :)
– Stefan0309
Nov 22 at 16:14
1
1
That doesn't happen here with my own test. What PowerShell version do you use? The order is correct; you probabply mean
-notin
then it's the reverse order If ($LookUp -notin $DbVScript)
– LotPings
Nov 22 at 16:42
That doesn't happen here with my own test. What PowerShell version do you use? The order is correct; you probabply mean
-notin
then it's the reverse order If ($LookUp -notin $DbVScript)
– LotPings
Nov 22 at 16:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431173%2frecursively-search-for-each-item-in-txt-file-in-multiple-folders%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Basically, I want to check all my folders do they have some file that isn't in the txt file. And if they do, to copy-item somewhere in new folder
– Stefan0309
Nov 22 at 12:37
1
If you want to improve your allready working code, try to post it in the CodeReview Page: codereview.stackexchange.com
– Paxz
Nov 22 at 12:38
@Paxz thanks, I know, but I am not sure if there any bugs. Thanks anyway.
– Stefan0309
Nov 22 at 12:43
Are all the files placed directly under the
0.0.0.x
folders or are there subfolder inside the main folders??– Mathias R. Jessen
Nov 22 at 13:51
@MathiasR.Jessen there are multiple folders inside main foilder. please refer to my question, you will see tha tree directory of the main folder. Inside of each x.x.x.x folders there are only sql files, so, there no folders of the depth of 2
– Stefan0309
Nov 22 at 13:55